Skip to content

Commit 75b858b

Browse files
committed
Use zero-copy decoding for reflists
Reflists are basically stored as arrays of strings, which are quite space-efficient in MessagePack. Thus, using zero-copy decoding results in nice performance and memory savings, because the overhead of separate allocations ends up far exceeding the overhead of the original slice. With the included benchmark run for 20s with -benchmem, the runtime, memory usage, and allocations go from ~740us/op, ~192KiB/op, and 4100 allocs/op to ~240us/op, ~97KiB/op, and 13 allocs/op, respectively. Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>
1 parent 5136031 commit 75b858b

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

deb/reflist.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ func (l *PackageRefList) Encode() []byte {
7171

7272
// Decode decodes msgpack representation into PackageRefLit
7373
func (l *PackageRefList) Decode(input []byte) error {
74-
decoder := codec.NewDecoderBytes(input, &codec.MsgpackHandle{})
74+
handle := &codec.MsgpackHandle{}
75+
handle.ZeroCopy = true
76+
decoder := codec.NewDecoderBytes(input, handle)
7577
return decoder.Decode(l)
7678
}
7779

deb/reflist_bench_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,20 @@ func BenchmarkReflistSimpleMerge(b *testing.B) {
2828
l.Merge(r, false, true)
2929
}
3030
}
31+
32+
func BenchmarkReflistDecode(b *testing.B) {
33+
const count = 4096
34+
35+
r := NewPackageRefList()
36+
for i := 0; i < count; i++ {
37+
r.Refs = append(r.Refs, []byte(fmt.Sprintf("Pamd64 pkg%d %d", i, i)))
38+
}
39+
40+
sort.Sort(r)
41+
data := r.Encode()
42+
43+
b.ResetTimer()
44+
for i := 0; i < b.N; i++ {
45+
(&PackageRefList{}).Decode(data)
46+
}
47+
}

0 commit comments

Comments
 (0)