func ensureCap(s []byte, n int) []byte {
    if n <= cap(s) {
        return s
    }
    // logic adapted from appendslice1 in runtime