Skip to content

Measure B1: is an O(1) (refcounted, immutable) str copy affordable? Numbers on stage1 decide #1211

Closed

Description

@ehartford

Tracks the measurement that decides whether str gets an O(1) copy. No representation change happens under this issue; it produces numbers.

Context

Ruling (Eric, 2026-09-19): a copy is never implicit unless it is O(1). str is an owned (ptr, len) heap buffer today; §15.2 says &str → str is .to_owned() (allocates). So an owned-value demand on a &str view is spelled .clone() everywhere, comprehensions included ("option A"; landed with the fix for the slice/array double owner). That holds regardless of what happens here.

Reference survey (verified in .reference/): none of go / mojo / rust / scala3 / swift / Vale / zig copies a string implicitly while paying an allocation for it. The ones with implicit copy made the copy O(1) first — Mojo (3-word copy + relaxed-atomic refcount, COW, ≤23 B inline; stdlib/std/collections/string/string.mojo:601-613), Swift (2 words + ARC retain, skipped for small/immortal strings), Vale (str is always ShareT; alias + RC++, nothing in an immutable region; Backend/src/region/rcimm/rcimm.cpp:99-114). Rust, whose String::clone allocates, spells it. Mojo 0.25.6 drew the line exactly here: List/Dict/Set lost implicit copy, String kept it.

B1, the candidate

Immutable shared str with a refcount:

  • str stays (ptr, len); the count lives in the allocation header the allocator already writes before the payload.
  • Static literals are immortal, marked in the value itself (a bit in len), so copying or dropping one touches nothing.
  • Copy = increment; drop = decrement, free at zero. ++ keeps its in-place append only when the count is 1 (str_owned_capacity_from_ptr).
  • Strings hold no pointers: no cycles, no leak risk.
  • The run-time "is this pointer heap-owned?" lookup (rt_payload_start_is_owned, a binary search of the allocator's range table on every string free and capacity check) goes away.

What to measure, on stage1 self-compiling the compiler (with-stage1 check src/main.w and a :dev build)

  1. str clones — count and bytes copied (with_str_clone_ref and the codegen clone path). This is what B1 turns into increments.
  2. str drops — count, split static vs heap-owned. This is what B1 turns into decrements.
  3. Cost of rt_payload_start_is_owned today — calls and time. In a sample profile of c_import on 2026-09-18 it was the top frame (9,146 of ~21,000 samples).
  4. Thread crossing — how many str values cross a thread or fiber-scheduler boundary (spawn captures, channel sends). Decides atomic vs non-atomic counts, or an atomic count only for strings that are Send-ed.
  5. Increment cost — a micro-benchmark of plain vs relaxed-atomic increment/decrement at the clone/drop rates from 1–2, on darwin-arm64 and linux-x86_64.
  6. In-place append — how often ++ reuses capacity today, and how often the buffer would have been shared (count > 1) under B1.

Instrument inside the runtime (counters dumped at exit under an env var), not around the tools.

Decision rule (Eric)

  • Numbers fine — thread crossing is rare or relaxed-atomic is cheap, and dropping rt_payload_start_is_owned pays for the increments → B1 becomes its own campaign. When it lands: str gets the cheap-copy capability, D22 extends by one line (a &str materializes a str), and the .clone() calls option A added on strings become redundant and are removed.
  • Numbers bad → A is the permanent answer, and the §13.6 examples change to iterate move tokens.

Out of scope here

The representation change itself, small-string inline storage, and any spec wording. §13.6's examples do not compile as written under A; that edit is separate and needs Eric's blessed words.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): superseded by D111 (Eric, 2026-10-08), which decided the representation this measurement was to inform: "str is a value: passing a str always copies … The implementation is an immutable, shared, reference-counted buffer (atomic count — str is Send; literals immortal): a copy is a pointer plus a count increment". D120 (the copy-on-write campaign) landed it.

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