Skip to content

Bug: str self-append is quadratic past 4KB — large allocations never reuse capacity in place #554

Closed

Description

@ehartford

Background

++ already has StringBuilder-like lowering in the two common shapes:

  1. Single-expression chains a ++ b ++ ... ++ z flatten to one RK_STR_CONCAT_N → with_str_concat_n (one allocation, one pass) — src/MirLower.w:2656.
  2. Self-append s = s ++ x [++ y ...] on a non-aliased local moves s into with_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

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

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Fixed in 9be5070. with_str_concat_n_move_first now 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions