Repository navigation
perf(dstr): substitute safe_printf placeholders in one pass - #1619
Merged
Merged
Conversation
`dstr_safe_printf` copied the format and then ran one `dstr_replace` per value, so every placeholder rescanned the growing buffer and rebuilt it. For `"$1: $2"` with a 4096-byte value that is an extra full scan and two rebuilds per call (obs-rust#1296). The format is now walked once and the result written once. C's four passes only differ when a value itself contains a `$`, because then a later pass substitutes a placeholder inside an inserted value (`$1` -> `"$2"` then `$2` -> its value); that case keeps the original chain. The C ABI also keeps it when an argument points into the destination's own buffer, where growing first would invalidate the pointer. port_comparison, 3 alternating runs per side with C as the in-run control: dstr/safe_printf/128 rust/C 1.042 -> 0.763 (418 -> 313 ns) dstr/safe_printf/4096 rust/C 0.837 -> 0.794 (1272 -> 1161 ns) An empty result from a non-empty format still leaves a non-NULL buffer, as `dstr_copy` plus a shrinking `dstr_replace` does, so `nullable_diagnostic` keeps reporting `Some([])` rather than `None`.
zackees
reviewed
Oct 21, 2026
zackees
left a comment
Member
There was a problem hiding this comment.
Coordinator review: behaviour-preserving perf change in obs-util dstr (safe_printf placeholders substituted in one pass, the four-pass chain kept as the fallback when a value contains $ or aliases the destination). Existing dstr parity tests cover behaviour against the C oracle; the new dstr_alloc cases pin the allocation count. No C tests or public headers touched. Queued.
This was referenced Oct 21, 2026
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 21, 2026
…-rust#1624) * obs-util: Pin dstr_safe_printf capacity and pass order against C Since the one-pass dstr_safe_printf, the Tier 3 proptest dstr_ops_match_c fails intermittently: the bytes match C, but the capacity does not. C copies the format and then runs one dstr_replace per non-NULL value, so its capacity is what those growth steps leave (6 for "$1" with a three-byte value), while the one-pass writer reserves the final length (4). Add fixed Tier 3 cases, checked on a fresh dstr and on ones that already hold a buffer, for both the C ABI and the safe Dstr: values longer than, shorter than and as long as their placeholder, several placeholders, a `$` inside a value, NULL values and formats. They fail on 2ad7f94 with capacity mismatches. A focused proptest, safe_printf_matches_c, draws formats and values from `$`, the four digits and a filler. The cases also show a byte mismatch dstr_ops_match_c could not reach: a `$` in the format right before a placeholder ("$$1" with $1 = "2") forms a new "$2" that C's second pass substitutes and one pass does not. Add `2` to its alphabet so it can generate such formats too. Refs obs-rust#1623 * obs-util: Give one-pass dstr_safe_printf C's capacity and passes The one-pass dstr_safe_printf from obs-rust#1619 reserved the final length, but C copies the format and then runs one dstr_replace per non-NULL value, so its capacity is whatever those steps leave: the copy sizes the buffer for the format and each growing replace grows it with the doubling rule of dstr_ensure_capacity. For "$1" with a three-byte value C ends with 6 and the one-pass writer with 4, which made the Tier 3 proptest dstr_ops_match_c fail intermittently. safe_printf_plan now replays C's steps on the count of each placeholder in the format, from the destination's current capacity, and the C ABI and the safe Dstr reserve exactly that capacity once before writing the result in one pass. The speed obs-rust#1619 wanted stays: one walk to count, one to write, one allocation. The plan also finds the one more case where four passes differ from one pass with no `$` in a value: a `$` in the format right before a substituted placeholder. "$$1" with $1 = "2" becomes "$2", which C's second pass then replaces. Those formats keep C's passes, like values that hold a `$` already did. The safe Dstr now also cuts its values at the first NUL before the one pass, as dstr_replace reads them. Follow-up to obs-rust#1619. Fixes obs-rust#1623
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1296 (claimed there after you said "yes please take it").
dstr_safe_printfcopied the format and then ran onedstr_replaceper value, so every placeholder rescanned the growing buffer and rebuilt it. With"$1: $2"and a 4096-byte value that is an extra full scan and two rebuilds per call.Now the format is walked once and the result written once.
Semantics
C's four passes only differ from one pass when a value itself contains a
$, because then a later pass substitutes a placeholder inside an inserted value:So that case keeps the original chain (
safe_printf_substitutions_are_not_rescannedalready pins this behaviour, despite its name). The C ABI also keeps the chain when an argument points into the destination's own buffer, where growing before writing would invalidate the pointer.Measurement
port_comparison, 3 alternating runs per side with the C implementation as the in-run control:dstr/safe_printf/128dstr/safe_printf/4096Both sizes are now ahead of C; before this the 128-byte case was behind it.
Tests
safe_printf_with_placeholders_allocates_once- one allocation for"$1: $2"(the old path allocated for the copy plus a rebuild per placeholder).ffi_safe_printf_matches_the_sequential_passes- byte equality against the chained passes, including an empty value and a value that itself contains$1.empty_formatted_diagnostic_preserves_nulland the rest ofcf_parserstill pass: an empty result from a non-empty format keeps a non-NULL buffer, asdstr_copy+ shrinkingdstr_replacedoes, sonullable_diagnosticstill yieldsSome([]).cargo test -p obs-util: 663 passed, 18 failed. The 18 are the pre-existingplatform_*parity failures on this Windows host - I verified the identical set fails on unmodifiedmaster(2aba69a) before touching anything.cargo fmtandcargo clippy --all-targetsare clean.