check slice capacity to avoid unnecessary memory allocation
qixiaogang opened this issue · 2 comments
qixiaogang commented
- 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 create
make([]byte, maxSize+headerSize)
whencap(w.Data) >= maxSize+headerSize
- 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
ernado commented
Hey, compiler should optimize w.Data = append(w.Data[:0], make([]byte, maxSize+headerSize)...)
anyway in the way that you are proposing.
qixiaogang commented
cool, thanks for answer.