Skip to content

Commit

Permalink
main: Fix buffer reuse read benchmark
Browse files Browse the repository at this point in the history
Previously, this benchmark was using the Reset() method to reset the
message's arena, which can't be done for reading, as the recently
introduced comments indicate.

This fixes the benchmark by releasing the previous arena and directly
modifying the arena field of the message.

In the future, this should be done explicitly by either Reset() or by a
new dedicated function.
  • Loading branch information
matheusd committed Jul 31, 2024
1 parent 7beaec0 commit ab9d4d0
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1763,28 +1763,37 @@ func BenchmarkUnmarshal(b *testing.B) {
}

func BenchmarkUnmarshal_Reuse(b *testing.B) {
type testCase struct {
a A
data []byte
}
r := rand.New(rand.NewSource(12345))
data := make([][]byte, 1000)
data := make([]testCase, 1000)
for i := range data {
a := generateA(r)
msg, seg, _ := capnp.NewMessage(capnp.SingleSegment(nil))
root, _ := air.NewRootBenchmarkA(seg)
a.fill(root)
data[i], _ = msg.Marshal()
buf, err := msg.Marshal()
if err != nil {
b.Fatal(err)
}
data[i].data, data[i].a = buf, *a
}
msg := new(capnp.Message)
ta := new(testArena)
arena := capnp.Arena(ta)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
*ta = testArena(data[r.Intn(len(data))][8:])
_, err := msg.Reset(arena)
if err != nil {
b.Fatal(err)
}
testIdx := r.Intn(len(data))
*ta = testArena(data[testIdx].data[8:])
msg.Release()
msg.Arena = ta
a, _ := air.ReadRootBenchmarkA(msg)
unmarshalA(a)
gotA := unmarshalA(a)
if gotA != data[testIdx].a {
b.Fatal("unexpected unmarshalled data")
}
}
}

Expand Down

0 comments on commit ab9d4d0

Please sign in to comment.