Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btf: optimize stringTable to speed up vmlinux parsing #1203

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions btf/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (st *stringTable) Lookup(offset uint32) (string, error) {
}

func (st *stringTable) lookup(offset uint32) (string, error) {
if offset == 0 && st.base == nil {
return "", nil
}

i, found := slices.BinarySearch(st.offsets, offset)
if !found {
return "", fmt.Errorf("offset %d isn't start of a string", offset)
Expand All @@ -92,20 +96,6 @@ func (st *stringTable) lookup(offset uint32) (string, error) {
return st.strings[i], nil
}

func (st *stringTable) Marshal(w io.Writer) error {
for _, str := range st.strings {
_, err := io.WriteString(w, str)
if err != nil {
return err
}
_, err = w.Write([]byte{0})
if err != nil {
return err
}
}
return nil
}

// Num returns the number of strings in the table.
func (st *stringTable) Num() int {
return len(st.strings)
Expand Down
24 changes: 15 additions & 9 deletions btf/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ func TestStringTable(t *testing.T) {
t.Fatal(err)
}

var buf bytes.Buffer
if err := st.Marshal(&buf); err != nil {
t.Fatal("Can't marshal string table:", err)
}

if !bytes.Equal([]byte(in), buf.Bytes()) {
t.Error("String table doesn't match input")
}

// Parse string table of split BTF
split, err := readStringTable(strings.NewReader(splitIn), st)
if err != nil {
Expand Down Expand Up @@ -97,6 +88,21 @@ func TestStringTableBuilder(t *testing.T) {
qt.Assert(t, err, qt.IsNil, qt.Commentf("Can't parse string table"))
}

func BenchmarkStringTableZeroLookup(b *testing.B) {
strings := vmlinuxTestdataSpec(b).strings

b.ResetTimer()
for i := 0; i < b.N; i++ {
s, err := strings.Lookup(0)
if err != nil {
b.Fatal(err)
}
if s != "" {
b.Fatal("0 is not the empty string")
}
}
}

func newStringTable(strings ...string) *stringTable {
offsets := make([]uint32, len(strings))

Expand Down