Skip to content

D34-C: ++ accumulation on str becomes amortized-efficient; StringBuilder retires; accumulator lint deleted #919

Closed

Description

@ehartford

Ruling: docs/decisions.md D34 ('C absolutely — if we can make ++ as efficient as StringBuilder we should do it and remove complexity').

Why With can do what Rust/Go can't

They need a separate builder type because they cannot prove the accumulator is unaliased at the append site. With's ownership model proves unique ownership statically — s = s ++ x (and s ++= x) on a uniquely-owned base is exactly the condition for growing the buffer in place.

Components

  • str capacity awareness: in-place growth needs to know the allocation's usable size — either the allocator's block-size query (rt_alloc knows) or a capacity field in the str payload header. Decide with a micro-benchmark; fixpoint-sensitive either way.
  • codegen/runtime fast path: base ++ x where base is a uniquely-owned local/place at its last use before reassignment to itself → reserve+memcpy append instead of fresh-alloc+copy. The unique-ownership condition comes from the move checker's existing facts.
  • DELETE the loop-accumulator lint (warn_loop_string_concat_accumulation) — the natural spelling becomes the fast spelling; a lint herding users from the ergonomic form to a ceremonial one is the mission's named anti-pattern.
  • retire StringBuilder from the user surface (docs, spec §29 mentions, examples); it may survive internally until callers migrate.
  • comptime evaluator: the same fast path in the evaluator's string ops (build actions accumulate manifests — the test-green 77s anomaly's home).
  • perf proof: before/after on the accumulation micro-bench + the evaluator-heavy build actions; baselines to docs/perf.

Interim (landing first, subsumed later)

  • [x-in-flight] StringBuilder.push_str becomes reserve+bulk-copy instead of one bounds-checked push PER BYTE (~100x native; far worse under the comptime evaluator where each byte was several interpreted steps).
  • to_str() zero-copy when uniquely owned (buffer adoption instead of final copy) — pending a look at str payload layout.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): needs a ruling: two decisions now point opposite ways. D34 (2026-09-02) rules that ++ accumulation becomes as efficient as a builder, StringBuilder retires and the accumulator lint is deleted. D111 (2026-10-08) rules that str is an immutable, shared, counted buffer and that "Text that is built or edited goes through a builder type that produces a str." Today the lint fires and recommends StringBuilder:

    warning: string concatenation with ++ inside a loop repeatedly copies the accumulator; use StringBuilder or collect pieces and concatenate once
    

    The question: does D111 supersede D34-C (the builder stays and the lint is the guidance), or does D34-C stand on top of D111 (a uniquely held buffer at its last use appends in place, so s = s ++ x is amortized and the builder and lint go)?

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. ehartford commented on Nov 19, 2026

    @ehartford
    ContributorAuthor

    Ruled, 2026-11-18 (D219): D34-C stands and D111's sentence describes the implementation; StringBuilder leaves the prelude and stays in std.string; the lint stays for non-append shapes with advice that works, and also covers an accumulator that is not uniquely held, where provable (every append copies then).

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions