Skip to content

perf(encoding): check label values for escaping eight bytes at a time - #388

Open
g4lb wants to merge 3 commits into
prometheus:masterfrom
g4lb:perf-label-escape-check
Open

g4lb wants to merge 3 commits into
prometheus:masterfrom
g4lb:perf-label-escape-check

Conversation

@g4lb

@g4lb g4lb commented Nov 18, 2026

Copy link
Copy Markdown

Summary

The text encoders checked each byte of a label value three times to find \, " and \n. Now needs_escaping checks eight bytes at a time with a zero-byte test. Values shorter than eight bytes use one padded word. The result does not change, so values that need escaping go to write_str_escaped as before.

The counter _total suffix is now one write instead of two.

Benchmarks (Apple M5, rustc 1.98.0)

cargo bench --bench text, 12 interleaved rounds against master, Mann-Whitney test:

Benchmark master this PR Change
encode_realistic_string_labels 10.9 ms 10.5 ms −3.3%
encode_escaped_label_values 11.2 ms 11.0 ms −1.4%
prometheus_text_encode_realistic_string_labels 9.04 ms 8.58 ms −5.1%
prometheus_text_encode_escaped_label_values 9.42 ms 9.14 ms −3.0%

A local benchmark with 500 label values that need escaping is 18% faster, and a registry with 10 counter and gauge series is 7% faster. encode and prometheus_text_encode did not change significantly, and I measured no significant slowdown. I did not measure other architectures.

Checks

  • cargo fmt, cargo clippy --workspace --all-targets -- -D warnings and cargo test --all --all-features pass.
  • Old and new output of encoding::text::encode and encoding::prometheus_text::encode matched on 300k random registries. The registries have counters, gauges, histograms, exemplars, info metrics, units, sub-registries, and label values with \, ", \n and non-ASCII text.

g4lb added 3 commits November 18, 2026 17:27
Signed-off-by: g4lb <galevgi@gmail.com>
Signed-off-by: g4lb <galevgi@gmail.com>
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.

1 participant