Skip to content

obs-util: dstr replace searches efficiently and skips no-match allocation (#1296) - #1389

Merged
zackees merged 2 commits into
masterfrom
perf/1296-dstr-replace
Oct 18, 2026
Merged

zackees merged 2 commits into
masterfrom
perf/1296-dstr-replace

Conversation

@zackees

@zackees zackees commented Oct 18, 2026

Copy link
Copy Markdown
Member

Refs #1296 (part of #1295)

Cause

Dstr::replace compared a find-sized slice at every byte and always built a new Vec of the whole string, even when nothing matched. dstr_safe_printf calls 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; memchr is not a workspace dependency), then compare the rest. Left-to-right, non-overlapping, as C strstr loop.
  • No match: return before any allocation.
  • Equal-length replace: rewritten in place. Otherwise count matches, allocate the exact size once, and fill in one pass. C capacity bookkeeping (ensure_capacity when growing) is unchanged.

Tests

  • RED (7dc77f8): new Tier 1 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_printf with 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.
  • Tier 3 parity: new proptest replace_overlapping_matches_c over 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 existing dstr_ops_match_c (which covers embedded NULs). Ran with PROPTEST_CASES=5000.
  • GREEN (e6e8441): soldr cargo test -p obs-util --test strings 83/83 passed, ./lint rc=0.

Benchmarks (soldr cargo bench -p obs-c-oracle --bench port_comparison -- 'dstr/(safe_printf|replace)', local, medians)

case Rust before Rust after C Rust/C before → after
safe_printf/128 1002 ns 248 ns ~151 ns 6.3× → 1.65×
safe_printf/4096 20.19 µs 1.92 µs ~347 ns 63× → 5.5×
replace/128 527 ns 875 ns ~1.08 µs 0.52× → 0.81×
replace/4096 10.3 µs 24.7 µs ~56.5 µs 0.17× → 0.44×

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 owned Dstr and 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.

@zackees
zackees enabled auto-merge October 18, 2026 03:55
@zackees
zackees added this pull request to the merge queue Oct 18, 2026
Merged via the queue into master with commit 827bd13 Oct 18, 2026
23 checks passed
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.
@zackees
zackees deleted the perf/1296-dstr-replace branch October 26, 2026 15:45
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.

1 participant