perf(otlp-transformer): check and copy short ASCII strings in one pass in ProtobufWriter#writeString #7385
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
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.369
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #7385"
general
weight: 0.35
legendecas
Which problem is this PR solving?
Refs #6100
String writes are the largest cost in OTLP protobuf serialization. In a CPU profile of
ProtobufTraceSerializer.serializeRequeston a batch of typical HTTP spans,ProtobufWriter#writeStringtakes about 45% of the time. Attribute keys and most values are short ASCII strings, and for thosewriteStringwalks the string twice: once to check that every character is ASCII, then again to copy it, with awriteVarintand an extra_ensureCapacitycall in between.Short description of the changes
For strings shorter than 128 characters,
writeStringnow 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 existingTextEncoderpath 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-8tests compare the output withTextEncoderfor 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, baselinemainat 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 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 → 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:benchsuite, 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, ... exceptionscases improved by 8–27%, with no overlap between runs. The JSON transforms, the 4 KB to 64 KB attribute cases and512 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 throughTextEncoder.Type of change
How Has This Been Tested?
@opentelemetry/otlp-transformerunit tests (256 passing)eslintandtscChecklist: