Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(col_str): reduce max memory usage #437

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions proto/col_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ColStr struct {

// Append string to column.
func (c *ColStr) Append(v string) {
c.Buf = binary.AppendUvarint(c.Buf, uint64(len(v)))
start := len(c.Buf)
c.Buf = append(c.Buf, v...)
end := len(c.Buf)
Expand All @@ -29,6 +30,7 @@ func (c *ColStr) Append(v string) {

// AppendBytes append byte slice as string to column.
func (c *ColStr) AppendBytes(v []byte) {
c.Buf = binary.AppendUvarint(c.Buf, uint64(len(v)))
start := len(c.Buf)
c.Buf = append(c.Buf, v...)
end := len(c.Buf)
Expand Down Expand Up @@ -68,26 +70,12 @@ func (c *ColStr) Reset() {

// EncodeColumn encodes String rows to *Buffer.
func (c ColStr) EncodeColumn(b *Buffer) {
buf := make([]byte, binary.MaxVarintLen64)
for _, p := range c.Pos {
n := binary.PutUvarint(buf, uint64(p.End-p.Start))
b.Buf = append(b.Buf, buf[:n]...)
b.Buf = append(b.Buf, c.Buf[p.Start:p.End]...)
}
b.Buf = append(b.Buf, c.Buf...)
}

// WriteColumn writes String rows to *Writer.
func (c ColStr) WriteColumn(w *Writer) {
buf := make([]byte, binary.MaxVarintLen64)
// Writing values from c.Buf directly might improve performance if [ColStr] contains a few rows of very long strings.
// However, most of the time it is quite opposite, so we copy data.
w.ChainBuffer(func(b *Buffer) {
for _, p := range c.Pos {
n := binary.PutUvarint(buf, uint64(p.End-p.Start))
b.PutRaw(buf[:n])
b.PutRaw(c.Buf[p.Start:p.End])
}
})
w.ChainWrite(c.Buf)
}

// ForEach calls f on each string from column.
Expand Down Expand Up @@ -139,6 +127,11 @@ func (c *ColStr) DecodeColumn(r *Reader, rows int) error {
return errors.Wrapf(err, "row %d: read length", i)
}

if len(c.Buf)-p.End < binary.MaxVarintLen64 {
zdyj3170101136 marked this conversation as resolved.
Show resolved Hide resolved
c.Buf = append(c.Buf, make([]byte, binary.MaxVarintLen64)...)
}
p.End += binary.PutUvarint(c.Buf[p.End:], uint64(n))

p.Start = p.End
p.End += n

Expand Down
Loading