From e21c665e70ae91a5d7e6d3e401f7791856c0e5c3 Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Tue, 28 Jun 2022 17:09:19 +0200 Subject: [PATCH] text: optimize faceCache.hashGIDs Use binary.LittleEndian directly instead of going through the binary.Write indirection. This allows the following optimizations to occur: - We can reuse our own byte slice between iterations - We don't have to put g.ID in an interface value - h doesn't escape - PutUint32 gets inlined On top of that, the argument to maphash.Hash.Write doesn't escape, so b doesn't move to the heap. Signed-off-by: Dominik Honnef --- text/shaper.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/text/shaper.go b/text/shaper.go index 338d215e..942c8097 100644 --- a/text/shaper.go +++ b/text/shaper.go @@ -156,8 +156,10 @@ func (f *faceCache) hashGIDs(layout Layout) uint64 { } var h maphash.Hash h.SetSeed(f.seed) + var b [4]byte for _, g := range layout.Glyphs { - binary.Write(&h, binary.LittleEndian, g.ID) + binary.LittleEndian.PutUint32(b[:], uint32(g.ID)) + h.Write(b[:]) } return h.Sum64() }