Avoid byte by byte LLVM symbolization when reconstructing inline frames
Problem
The experimental trace writer currently appears to perform LLVM inline-frame symbolization for every byte between the previous instruction location and the source instruction location of a trace event.
This happens in Trace_segment.add_event when the previous and current locations belong to the same DSO.
The current implementation walks the address range one byte at a time and calls symbolize_inlined_frames for every address.
There is already a comment beside this loop acknowledging that symbolizing every byte is likely to be very expensive.
I think this is worth addressing as a focused performance improvement, particularly because inline-frame reconstruction is one of the important additions of the new trace writer.
Relevant implementation
The main code path is in:
src/trace_segment.ml, insideadd_eventsrc/symbolizer.ml, which maintains the address-based symbolization cachesrc/llvm_symbolizer_stubs.cpp, which invokes LLVM'ssymbolizeInlinedCode
Relevant source:
- https://github.com/janestreet/magic-trace/blob/master/src/trace_segment.ml#L912-L948
- https://github.com/janestreet/magic-trace/blob/master/src/symbolizer.ml
- https://github.com/janestreet/magic-trace/blob/master/src/llvm_symbolizer_stubs.cpp#L34-L50
Current behavior
For a trace event, the implementation:
- Retrieves the previously known instruction address.
- Retrieves the current branch source address.
- Iterates through the entire address interval one byte at a time.
- Requests inline-frame symbolization for every address.
- Compares the returned inline stack against the previous inline stack.
- Emits call-stack transitions when the inline stack changes.
The exact-address cache helps avoid repeating the same lookup for an address that has already been seen. However, it does not avoid the initial symbolization of every distinct address.
Consequently, the amount of lookup work can grow with the number of bytes traversed, even when the inline-frame state remains unchanged across most of that interval.
The cache also retains entries for individual queried addresses rather than representing a whole address range with a shared result.
Why this matters
This approach may add significant work to trace decoding:
- Repeated LLVM symbolizer requests for addresses that belong to the same inline scope.
- Additional hash-table lookups and cache entries.
- Potentially unnecessary memory growth as more distinct instruction addresses are encountered.
- Additional processing overhead on traces containing large amounts of straight-line execution.
The merged experimental trace-writer work already documents a substantial performance gap compared with the previous writer. This issue is intended to isolate one concrete mechanism that may contribute to that cost, rather than repeat the broader performance concern.
I have not independently benchmarked this path yet, so the actual impact should be established through measurements.
Expected outcome
It would be useful to investigate a more efficient way to discover inline-frame transitions without symbolizing every intermediate byte.
The desired behavior is to preserve the exact inline call-stack reconstruction while reducing symbolization work to the relevant inline-scope transitions or address ranges.
One possible direction is to investigate range-based inline-scope lookup and caching rather than relying entirely on exact-address lookups.
DWARF inline-subroutine entries can describe their machine-code coverage through DW_AT_low_pc / DW_AT_high_pc or DW_AT_ranges. The latter is particularly important because inline instances can have non-contiguous ranges.
The implementation should reuse existing LLVM functionality where possible rather than introducing a separate DWARF parser without first establishing that it is necessary.
Implementation considerations
A possible implementation approach would be:
- Establish a benchmark for the current byte-by-byte lookup path.
- Investigate whether inline-scope ranges can be obtained or cached efficiently for a given executable or DSO.
- Introduce a lookup abstraction that can determine the inline stack at relevant addresses or ranges.
- Detect changes in inline-frame state without requesting symbolization for every intermediate byte.
- Preserve the existing call-stack transition semantics in
diff_inlined_frames'. - Retain a safe fallback for missing or unsupported debug information.
- Add regression tests for inline transitions and non-contiguous address ranges.
The implementation should account for nested inline functions, repeated symbols, DSO changes, relocation, and missing symbolization results.
I would avoid simply resolving the first and last addresses of an interval, since that could miss an inline function that begins and ends between those two addresses.
Validation
It would be useful to compare the existing and optimized implementations on deterministic workloads containing:
- Nested
always_inlinefunctions. - Multiple inline transitions within a single straight-line execution region.
- Large straight-line address intervals.
- Functions with no available inline debug information.
- Multiple or non-contiguous inline ranges.
The benchmark should report:
- Number of LLVM symbolizer calls.
- Number of distinct address-cache entries.
- Trace decoding wall time.
- Peak resident memory.
- Number and ordering of reconstructed inline frames.
The generated traces should also be compared to ensure that reducing symbolization work does not change the reconstructed call-stack semantics.
Acceptance criteria
- Inline-frame transitions remain correctly reconstructed.
- The implementation avoids symbolizing every intermediate byte.
- Lookup work scales with relevant inline-scope information rather than raw address distance.
- Non-contiguous inline ranges are handled correctly.
- Missing debug information does not introduce unnecessary repeated symbolization work.
- Existing trace-segment tests continue to pass.
- New regression tests cover inline entry, exit, nesting, and multiple ranges.
- Before-and-after benchmarks demonstrate the actual effect on decoding time and memory use.
Open design question
Is there an existing LLVM or internal debug-information abstraction that could expose the relevant inline address ranges efficiently?
I would be interested in the maintainers' preferred approach before introducing a new range-indexing abstraction.
The goal is to make inline-frame reconstruction substantially more efficient without compromising the fidelity that motivated the new trace writer in the first place.