Skip to content

perf(encoding): add text buffer to reduce dynamic dispatch overhead - #370

Open
herin049 wants to merge 6 commits into
prometheus:masterfrom
herin049:perf/remove-dyn-writer
Open

herin049 wants to merge 6 commits into
prometheus:masterfrom
herin049:perf/remove-dyn-writer

Conversation

@herin049

@herin049 herin049 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The text encoders write every small fragment ("{", ",", "=\"", numbers, …) through &mut dyn Write. These small writes introduce measurable overhead due to dynamic dispatch.

This PR adds a new TextBuf type to buffer writes to the underlying writer to ensure hot path writes are statically dispatched.

Benchmarks (Apple M5 Pro - 48GB memory, rustc 1.94.1)

cargo bench --bench text, compared with --baseline against master:

OpenMetrics (encoding::text)

Benchmark master this PR Change
encode 11.56 ms 9.15 ms −20.9%
encode_realistic_string_labels 12.20 ms 9.74 ms −20.2%
encode_escaped_label_values 14.05 ms 10.45 ms −25.6%

Prometheus text (encoding::prometheus_text)

Benchmark master this PR Change
prometheus_text_encode 11.09 ms 8.48 ms −23.5%
prometheus_text_encode_realistic_string_labels 11.81 ms 9.18 ms −22.3%
prometheus_text_encode_escaped_label_values 13.49 ms 9.54 ms −29.3%

Buffer sizes from 1 to 64 KiB all landed within about 3% of each other. 4 KiB was chosen to keep the footprint small.

@herin049
herin049 force-pushed the perf/remove-dyn-writer branch from 8b93088 to 9ff12dd Compare November 6, 2026 19:58
Signed-off-by: Lukas Hering <lukasheringb@gmail.com>
Signed-off-by: Lukas Hering <lukasheringb@gmail.com>
@herin049
herin049 force-pushed the perf/remove-dyn-writer branch from 9ff12dd to 735f9a7 Compare November 6, 2026 19:59
@herin049
herin049 marked this pull request as ready for review November 6, 2026 20:36
herin049 and others added 3 commits November 6, 2026 15:36
Signed-off-by: Lukas Hering <lukasheringb@gmail.com>
Signed-off-by: Lukas Hering <lukasheringb@gmail.com>

@krisztianfekete krisztianfekete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks, LGTM with the inline fix. Please also add CHANGELOG entries for the output changes.

Comment thread src/encoding/text.rs Resolved

@krisztianfekete krisztianfekete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, please sign the DCO so we can merge this!

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.

2 participants