← Back to Pull requests

perf(otlp-transformer): check and copy short ASCII strings in one pass in ProtobufWriter#writeString #7385

Closed
unknown closed this pull request without merging on Nov 26, 2026 09:41 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 12:59 UTC
Author

Which problem is this PR solving?

Refs #6100

String writes are the largest cost in OTLP protobuf serialization. In a CPU profile of ProtobufTraceSerializer.serializeRequest on a batch of typical HTTP spans, ProtobufWriter#writeString takes about 45% of the time. Attribute keys and most values are short ASCII strings, and for those writeString walks the string twice: once to check that every character is ASCII, then again to copy it, with a writeVarint and an extra _ensureCapacity call in between.

Short description of the changes

For strings shorter than 128 characters, writeString now checks and copies each character in the same loop, writing just after the length-prefix byte. An ASCII string shorter than 128 bytes always has a 1-byte length prefix, so the prefix is written directly after the loop. If a non-ASCII character turns up, the loop stops and the existing TextEncoder path runs as before. It writes from the same position, so the partly copied bytes are overwritten.

The output is byte-for-byte the same. The existing writeString UTF-8 tests compare the output with TextEncoder for ASCII prefixes followed by non-ASCII characters, lone surrogates, and different buffer capacities. I added length 126 to them, so a non-ASCII character in the last position under the 128 limit is covered.

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, half HTTP server spans with 13 attributes and half HTTP client spans with 8, on a resource with 19 attributes. That's about 463 bytes per span. 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 → 28.7k (−23%) | 38.4k → 30.0k (−22%) |
| Cycles, whole run | 20.85B → 16.57B (−21%) | 21.80B → 17.63B (−19%) |
| Time per span | 4,375 ns → 3,506 ns (−20%) | 4,614 ns → 3,734 ns (−19%) |

All three before and after ranges were separated, with no overlap. The serialized output was identical (237,018 bytes per batch).

Package benchmarks

The package's own npm run test:bench suite, which feeds the benchmark dashboard. Same machine, before and after builds alternated over three rounds, medians in ops/sec. Absolute numbers are lower than on the CI runner.

| Benchmark | Before | After | Change |
|---|---:|---:|---:|
| transform 512 spans (protobuf) | 491 | 607 | +24% |
| transform 512 logs (protobuf) | 467 | 597 | +28% |
| transform 512 metrics (protobuf) | 475 | 606 | +28% |
| protobuf: 10 spans, 0% exceptions | 24,871 | 29,725 | +20% |
| protobuf: 100 spans, 0% exceptions | 3,046 | 3,560 | +17% |
| protobuf: 512 spans, 0% exceptions | 565 | 686 | +21% |
| protobuf: 512 spans, 10% exceptions (short ASCII) | 536 | 691 | +29% |
| protobuf: 512 spans, 100% exceptions (long ASCII) | 240 | 331 | +38% |

The other protobuf: N spans, ... exceptions cases improved by 8–27%, with no overlap between runs. The JSON transforms, the 4 KB to 64 KB attribute cases and 512 spans, 100% exceptions (long Unicode) stayed within noise: they don't use this path or are dominated by strings of 128 characters or more, which still go through TextEncoder.

Type of change

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

  • @opentelemetry/otlp-transformer unit tests (256 passing)

  • eslint and tsc
  • Checklist:

  • Followed the style guidelines of this project

  • Unit tests have been added

  • Documentation has been updated (not applicable)
  • Diff: 480 lines (+360 / -120) across 6 files Reviewer: legendecas Review duration: 2681m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 480 lines diff across 6 files
    0.47
    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.369
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7385"
    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: 47.3% (MEDIUM) · Higher = larger blast radius
    47.3%
    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: 18.7% · Safe to merge
    18.7%