Skip to content

btf: parse BTF types in a single pass during decoder initialization - #2082

Closed
DARSHANR007 wants to merge 1 commit into
cilium:mainfrom
DARSHANR007:btf-single-pass-decoder
Closed

DARSHANR007 wants to merge 1 commit into
cilium:mainfrom
DARSHANR007:btf-single-pass-decoder

Conversation

@DARSHANR007

@DARSHANR007 DARSHANR007 commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2081

newDecoder() currently scans the raw BTF buffer twice: once to count the number of types and a second time to parse them into memory.

Since every BTF type header is at least 12 bytes (btfTypeSize), we can estimate the maximum possible number of types upfront (len(raw) / btfTypeSize) and parse all types in a single pass instead.

newDecoder currently scans the BTF buffer twice: once to count the number of types and a second time to parse them.

Estimate the maximum number of types upfront from the buffer length and parse all types in a single pass instead.

Signed-off-by: darsh <[email protected]>
@dylandreimerink

Copy link
Copy Markdown
Member

I must say I am not super convinced. I ran the benchmark myself as well, getting the following result:

goos: linux
goarch: amd64
pkg: github.com/cilium/ebpf/btf
cpu: 13th Gen Intel(R) Core(TM) i7-13800H
                │ before.txt  │             after.txt              │
                │   sec/op    │   sec/op     vs base               │
ParseVmlinux-20   7.659m ± 1%   7.328m ± 4%  -4.33% (p=0.024 n=20)

                │  before.txt  │               after.txt               │
                │     B/op     │     B/op      vs base                 │
ParseVmlinux-20   1.254Mi ± 0%   4.105Mi ± 0%  +227.33% (p=0.000 n=20)

                │ before.txt │             after.txt              │
                │ allocs/op  │ allocs/op   vs base                │
ParseVmlinux-20   246.0 ± 0%   282.0 ± 0%  +14.63% (p=0.000 n=20)

So we speed up 4% on average, in exchange for a 227% increase in temporary memory usage. Likely due to now having to resize the fuzzy string index and decl tags index multiple times.

This method also grossly overestimates the amount of offsets, leading to offsets slice capacity being ~50% larger than what is actually filled (on the vmlinux), which is about 900KiB which gets retained for as long as the btf.Spec stays alive, which can be long when a btf.Cache is used.

I don't think this tradeoff is worth.

@ti-mo

ti-mo commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Yep, quadrupling allocs for a low-single digit % gain in wall clock time is typically not what our users ask for. On the contrary, memory consumption gets much more heavily scrutinized as it sticks around for longer in caches and throughout GC cycles.

@ti-mo ti-mo closed this Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

btf: avoid scanning the BTF type buffer twice when building a decoder

3 participants