ClickHouse/ch-go

check slice capacity to avoid unnecessary memory allocation

qixiaogang opened this issue · 2 comments

  1. When I use function https://github.com/ClickHouse/ch-go/blob/main/compress/writer.go#L21-L52, find https://github.com/ClickHouse/ch-go/blob/main/compress/writer.go#L23 don't need to createmake([]byte, maxSize+headerSize)when cap(w.Data) >= maxSize+headerSize
  2. And for EncodeColumn, when b.Buf has enough capacity, then no need to create make([]byte, size*len(v)), here are some examples:
    https://github.com/ClickHouse/ch-go/blob/main/proto/col_decimal64_unsafe_gen.go#L31-L45
    https://github.com/ClickHouse/ch-go/blob/main/proto/col_int64_unsafe_gen.go#L31-L45

Hey, compiler should optimize w.Data = append(w.Data[:0], make([]byte, maxSize+headerSize)...) anyway in the way that you are proposing.

cool, thanks for answer.