Repository navigation
obs-util: Give one-pass dstr_safe_printf C's capacity and passes - #1624
Merged
Merged
Conversation
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
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.
Fixes #1623
What and why
Follow-up to #1619. Since the one-pass
dstr_safe_printf, the Tier 3 proptesttests::strings::dstr_parity::dstr_ops_match_cfails intermittently on master and on unrelated PRs: the bytes match C, the capacity does not.dstr_safe_printfisdstr_copy(format)and then onedstr_replaceper non-NULL value, in$1..$4order. The copy sizes the buffer for the format, each growing replace grows it withdstr_ensure_capacity's rule (double, or the needed size if that is larger), and a shrinking replace keeps it. The one-pass writer reservedmax(format, result) + 1instead. 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.safe_printf_plan(inrust/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 safeDstrreserve 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).$, 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_ccould not generate this (its alphabet had no2). perf(dstr): substitute safe_printf placeholders in one pass #1619 merged after v0.1.0-beta.20261020, so no tagged release has either difference.Dstrnow also cuts the values at their first NUL before the one pass, asdstr_replacereads them (the C ABI already did).docs/features.mdline: 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 torust/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 safeDstragainst 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..4and a filler, with a random prior buffer, for both the C ABI and the safeDstr. On master it shrinks to"$$1"with$1 = "2",$2 = "".2added to the alphabet ofdstr_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_cat PROPTEST_CASES=20000 and 200000,safe_printf_matches_cat 200000 and 1000000.Local gate:
--lane lint,--lane crossand--lane rust-testok (obs-tests-core397/397, the obs-util harness 744/744, plus the x86_64-pc-windows-gnu--no-runbuild).Speed,
port_comparisondstr/safe_printf, master and this branch alternated, with C as the in-run control (rust/C medians):dstr/safe_printf/128dstr/safe_printf/4096Still 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
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable