Skip to content

obs-util: Give one-pass dstr_safe_printf C's capacity and passes - #1624

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/dstr-safe-printf-capacity
Oct 21, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/dstr-safe-printf-capacity

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1623

What and why

Follow-up to #1619. Since the one-pass dstr_safe_printf, the Tier 3 proptest tests::strings::dstr_parity::dstr_ops_match_c fails intermittently on master and on unrelated PRs: the bytes match C, the capacity does not.

  • Cause. C's dstr_safe_printf is dstr_copy(format) and then one dstr_replace per non-NULL value, in $1..$4 order. The copy sizes the buffer for the format, each growing replace grows it with dstr_ensure_capacity's rule (double, or the needed size if that is larger), and a shrinking replace keeps it. The one-pass writer reserved max(format, result) + 1 instead. For the shrunk case, "$1" with a three-byte value, C ends with capacity 6 (3 for the copy, then doubled to fit 4) and Rust with 4.
  • Fix. safe_printf_plan (in rust/obs-util/src/dstr.rs) counts each placeholder in the format and replays C's steps on those counts, starting from the destination's current capacity: the copy, then one growth step per value that is longer than its placeholder. The C ABI (ffi/dstr.rs) and the safe Dstr reserve exactly that capacity once and then write the result in one pass. It keeps perf(dstr): substitute safe_printf placeholders in one pass #1619's shape: one walk to count, one to write, one allocation (speed below).
  • Pass order. perf(dstr): substitute safe_printf placeholders in one pass #1619 kept C's four passes when a value holds a $, since a later pass then rewrites what an earlier one inserted ("$1", $1 = "$2", $2 = "X" gives "X"). That still holds, and is pinned again below. The new cases found one more way a later pass sees a placeholder that the format did not have: a $ in the format right before a substituted placeholder joins the start of its value. "$$1" with $1 = "2" and $2 = "X" is "X" in C (pass 1 makes "$2", pass 2 replaces it) and was "$2" in one pass. The plan returns nothing for such formats, and they run C's passes too. dstr_ops_match_c could not generate this (its alphabet had no 2). perf(dstr): substitute safe_printf placeholders in one pass #1619 merged after v0.1.0-beta.20261020, so no tagged release has either difference.
  • The safe Dstr now also cuts the values at their first NUL before the one pass, as dstr_replace reads them (the C ABI already did).
  • No exported symbol changes: only private Rust functions changed.
  • No docs/features.md line: buffer capacity is not visible to users, and the "$$1" difference never shipped.

RED → GREEN

  • RED commit: 5d14f52 obs-util: Pin dstr_safe_printf capacity and pass order against C. Adds to rust/obs-util/tests/strings/dstr_parity.rs:

    • safe_printf_capacity_matches_c: 24 fixed cases, each on a fresh dstr and on two that already hold a buffer (capacity 3 and 17), for both the C ABI and the safe Dstr against the C oracle. Values longer than, shorter than and as long as their placeholder, several placeholders that grow and shrink in different passes, a $ inside a value, a $ in the format right before a placeholder, NULL values, a NULL and an empty format. On master it fails: capacity mismatches in 5 cases (the bug: obs-util: dstr_safe_printf one-pass port leaves a different capacity than C, so dstr_ops_match_c fails intermittently #1623 case among them, 4 vs 6) and byte mismatches in the 3 "$$1"-style cases.
    • safe_printf_matches_c: a proptest over formats and values made of $, 1..4 and a filler, with a random prior buffer, for both the C ABI and the safe Dstr. On master it shrinks to "$$1" with $1 = "2", $2 = "".
    • 2 added to the alphabet of dstr_ops_match_c.
  • GREEN: 97a48be. Both new tests pass, and so do the existing ones (safe_printf_substitutions_are_not_rescanned, safe_printf_with_placeholders_allocates_once, ffi_safe_printf_matches_the_sequential_passes, safe_printf_does_not_allocate_rust_side, cf_parser). soldr cargo test -p obs-util: 744/744 in the harness.

  • High case counts on GREEN, all passing: dstr_ops_match_c at PROPTEST_CASES=20000 and 200000, safe_printf_matches_c at 200000 and 1000000.

  • Local gate: --lane lint, --lane cross and --lane rust-test ok (obs-tests-core 397/397, the obs-util harness 744/744, plus the x86_64-pc-windows-gnu --no-run build).

  • Speed, port_comparison dstr/safe_printf, master and this branch alternated, with C as the in-run control (rust/C medians):

    bench master this branch
    dstr/safe_printf/128 0.61, 0.62, 0.62 0.69, 0.66, 0.70, 0.68
    dstr/safe_printf/4096 0.84, 0.85, 0.85 0.92, 0.83, 0.87, 0.87

    Still one allocation and ahead of C at both sizes. The 128-byte case costs about 6 ns more (59 → 65 ns, C 95 ns): the counting walk now also checks for $$ and replays the four growth steps. At 4096 bytes the difference is within the noise.

  • Real app: not applicable (no change a user can see).

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none (this removes two)

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
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
@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 15:39
@zackees
zackees enabled auto-merge October 21, 2026 15:41
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit d05d39a Oct 21, 2026
25 checks passed
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: obs-util: dstr_safe_printf one-pass port leaves a different capacity than C, so dstr_ops_match_c fails intermittently

2 participants