From c579349c05ba7e7fa1c8936cf94647e5771eed58 Mon Sep 17 00:00:00 2001 From: Laurence Withers Date: Sun, 18 Feb 2024 14:31:13 +0000 Subject: [PATCH] Pre-size net.Buffers This leads to a further reduction in allocations per call. Before: goos: linux goarch: amd64 pkg: src.lwithers.me.uk/go/journal cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz BenchmarkEntry-8 12611158 1445 ns/op 221 B/op 7 allocs/op PASS ok src.lwithers.me.uk/go/journal 19.673s After: goos: linux goarch: amd64 pkg: src.lwithers.me.uk/go/journal cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz BenchmarkEntry-8 13613377 1329 ns/op 149 B/op 5 allocs/op PASS ok src.lwithers.me.uk/go/journal 19.435s --- wire_proto.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wire_proto.go b/wire_proto.go index 7f1b41c..4f8d3ea 100644 --- a/wire_proto.go +++ b/wire_proto.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "io" "net" + "slices" ) var ( @@ -19,6 +20,7 @@ var ( // atomic, and this function is safe to use across goroutines. Otherwise, // writes may be interleaved. func WireWrite(buf *net.Buffers, w io.Writer, attrs []Attr) error { + *buf = slices.Grow(*buf, len(*buf)+len(attrs)*4) for i := range attrs { key := attrs[i].Key.key if key == "" {