Skip to content

perf(dstr): substitute safe_printf placeholders in one pass - #1619

Merged
zackees merged 2 commits into
obs-rust:masterfrom
MCPVOT:fix/1296-safe-printf-one-pass
Oct 21, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
MCPVOT:fix/1296-safe-printf-one-pass

Conversation

@MCPVOT

@MCPVOT MCPVOT commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Closes #1296 (claimed there after you said "yes please take it").

dstr_safe_printf copied the format and then ran one dstr_replace per 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:

format "$1"  value1 "$2"  value2 "X"
C:  pass 1 -> "$2"            (the value was inserted verbatim)
    pass 2 -> "X"             (the later pass rewrites what pass 1 inserted)

So that case keeps the original chain (safe_printf_substitutions_are_not_rescanned already 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:

bench before after
dstr/safe_printf/128 1.042 0.763 -26.8 % vs C (418 -> 313 ns)
dstr/safe_printf/4096 0.837 0.794 -5.1 % vs C (1272 -> 1161 ns)

Both 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_null and the rest of cf_parser still pass: an empty result from a non-empty format keeps a non-NULL buffer, as dstr_copy + shrinking dstr_replace does, so nullable_diagnostic still yields Some([]).

cargo test -p obs-util: 663 passed, 18 failed. The 18 are the pre-existing platform_* parity failures on this Windows host - I verified the identical set fails on unmodified master (2aba69a) before touching anything. cargo fmt and cargo clippy --all-targets are clean.

`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`.
@MCPVOT
MCPVOT requested a review from zackees as a code owner October 21, 2026 13:16

@zackees zackees 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.

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.

@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit 2ad7f94 Oct 21, 2026
25 checks passed
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
@MCPVOT
MCPVOT deleted the fix/1296-safe-printf-one-pass branch October 21, 2026 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: dstr: safe formatting is 6.6–58.2× slower than C

2 participants