perf(otlp-transformer): add a fast path for single-byte varints in ProtobufWriter#writeVarint #7387
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 387 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.357
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #7387"
general
weight: 0.35
legendecas
Which problem is this PR solving?
Refs #6100
ProtobufWriter#writeVarintis the most frequent call in protobuf serialization. Every field tag goes through it viawriteTag, as do lengths, enum values and counts. Each attribute alone writes four tags: theKeyValueentry, its key, its value and theAnyValuetype. Nearly all of these values are below0x80, so they encode as a single byte, but each one still goes throughestimateVarintSize(), the 32-bit range check and the varint loop.Short description of the changes
writeVarintwrites values from 0 to 127 as a single byte after_ensureCapacity(1). Everything else goes to the existing code, which is moved unchanged into a private_writeMultiByteVarint()method. Keeping that code in a separate method matters: when I added the fast path inline at the top ofwriteVarint, it made no measurable difference (+0.6% / −1.0% instructions per span), presumably because the larger method was no longer inlined.writeTagis unchanged and gets the fast path throughwriteVarint. Field numbers below 16 have a 1-byte tag. The bytes written are the same as before.I added tests that check the exact bytes
writeVarintwrites for 0, 1, 127, 128, 300, 2³² − 1 and −1, and thatwriteTagwrites for 1-, 2- and 3-byte tags, starting from both an empty and a pre-sized buffer. They pass onmainand on this branch.Benchmarks
Node.js v26.13.1, Linux, pinned to one core with
taskset, baselinemainat c15964b82. The benchmark serializes 3,000 batches of 512 spans withProtobufTraceSerializer.serializeRequest: half HTTP server spans with 13 attributes and half HTTP client spans with 8, on a resource with 19 attributes. Before and after builds alternated over three rounds; the table shows medians. Instruction and cycle counts come fromperf stat(3 repeats per build); instructions per span have Node.js startup subtracted.| Measurement | CommonJS before → after | ESM before → after |
|---|---|---|
| Instructions per span | 37.1k → 34.6k (−6.6%) | 38.0k → 35.1k (−7.7%) |
| Cycles, whole run | 20.85B → 19.90B (−4.5%) | 21.90B → 20.48B (−6.5%) |
| Time per span | 4,406 ns → 4,203 ns (−4.6%) | 4,545 ns → 4,322 ns (−4.9%) |
The before and after ranges were separated in every row. An earlier run of the same comparison gave −6.4% / −8.1% instructions and −4.9% / −7.9% time per span.
For comparison, the first version of this PR, which put the fast path in
writeTagonly, measured −5.4% / −5.4% instructions and −3.4% / −5.4% time per span in the same run.I haven't included the package's
test:benchresults: their run-to-run variation on this machine is larger than this change.Type of change
How Has This Been Tested?
@opentelemetry/otlp-transformerunit tests (258 passing), including newwriteVarintandwriteTagtestseslintandtscChecklist: