Skip to content

Commit

Permalink
perf(proto): improve ColStr writing performance for small strings
Browse files Browse the repository at this point in the history
```
goos: linux
goarch: amd64
pkg: github.com/ClickHouse/ch-go
cpu: AMD Ryzen 9 5950X 16-Core Processor
                                  │    old.txt    │                new.txt                 │
                                  │    sec/op     │    sec/op      vs base                 │
Insert/SmallColStr/Rows10000-32     2903.0µ ±  4%    634.0µ ±  8%   -78.16% (p=0.000 n=15)
Insert/SmallColStr/Rows100000-32    28.549m ±  2%    2.584m ± 13%   -90.95% (p=0.000 n=15)
Insert/SmallColStr/Rows1000000-32   172.39m ±  2%    27.35m ±  2%   -84.14% (p=0.000 n=15)
Insert/BigColStr/Rows10000-32        25.97m ± 17%    33.78m ± 10%   +30.07% (p=0.000 n=15)
Insert/BigColStr/Rows100000-32       375.0m ± 11%   1051.0m ± 41%  +180.22% (p=0.000 n=15)
geomean                              20.52m          13.16m         -35.87%

                                  │    old.txt     │                 new.txt                  │
                                  │      B/s       │      B/s        vs base                  │
Insert/SmallColStr/Rows10000-32      55.85Mi ±  4%   255.73Mi ±  8%   +357.91% (p=0.000 n=15)
Insert/SmallColStr/Rows100000-32     56.79Mi ±  2%   627.37Mi ± 12%  +1004.70% (p=0.000 n=15)
Insert/SmallColStr/Rows1000000-32    94.04Mi ±  2%   592.83Mi ±  2%   +530.39% (p=0.000 n=15)
Insert/BigColStr/Rows10000-32        1.469Gi ± 15%    1.130Gi ± 11%    -23.12% (p=0.000 n=15)
Insert/BigColStr/Rows100000-32      1042.1Mi ± 10%    371.9Mi ± 70%    -64.31% (p=0.000 n=15)
geomean                              407.7Mi          635.8Mi          +55.93%

                                  │     old.txt     │                  new.txt                  │
                                  │      B/op       │      B/op        vs base                  │
Insert/SmallColStr/Rows10000-32      2085.57Ki ± 0%     11.14Ki ±  0%    -99.47% (p=0.000 n=15)
Insert/SmallColStr/Rows100000-32    23133.30Ki ± 0%     28.04Ki ±  4%    -99.88% (p=0.000 n=15)
Insert/SmallColStr/Rows1000000-32    234.485Mi ± 2%     2.458Mi ±  7%    -98.95% (p=0.000 n=15)
Insert/BigColStr/Rows10000-32          2.041Mi ± 0%     5.743Mi ± 12%   +181.31% (p=0.000 n=15)
Insert/BigColStr/Rows100000-32         24.32Mi ± 6%   2409.10Mi ± 50%  +9804.48% (p=0.000 n=15)
geomean                                402.6Ki          135.7Ki          -66.31%

                                  │  old.txt   │               new.txt               │
                                  │ allocs/op  │ allocs/op   vs base                 │
Insert/SmallColStr/Rows10000-32     335.0 ± 0%   327.0 ± 0%  -2.39% (p=0.000 n=15)
Insert/SmallColStr/Rows100000-32    343.0 ± 0%   328.0 ± 0%  -4.37% (p=0.000 n=15)
Insert/SmallColStr/Rows1000000-32   353.0 ± 1%   329.0 ± 0%  -6.80% (p=0.000 n=15)
Insert/BigColStr/Rows10000-32       335.0 ± 0%   329.0 ± 0%  -1.79% (p=0.000 n=15)
Insert/BigColStr/Rows100000-32      354.0 ± 1%   381.0 ± 7%  +7.63% (p=0.009 n=15)
geomean                             336.2        333.3       -0.87%
```
  • Loading branch information
tdakkota committed Sep 26, 2024
1 parent c49cb3a commit 308ecb3
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions proto/col_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ func (c ColStr) EncodeColumn(b *Buffer) {
// WriteColumn writes String rows to *Writer.
func (c ColStr) WriteColumn(w *Writer) {
buf := make([]byte, binary.MaxVarintLen64)
for _, p := range c.Pos {
w.ChainBuffer(func(b *Buffer) {
// 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])
})
w.ChainWrite(c.Buf[p.Start:p.End])
}
b.PutRaw(c.Buf[p.Start:p.End])
}
})
}

// ForEach calls f on each string from column.
Expand Down

0 comments on commit 308ecb3

Please sign in to comment.