Repository navigation
obs-util: dstr replace searches efficiently and skips no-match allocation (#1296) - #1389
Merged
Merged
Conversation
zackees
enabled auto-merge
October 18, 2026 03:55
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 18, 2026
…obs-rust#1393) * test(obs-util): RED dstr FFI appends and safe_printf allocate on the Rust side (obs-rust#1297) The C ABI shims copy the whole C string into an owned Dstr and back for every append, so an 8-byte dstr_ncat onto a 4096-byte string allocates. New deterministic allocation-counting tests (as in obs-rust#1389) require zero Rust-side allocations for dstr_ncat/cat_dstr/cat_strref/ncat_dstr and dstr_safe_printf; they fail now (2, 13 and 3 allocations). Tier 3 parity gains the aliasing cases: cat_dstr(d, d), ncat_dstr(d, d), ncat from inside d's own buffer and copy from inside it, with capacity compared after every step. * perf(obs-util): dstr FFI appends edit the C buffer in place (obs-rust#1297) dstr_copy, dstr_ncat, dstr_cat_dstr, dstr_cat_strref, dstr_ncat_dstr, dstr_replace and dstr_safe_printf no longer copy the whole string into an owned Dstr and back. They grow the C buffer with dstr_ensure_capacity's rule and the same brealloc call, then memmove the new bytes and write len and the terminator. A source inside the destination buffer (cat_dstr(d, d), a pointer into d->array) is re-derived after a moving brealloc. dstr_replace runs on the buffer in place: same-length and shrinking replaces compact in place, a growing one grows the buffer once and rebuilds front to back. find_from uses a SIMD memchr for the first byte, and the replace value is measured once instead of per step. Medians (Rust/C): dstr/cat/128 114.98/53.14 -> 52.65/52.59 ns, dstr/cat/4096 242.43/115.70 -> 141.53/136.85 ns, dstr/safe_printf/128 241.80/142.47 -> 159.07/148.64 ns, dstr/safe_printf/4096 1963.8/331.27 -> 339.68/335.79 ns. * test(obs-util): record the dstr self-aliasing difference; keep the oracle off C UB (obs-rust#1297) A source that points into the destination buffer (dstr_ncat/cat_strref with a pointer into dst->array, dstr_cat_dstr(d, d), dstr_copy from inside dst->array) is UB in C (memcpy from the freed pointer after brealloc, or overlapping memcpy). Rust finds the source again after the grow and memmoves it. Listed in the parity header and in rust/README.md. The parity ops now give only Rust the aliasing pointer, and C a private copy of the same bytes, so the oracle is never fed the UB case. NcatFromOwn now also covers the growing (moving) case on the Rust side.
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.
Refs #1296 (part of #1295)
Cause
Dstr::replacecompared afind-sized slice at every byte and always built a newVecof the whole string, even when nothing matched.dstr_safe_printfcalls it once per non-NULL placeholder, so a 4 KiB value copied and rescanned the string byte-wise twice.Design
find_from: scan for the needle's first byte eight bytes at a time (SWAR, no new deps;memchris not a workspace dependency), then compare the rest. Left-to-right, non-overlapping, as Cstrstrloop.ensure_capacitywhen growing) is unchanged.Tests
tests/strings/dstr_alloc.rs, a counting#[global_allocator](thread-local count, so deterministic and independent of wall clock and parallel tests): no-match replace allocates 0 times,safe_printfwith unused placeholders allocates only for the format copy, a matching replace allocates at most once. All 3 failed on master. No enforced Rust/C bound exists in CI (release-criteria.md calls the 1.10 report bound descriptive), so I used allocation counts instead of a timing bound.replace_overlapping_matches_cover an{a,b}alphabet (self-overlapping needles, needle longer than haystack, replacement that contains the needle, empty needle, NULL replacement) against the C oracle, plus the existingdstr_ops_match_c(which covers embedded NULs). Ran with PROPTEST_CASES=5000.soldr cargo test -p obs-util --test strings83/83 passed,./lintrc=0.Benchmarks (
soldr cargo bench -p obs-c-oracle --bench port_comparison -- 'dstr/(safe_printf|replace)', local, medians)safe_printf is still above the 1.10 bound. What remains is mostly the FFI
with()wrapper, which copies the whole C buffer into an ownedDstrand back on every call (the owned-model copy tracked in #1297), so this PR says Refs, not Fixes.replace(dense matches: 'o' every 4 bytes) is slower than before because of the SWAR setup per match, but it is still well under C.