Repository navigation
Description
Background
++ already has StringBuilder-like lowering in the two common shapes:
- Single-expression chains
a ++ b ++ ... ++ zflatten to oneRK_STR_CONCAT_N→with_str_concat_n(one allocation, one pass) —src/MirLower.w:2656. - Self-append
s = s ++ x [++ y ...]on a non-aliased local movessintowith_str_concat_n_move_first, which appends in place when the owned buffer has slack —src/MirLower.w:2585,rt/rt_core.w.
For small strings this works because allocator size classes double (16 → 32 → ... → 4096), so the in-place fast path gives amortized O(n) accumulation.
Problem (corrected 2026-06-11)
An earlier version of this issue claimed rt_payload_start_is_owned rejects large blocks. That was wrong — it already recognizes them via the large-range registry (rt/rt_core.w:455). The actual gap is narrower: past RT_LARGE_THRESHOLD (4096 bytes) allocations are exact-size (alloc_align_size rounds only to 16 bytes), so an owned large string has at most 15 bytes of slack. with_str_concat_n_move_first's in-place fast path almost never fires, and its fallback (str_concat_n_copy) allocates exactly total + 1 again — so every append past 4KB copies the entire string.
Measured: a loop appending 16 bytes per iteration, 20k iterations (320KB final) vs 60k iterations (960KB final) → 0.86s vs 7.0s user — 3× the work, 8× the time. Quadratic confirmed.
This hits the compiler itself
src/CImport.w alone has 28 out = out ++ ... accumulation sites, several of which build large translated-header sections — likely a measurable chunk of heavy c_import time (OpenSSL).
Fix (small, runtime-only)
In with_str_concat_n_move_first's reallocation path, allocate with geometric headroom — max(total + 1, 2 × (old_capacity + 1)) when the first operand was owned — instead of calling exact-size str_concat_n_copy. One-shot concats (with_str_concat_n, non-owned first operand) stay exact-size; only the loop-shaped self-append path over-allocates, restoring amortized O(n) at every size with no user-facing change.
Split-out related work
- Bug: struct-field str self-append is quadratic — field destinations never reach the move-first lowering #555 — widen the self-append in-place lowering to globals and struct fields
- Comptime-fold all-literal ++ chains in runtime code #556 — comptime-fold all-literal
++chains
Mission relevance
"If With can infer it... the programmer should not have to spell it out." With this fixed, ++ is StringBuilder under the covers everywhere, and users (and the compiler) never need to reach for the explicit type for performance reasons.
Activity
Reallocate self-append strings with geometric headroom
ehartford commentedon Jun 12, 2026
Fixed in 9be5070.
with_str_concat_n_move_firstnow reallocates with geometric headroom (max(total + 1, 2 × (capacity + 1))) when the first operand was owned; one-shot concat paths stay exact-size.Measured: the 16-bytes × 60k-iteration append loop (960KB final) drops from 7.0s to 0.09s user — linear at every size now. Regression test added (
test/behavior/behav_str_self_append_growth.w) covering content integrity across the 4KB threshold.One honest correction to this issue's claims: the predicted c_import speedup did not materialize — a cold-cache openssl import is ~35s before and after. CImport's main accumulator already uses StringBuilder, and clang parsing dominates; the
out = out ++sub-sites don't accumulate enough to matter. The win is for user programs (and any future compiler code that accumulates large strings naively, which is now free to do so).