Skip to content

Commit 22ffff5

Browse files
paulcacheuxlmb
authored andcommitted
btf: introduce caching to the string table to speed up ext info loading
when parsing line infos, we end up querying multiple times the same offest (especially the file name), and we currently create a new string each time. This commit deduplicates this allocation by caching the string. Signed-off-by: Paul Cacheux <[email protected]>
1 parent dab673c commit 22ffff5

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

btf/ext_info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,12 @@ func LoadLineInfos(reader io.Reader, bo binary.ByteOrder, recordNum uint32, spec
559559
}
560560

561561
func newLineInfo(li bpfLineInfo, strings *stringTable) (LineOffset, error) {
562-
line, err := strings.Lookup(li.LineOff)
562+
line, err := strings.LookupCached(li.LineOff)
563563
if err != nil {
564564
return LineOffset{}, fmt.Errorf("lookup of line: %w", err)
565565
}
566566

567-
fileName, err := strings.Lookup(li.FileNameOff)
567+
fileName, err := strings.LookupCached(li.FileNameOff)
568568
if err != nil {
569569
return LineOffset{}, fmt.Errorf("lookup of filename: %w", err)
570570
}

btf/strings.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
type stringTable struct {
1313
base *stringTable
1414
bytes []byte
15+
16+
cache map[uint32]string
1517
}
1618

1719
// sizedReader is implemented by bytes.Reader, io.SectionReader, strings.Reader, etc.
@@ -88,6 +90,30 @@ func (st *stringTable) lookupSlow(offset uint32) ([]byte, error) {
8890
return st.bytes[offset : offset+uint32(i)], nil
8991
}
9092

93+
// LookupCache returns the string at the given offset, caching the result
94+
// for future lookups.
95+
func (cst *stringTable) LookupCached(offset uint32) (string, error) {
96+
// Fast path: zero offset is the empty string, looked up frequently.
97+
if offset == 0 {
98+
return "", nil
99+
}
100+
101+
if str, ok := cst.cache[offset]; ok {
102+
return str, nil
103+
}
104+
105+
str, err := cst.Lookup(offset)
106+
if err != nil {
107+
return "", err
108+
}
109+
110+
if cst.cache == nil {
111+
cst.cache = make(map[uint32]string)
112+
}
113+
cst.cache[offset] = str
114+
return str, nil
115+
}
116+
91117
// stringTableBuilder builds BTF string tables.
92118
type stringTableBuilder struct {
93119
length uint32

0 commit comments

Comments
 (0)