← Back to Pull requests

perf(otlp-transformer): add a fast path for single-byte varints in ProtobufWriter#writeVarint #7387

Closed
unknown closed this pull request without merging on Nov 26, 2026 09:23 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 17:19 UTC
Author

Which problem is this PR solving?

Refs #6100

ProtobufWriter#writeVarint is the most frequent call in protobuf serialization. Every field tag goes through it via writeTag, as do lengths, enum values and counts. Each attribute alone writes four tags: the KeyValue entry, its key, its value and the AnyValue type. Nearly all of these values are below 0x80, so they encode as a single byte, but each one still goes through estimateVarintSize(), the 32-bit range check and the varint loop.

Short description of the changes

writeVarint writes 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 of writeVarint, it made no measurable difference (+0.6% / −1.0% instructions per span), presumably because the larger method was no longer inlined.

writeTag is unchanged and gets the fast path through writeVarint. 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 writeVarint writes for 0, 1, 127, 128, 300, 2³² − 1 and −1, and that writeTag writes for 1-, 2- and 3-byte tags, starting from both an empty and a pre-sized buffer. They pass on main and on this branch.

Benchmarks

Node.js v26.13.1, Linux, pinned to one core with taskset, baseline main at c15964b82. The benchmark serializes 3,000 batches of 512 spans with ProtobufTraceSerializer.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 from perf 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 writeTag only, 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:bench results: their run-to-run variation on this machine is larger than this change.

Type of change

  • Performance improvement (non-breaking, no behaviour change)
  • How Has This Been Tested?

  • @opentelemetry/otlp-transformer unit tests (258 passing), including new writeVarint and writeTag tests

  • eslint and tsc
  • Checklist:

  • Followed the style guidelines of this project

  • Unit tests have been added

  • Documentation has been updated (not applicable)
  • Diff: 387 lines (+290 / -97) across 4 files Reviewer: legendecas Review duration: 2403m
    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
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43% (MEDIUM) · Higher = larger blast radius
    43%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17% · Safe to merge
    17%