Skip to content

Commit

Permalink
Allocate entries array with correct size while decoding WAL entries. (#…
Browse files Browse the repository at this point in the history
…3241)

Small optimization I found while deployment the new version.
We might want to have a pool of entries here.

```
❯ benchcmp  before.txt after.txt
benchmark                  old ns/op     new ns/op     delta
Benchmark_DecodeWAL-16     2350024       1591633       -32.27%

benchmark                  old allocs     new allocs     delta
Benchmark_DecodeWAL-16     20043          20005          -0.19%

benchmark                  old bytes     new bytes     delta
Benchmark_DecodeWAL-16     4946246       1763270       -64.35%
```

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena authored Jan 27, 2021
1 parent 4c6f8e8 commit 8716e24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/ingester/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func decodeEntries(b []byte, rec *WALRecord) error {
}

nEntries := dec.Uvarint()
refEntries.Entries = make([]logproto.Entry, 0, nEntries)
rem := nEntries
for ; dec.Err() == nil && rem > 0; rem-- {
timeOffset := dec.Varint64()
Expand Down
33 changes: 33 additions & 0 deletions pkg/ingester/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,39 @@ func Benchmark_EncodeEntries(b *testing.B) {
}
}

func Benchmark_DecodeWAL(b *testing.B) {
var entries []logproto.Entry
for i := int64(0); i < 10000; i++ {
entries = append(entries, logproto.Entry{
Timestamp: time.Unix(0, i),
Line: fmt.Sprintf("long line with a lot of data like a log %d", i),
})
}
record := &WALRecord{
entryIndexMap: make(map[uint64]int),
UserID: "123",
RefEntries: []RefEntries{
{
Ref: 456,
Entries: entries,
},
{
Ref: 789,
Entries: entries,
},
},
}

buf := record.encodeEntries(nil)
rec := recordPool.GetRecord()
b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
require.NoError(b, decodeWALRecord(buf, rec))
}
}

func fillChunk(t testing.TB, c chunkenc.Chunk) {
t.Helper()
var i int64
Expand Down

0 comments on commit 8716e24

Please sign in to comment.