Skip to content

Optimize lazy tracing hot paths - #412

Merged
PhilippGrulich merged 15 commits into
mainfrom
Codex/optimize-lazy-tracing-260720
Jul 22, 2026
Merged

Optimize lazy tracing hot paths#412
PhilippGrulich merged 15 commits into
mainfrom
Codex/optimize-lazy-tracing-260720

Conversation

@PhilippGrulich

@PhilippGrulich PhilippGrulich commented Jul 21, 2026

Copy link
Copy Markdown
Member

Summary

  • store TraceState inline in the thread-local trace context, removing a heap allocation for each traced function
  • replace the duplicate global/local execution-trace tag maps with one pre-sized tag index
  • pre-size the symbolic tag map to reduce rehashing in branch-heavy traces
  • make the repository format target safe for long paths, whitespace-containing paths, and binary fuzz seeds

Motivation

Lazy tracing repeatedly constructs trace state and grows tag indexes while exploring control-flow paths. The old localTagMap duplicated every insertion and relocation already written to globalTagMap; checkTag always searched the global map first, and reset merged keys that already existed globally before clearing the duplicates. Removing that redundant map avoids its bucket allocation, duplicate writes, lookups, merge, and clear without changing trace semantics.

The formatting changes fix issues encountered while applying the repository's required format target on macOS: BSD xargs -I exceeded its replacement limit for long tracked paths, and newline normalization treated fuzz .bin seeds as text.

Performance

Verification used immutable baseline executables and nine alternating baseline/candidate rounds with ten Catch2 samples per workload.

Single-map change versus the previous PR revision

  • geometric-mean latency ratio: 0.900809814 (9.92% faster)
  • paired-bootstrap 95% upper bound: 0.908716428
  • all 14 completing_trace_* workloads improved
  • worst workload ratio: 0.958599

Complete PR versus the original baseline

  • geometric-mean latency ratio: 0.813920981 (18.61% faster)
  • paired-bootstrap 95% upper bound: 0.823412571
  • all 14 workloads improved
  • worst workload ratio: 0.941295 (still 5.87% faster)
  • per-workload regression guard: no ratio above 1.10

Validation

  • Release build with Clang 21
  • full configured CTest suite: 368/368 passed
  • focused tracing/regression suite covering both trace modes: 15/15 passed
  • benchmark harness unit tests: 5/5 passed
  • both ./format.sh -i and ./format.sh pass with Clang 21
  • binary fuzz seeds remain byte-identical after formatting
  • independent review confirmed the one-map invariant across every write, relocation, lookup, and reset path and assessed the change ready to push

@PhilippGrulich
PhilippGrulich marked this pull request as ready for review July 21, 2026 21:38

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracing Benchmark

Details
Benchmark suite Current: f11412f Previous: 56621ea Ratio
ssa_staticSquareSum1000 40.2482 us (± 3.17555) 45.3994 us (± 2.20425) 0.89
ssa_staticSquareSum2000 82.2459 us (± 6.89276) 87.8591 us (± 3.13901) 0.94
ssa_staticSquareSum4000 168.693 us (± 7.71027) 177.67 us (± 8.95293) 0.95
comp_mlir_add 6.3199 ms (± 194.915) 6.19956 ms (± 167.627) 1.02
comp_mlir_ifThenElse 6.94807 ms (± 225.453) 6.79538 ms (± 163.325) 1.02
comp_mlir_deeplyNestedIfElse 5.78251 ms (± 191.626) 5.65793 ms (± 151.854) 1.02
comp_mlir_loop 7.95538 ms (± 218.294) 7.84635 ms (± 191.516) 1.01
comp_mlir_ifInsideLoop 31.4954 ms (± 705.15) 30.4944 ms (± 486.6) 1.03
comp_mlir_loopDirectCall 13.2279 ms (± 428.501) 12.6477 ms (± 215.396) 1.05
comp_mlir_pointerLoop 30.3744 ms (± 689.815) 29.7151 ms (± 230.526) 1.02
comp_mlir_staticLoop 5.57431 ms (± 151.292) 5.59688 ms (± 146.324) 1.00
comp_mlir_fibonacci 11.3038 ms (± 172.427) 11.3005 ms (± 190.458) 1.00
comp_mlir_gcd 10.0977 ms (± 185.044) 10.086 ms (± 184.161) 1.00
comp_mlir_nestedIf10 11.6615 ms (± 354.761) 11.2114 ms (± 177.032) 1.04
comp_mlir_nestedIf100 26.6811 ms (± 490.269) 26.4466 ms (± 259.973) 1.01
comp_mlir_chainedIf10 10.8714 ms (± 253.316) 10.505 ms (± 191.054) 1.03
comp_mlir_chainedIf100 22.7715 ms (± 351.807) 22.0129 ms (± 243.781) 1.03
exec_mlir_add 10.048 ns (± 1.80348) 10.2708 ns (± 1.56311) 0.98
exec_mlir_fibonacci 16.0541 us (± 1.7218) 15.7284 us (± 2.04692) 1.02
exec_mlir_sum 553.088 us (± 63.2978) 280.214 us (± 154.165) 1.97
exec_cpp_add 4.48732 ns (± 0.500988) 4.8673 ns (± 1.10318) 0.92
exec_cpp_fibonacci 55.2467 us (± 5.92851) 55.2635 us (± 6.81187) 1.00
exec_cpp_sum 12.4173 ms (± 104.09) 12.1337 ms (± 76.9327) 1.02
exec_bc_add 46.6366 ns (± 6.70123) 45.4632 ns (± 4.81197) 1.03
exec_bc_fibonacci 385.231 us (± 47.7491) 393.426 us (± 37.808) 0.98
exec_bc_sum 87.0064 ms (± 2.75768) 91.1865 ms (± 1.74301) 0.95
exec_tbc_add 30.3219 ns (± 1.51578) 31.7338 ns (± 2.55949) 0.96
exec_tbc_fibonacci 152.217 us (± 6.25075) 150.697 us (± 3.99896) 1.01
exec_tbc_sum 40.0276 ms (± 255.067) 39.9842 ms (± 80.4115) 1.00
exec_asmjit_add 3.54058 ns (± 0.323397) 3.60684 ns (± 0.433125) 0.98
exec_asmjit_fibonacci 18.4345 us (± 234.437) 21.2151 us (± 1.69519) 0.87
exec_asmjit_sum 3.57451 ms (± 302.173) 3.71283 ms (± 348.108) 0.96
exec_bc_add_passesOff 47.2645 ns (± 6.44231) 45.6957 ns (± 4.36992) 1.03
exec_bc_add_passesOn 47.7113 ns (± 7.98822) 45.7336 ns (± 3.93035) 1.04
exec_bc_fibonacci_passesOff 388.219 us (± 36.1129) 392.456 us (± 20.6619) 0.99
exec_bc_fibonacci_passesOn 372.05 us (± 16.1325) 385.408 us (± 17.7055) 0.97
exec_bc_sum_passesOff 87.205 ms (± 3.60775) 91.0237 ms (± 495.161) 0.96
exec_bc_sum_passesOn 77.383 ms (± 1.68545) 76.9524 ms (± 2.07238) 1.01
exec_tbc_add_passesOff 30.3417 ns (± 1.61837) 32.3244 ns (± 2.93185) 0.94
exec_tbc_add_passesOn 30.3689 ns (± 1.56426) 32.1356 ns (± 2.95043) 0.95
exec_tbc_fibonacci_passesOff 152.56 us (± 4.95323) 151.955 us (± 4.99842) 1.00
exec_tbc_fibonacci_passesOn 151.302 us (± 4.29466) 151.643 us (± 4.8017) 1.00
exec_tbc_sum_passesOff 39.9385 ms (± 102.17) 40.0081 ms (± 92.9324) 1.00
exec_tbc_sum_passesOn 37.8539 ms (± 104.797) 37.9037 ms (± 101.233) 1.00
exec_asmjit_add_passesOff 3.56694 ns (± 0.349872) 3.55016 ns (± 0.264041) 1.00
exec_asmjit_add_passesOn 3.58794 ns (± 0.224977) 3.65196 ns (± 0.610753) 0.98
exec_asmjit_fibonacci_passesOff 18.9381 us (± 2.46813) 21.7412 us (± 2.95025) 0.87
exec_asmjit_fibonacci_passesOn 18.5706 us (± 1.0105) 21.4589 us (± 2.29896) 0.87
exec_asmjit_sum_passesOff 3.54077 ms (± 287.55) 3.70217 ms (± 341.653) 0.96
exec_asmjit_sum_passesOn 3.40326 ms (± 125.29) 3.6433 ms (± 331.324) 0.93
exec_bc_add_noRegAlloc 46.9283 ns (± 6.4915) 45.2427 ns (± 3.23317) 1.04
exec_bc_add_regAlloc 46.5953 ns (± 5.67839) 45.1945 ns (± 3.73051) 1.03
exec_bc_fibonacci_noRegAlloc 406.624 us (± 23.3211) 411.872 us (± 16.252) 0.99
exec_bc_fibonacci_regAlloc 386.269 us (± 28.1983) 390.533 us (± 18.6484) 0.99
exec_bc_sum_noRegAlloc 86.9803 ms (± 325.451) 93.8794 ms (± 2.93793) 0.93
exec_bc_sum_regAlloc 87.5113 ms (± 4.89772) 91.1423 ms (± 808.849) 0.96
exec_bc_add_call 46.8787 ns (± 6.46076) 45.8183 ns (± 4.13466) 1.02
exec_bc_add_switch 45.257 ns (± 5.24967) 44.9966 ns (± 3.48139) 1.01
exec_bc_add_threaded 40.9141 ns (± 4.01299) 39.9647 ns (± 2.80649) 1.02
exec_bc_fibonacci_call 386.095 us (± 28.0243) 393.693 us (± 35.8617) 0.98
exec_bc_fibonacci_switch 325.844 us (± 16.2564) 358.924 us (± 7.95108) 0.91
exec_bc_fibonacci_threaded 479.732 us (± 12.4114) 480.489 us (± 13.2202) 1.00
exec_bc_sum_call 87.3395 ms (± 3.33029) 91.778 ms (± 6.54825) 0.95
exec_bc_sum_switch 83.0902 ms (± 5.63448) 90.6843 ms (± 1.49754) 0.92
exec_bc_sum_threaded 97.1992 ms (± 13.0726) 95.9613 ms (± 1.02184) 1.01
exec_bc_add_threaded_noReuse 39.9845 ns (± 2.73061) 40.7231 ns (± 3.78912) 0.98
exec_bc_add_threaded_reuse 33.6208 ns (± 4.07457) 34.0004 ns (± 4.97246) 0.99
exec_bc_fibonacci_threaded_noReuse 478.628 us (± 12.1341) 478.859 us (± 12.2946) 1.00
exec_bc_fibonacci_threaded_reuse 482.785 us (± 13.2849) 482.025 us (± 13.8314) 1.00
exec_bc_sum_threaded_noReuse 96.6143 ms (± 10.1054) 96.054 ms (± 1.2543) 1.01
exec_bc_sum_threaded_reuse 95.0857 ms (± 637.703) 95.7943 ms (± 347.77) 0.99
exec_bc_add_threaded_noSuperinstr 39.8583 ns (± 3.42471) 41.0476 ns (± 4.99908) 0.97
exec_bc_add_threaded_superinstr 40.2192 ns (± 3.05885) 40.4545 ns (± 4.36529) 0.99
ssa_add 148.807 ns (± 16.5232) 147.705 ns (± 7.34436) 1.01
ssa_ifThenElse 1000.8300000000002 ns (± 189445) 963.841 ns (± 70.5516) 1.04
ssa_deeplyNestedIfElse 2.53131 us (± 392.781) 2.50651 us (± 311.987) 1.01
ssa_loop 1251.3700000000001 ns (± 124947) 1239.3700000000001 ns (± 153839) 1.01
ssa_ifInsideLoop 2625.1000000000004 ns (± 315527) 2590.11 ns (± 335074) 1.01
ssa_loopDirectCall 1485.33 ns (± 411449) 1400.1200000000001 ns (± 130625) 1.06
ssa_pointerLoop 1640.56 ns (± 192559) 1683.59 ns (± 191280) 0.97
ssa_staticLoop 420.647 ns (± 43.7402) 456.629 ns (± 73.719) 0.92
ssa_fibonacci 1453.14 ns (± 130389.99999999999) 1431.94 ns (± 128765.99999999999) 1.01
ssa_gcd 1247.8 ns (± 105385) 1274.5 ns (± 213539) 0.98
ssa_nestedIf10 6.39154 us (± 440.159) 6.55705 us (± 594.229) 0.97
ssa_nestedIf100 103.602 us (± 4.87201) 109.643 us (± 3.5851) 0.94
ssa_chainedIf10 10.3197 us (± 903.574) 10.702 us (± 1.17742) 0.96
ssa_chainedIf100 172.516 us (± 5.44184) 177.12 us (± 5.47727) 0.97
ssa_liveIn16 1.87107 us (± 154.483) 1.85093 us (± 138.281) 1.01
ssa_liveIn64 7.34871 us (± 427.307) 7.27503 us (± 640.725) 1.01
ssa_liveIn256 27.7277 us (± 1.96194) 27.5694 us (± 1.46746) 1.01
exec_bc_addOne 38.156 ns (± 2.29262) 37.7297 ns (± 2.45605) 1.01
exec_mlir_addOne 291.554 ns (± 8.43406) 304.582 ns (± 7.63068) 0.96
exec_cpp_addOne 3.74628 ns (± 0.160706) 4.12557 ns (± 0.481633) 0.91
exec_interpreted_addOne 39.1 ns (± 1.74662) 39.0968 ns (± 1.77749) 1.00
e2e_tiered_bc_to_mlir 3820.78 us (± 137860) 3715.14 us (± 109608) 1.03
e2e_single_mlir 6.46967 ms (± 179.846) 6.1608 ms (± 163.196) 1.05
tiered_twotier_addOne 3.80184 ms (± 121.55) 3.72202 ms (± 112.187) 1.02
tiered_singletier_addOne 3.64001 ms (± 124.763) 3.57987 ms (± 109.612) 1.02
single_compile_mlir_addOne 3.64627 ms (± 120.951) 3.58775 ms (± 109.484) 1.02
single_compile_cpp_addOne 28.405 ms (± 291.433) 26.4949 ms (± 235.91) 1.07
single_compile_bc_addOne 61.3081 us (± 18.4043) 61.5553 us (± 19.753) 1.00
tiered_twotier_sumLoop 6.15935 ms (± 202.305) 5.85687 ms (± 146.446) 1.05
tiered_singletier_sumLoop 5.96139 ms (± 213.786) 5.7162 ms (± 141.496) 1.04
single_compile_mlir_sumLoop 5.92299 ms (± 194.258) 5.72675 ms (± 131.862) 1.03
single_compile_cpp_sumLoop 28.8351 ms (± 357.664) 27.1571 ms (± 207.034) 1.06
single_compile_bc_sumLoop 114.426 us (± 21.8649) 117.52 us (± 22.2444) 0.97
ir_add 737.394 ns (± 40.4162) 746.807 ns (± 70.4252) 0.99
ir_ifThenElse 1.55149 us (± 161.594) 1.51994 us (± 86.3702) 1.02
ir_deeplyNestedIfElse 3.38426 us (± 184.469) 3.37536 us (± 230.542) 1.00
ir_loop 1.63866 us (± 101.279) 1.60396 us (± 104.388) 1.02
ir_ifInsideLoop 2.8442 us (± 228.294) 2.7799 us (± 169.9) 1.02
ir_loopDirectCall 1.78949 us (± 107.447) 1.7505 us (± 97.9929) 1.02
ir_pointerLoop 1.97242 us (± 101.758) 1.94324 us (± 136.883) 1.02
ir_staticLoop 1.46869 us (± 90.3858) 1.43205 us (± 77.8518) 1.03
ir_fibonacci 1.7135 us (± 83.7978) 1.71727 us (± 89.4881) 1.00
ir_gcd 1.45252 us (± 84.5277) 1.43973 us (± 79.719) 1.01
ir_nestedIf10 7.80597 us (± 507.525) 7.76727 us (± 476.311) 1.00
ir_nestedIf100 89.3882 us (± 5.76029) 92.9779 us (± 3.88496) 0.96
ir_chainedIf10 12.3283 us (± 931.151) 11.6789 us (± 806.906) 1.06
ir_chainedIf100 152.049 us (± 12.8523) 150.497 us (± 6.06715) 1.01
ssa_module_add 186.139 ns (± 11.2531) 190.706 ns (± 9.07784) 0.98
ssa_module_ifThenElse 1082.49 ns (± 294687) 1038.88 ns (± 101106) 1.04
ssa_module_deeplyNestedIfElse 2.59516 us (± 405.874) 2.59697 us (± 334.423) 1.00
ssa_module_loop 1307.03 ns (± 153234) 1281.73 ns (± 159256) 1.02
ssa_module_ifInsideLoop 2.71806 us (± 403.694) 2.58529 us (± 284.803) 1.05
ssa_module_loopDirectCall 1.4756 us (± 176.656) 1.44045 us (± 145.353) 1.02
ssa_module_pointerLoop 1.70842 us (± 197.671) 1.74605 us (± 210.222) 0.98
ssa_module_staticLoop 463.101 ns (± 45.8009) 489.867 ns (± 39.7839) 0.95
ssa_module_fibonacci 1.51699 us (± 180.977) 1.70543 us (± 1.29394) 0.89
ssa_module_gcd 1297.16 ns (± 118305) 1336.75 ns (± 265525) 0.97
ssa_module_nestedIf10 6.67512 us (± 806.127) 6.51021 us (± 876.528) 1.03
ssa_module_nestedIf100 105.024 us (± 11.5013) 120.88 us (± 33.4545) 0.87
ssa_module_chainedIf10 10.45 us (± 844.095) 10.6575 us (± 1.24179) 0.98
ssa_module_chainedIf100 171.637 us (± 4.61612) 178.545 us (± 8.52751) 0.96
trace_add 2.86554 us (± 269.902) 2.43649 us (± 297.507) 1.18
completing_trace_add 2.92023 us (± 326.014) 2.43354 us (± 230.354) 1.20
trace_ifThenElse 9.67265 us (± 1.65316) 8.92374 us (± 1.38978) 1.08
completing_trace_ifThenElse 5.60141 us (± 796.136) 4.69761 us (± 570.606) 1.19
trace_deeplyNestedIfElse 28.4628 us (± 3.83849) 26.3364 us (± 3.91513) 1.08
completing_trace_deeplyNestedIfElse 13.161 us (± 1.84537) 13.0312 us (± 2.03481) 1.01
trace_loop 9.49855 us (± 1.33939) 8.93034 us (± 1.5409) 1.06
completing_trace_loop 5.60315 us (± 676.459) 4.82915 us (± 595.832) 1.16
trace_ifInsideLoop 17.9363 us (± 3.44022) 17.5983 us (± 3.82998) 1.02
completing_trace_ifInsideLoop 9.79487 us (± 1.29935) 8.65536 us (± 1.25838) 1.13
trace_loopDirectCall 10.008 us (± 1.91482) 9.0726 us (± 1.63163) 1.10
completing_trace_loopDirectCall 5.69431 us (± 833.473) 4.92844 us (± 600.09) 1.16
trace_pointerLoop 16.516 us (± 3.67735) 14.5722 us (± 2.6676) 1.13
completing_trace_pointerLoop 12.4547 us (± 2.16965) 10.2839 us (± 1.36936) 1.21
trace_staticLoop 6.72525 us (± 684.735) 8.04371 us (± 1.0152) 0.84
completing_trace_staticLoop 6.71219 us (± 749.926) 8.05164 us (± 1.02337) 0.83
trace_fibonacci 11.0548 us (± 2.18922) 10.4753 us (± 1.59802) 1.06
completing_trace_fibonacci 6.85477 us (± 970.591) 6.21133 us (± 708.511) 1.10
trace_gcd 8.97738 us (± 1.61893) 8.05557 us (± 1.3856) 1.11
completing_trace_gcd 4.84623 us (± 566.774) 4.08434 us (± 563.625) 1.19
trace_nestedIf10 52.4095 us (± 6.63453) 46.4515 us (± 7.9215) 1.13
completing_trace_nestedIf10 52.9783 us (± 6.87533) 45.9339 us (± 8.09505) 1.15
trace_nestedIf100 1.8913 ms (± 28.597) 1.99993 ms (± 38.3385) 0.95
completing_trace_nestedIf100 1.8955 ms (± 39.3047) 2.00139 ms (± 38.1537) 0.95
trace_chainedIf10 110.197 us (± 9.99354) 104.26 us (± 11.558) 1.06
completing_trace_chainedIf10 56.3626 us (± 6.72066) 51.4657 us (± 8.0369) 1.10
trace_chainedIf100 5.16209 ms (± 100.688) 5.21589 ms (± 49.247) 0.99
completing_trace_chainedIf100 2.20069 ms (± 27.5422) 2.37 ms (± 75.7961) 0.93

This comment was automatically generated by workflow using github-action-benchmark.

@PhilippGrulich
PhilippGrulich force-pushed the Codex/optimize-lazy-tracing-260720 branch from df49d33 to f11412f Compare July 21, 2026 22:10
@PhilippGrulich
PhilippGrulich merged commit fc55fb0 into main Jul 22, 2026
18 of 19 checks passed
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.

1 participant