Skip to content

Commit c03bd45

Browse files
committed
btf: reduce memory usage of namedTypes
Appending to a nil slice allocates a slice with an 8 item capacity as of Go 1.24. This is wasteful since most entries will only ever need a single ID. Do some syntax gymnastics to avoid this waste. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent 89844e9 commit c03bd45

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

btf/unmarshal.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,16 @@ func newDecoder(raw []byte, bo binary.ByteOrder, strings *stringTable, base *dec
100100
}
101101

102102
if name := newEssentialName(name); name != "" {
103-
namedTypes[name] = append(namedTypes[name], id)
103+
ids := namedTypes[name]
104+
if ids == nil {
105+
// Almost all names will only have a single name to them.
106+
// Explicitly allocate a slice of capacity 1 instead of relying
107+
// on append behaviour.
108+
ids = []TypeID{id}
109+
} else {
110+
ids = append(ids, id)
111+
}
112+
namedTypes[name] = ids
104113
}
105114

106115
id++

0 commit comments

Comments
 (0)