Skip to content

perf(sdk-trace-base): optimize TraceIdRatioBasedSampler hex parsing#6284

Open
AbhiPrasad wants to merge 2 commits intoopen-telemetry:mainfrom
AbhiPrasad:perf/optimize-trace-id-ratio-sampler
Open

perf(sdk-trace-base): optimize TraceIdRatioBasedSampler hex parsing#6284
AbhiPrasad wants to merge 2 commits intoopen-telemetry:mainfrom
AbhiPrasad:perf/optimize-trace-id-ratio-sampler

Conversation

@AbhiPrasad
Copy link
Member

@AbhiPrasad AbhiPrasad commented Jan 12, 2026

Which problem is this PR solving?

Optimize the _accumulate method in TraceIdRatioBasedSampler by replacing parseInt() with string slicing with direct charCodeAt() hex parsing. This avoids creating temporary strings via slice() and the overhead of parseInt() for each of the 4 chunks of the trace ID. Improves sampling decision calls by 15%.

The optimization preserves exact behavioral compatibility - the same trace IDs will produce the same sampling decisions as before.

Type of change

Performance Improvement

How Has This Been Tested?

Add a new benchmark for this (AI generated). Tested on a 2021 Apple M1 Pro 32 GB - MacOS 15.5 (24F74)

Version ops/sec Improvement
Original (parseInt + slice) 2,742,516 -
Optimized (charCodeAt) 3,152,165 +15%

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@AbhiPrasad AbhiPrasad requested a review from a team as a code owner January 12, 2026 23:22
@codecov
Copy link

codecov bot commented Jan 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.68%. Comparing base (903739c) to head (c70e05a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6284   +/-   ##
=======================================
  Coverage   95.68%   95.68%           
=======================================
  Files         313      313           
  Lines        9667     9670    +3     
  Branches     2226     2227    +1     
=======================================
+ Hits         9250     9253    +3     
  Misses        417      417           
Files with missing lines Coverage Δ
...trace-base/src/sampler/TraceIdRatioBasedSampler.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 12, 2026

This is missing a changelog entry.

@AbhiPrasad
Copy link
Member Author

forgot the git push!

@david-luna
Copy link
Contributor

@AbhiPrasad there are some conflicts. Please fix them so we can move on this PR :)

Optimize the `_accumulate` method in TraceIdRatioBasedSampler by replacing
`parseInt()` with string slicing with direct `charCodeAt()` hex parsing.

This avoids creating temporary strings via `slice()` and the overhead of
`parseInt()` for each of the 4 chunks of the trace ID.

| Version | ops/sec | Improvement |
|---------|---------|-------------|
| Original (parseInt + slice) | 2,742,516 | baseline |
| Optimized (charCodeAt) | 3,152,165 | +15% |

The optimization preserves exact behavioral compatibility - the same trace IDs
will produce the same sampling decisions as before.

Also adds a benchmark for the sampler to track performance.
@AbhiPrasad AbhiPrasad force-pushed the perf/optimize-trace-id-ratio-sampler branch from c4141b6 to c70e05a Compare January 29, 2026 16:04
@AbhiPrasad
Copy link
Member Author

Rebased this branch and fixed the conflicts

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.

3 participants