Skip to content

feat!: give fast safe code a heap of per-thread pool entries - #82

Merged
owenthcarey merged 4 commits into
mainfrom
rfc0055-a-heap-for-safe-code
Nov 30, 2026
Merged

owenthcarey merged 4 commits into
mainfrom
rfc0055-a-heap-for-safe-code

Conversation

@owenthcarey

Copy link
Copy Markdown
Contributor

Implements RFC 0055 — A heap for safe code, which was accepted as drafted. Its Implementation amendments (1–15) and Measured gates record what was built and measured.

What changes

  • Per-thread pool entries and a one-word short path (§4, §5).
    • Each thread has an entry for each pool, at the pool's number, holding up to 128 free slots. The entries live in a reservation the sweep does not read.
    • __weavec_rt_take pops a slot and stores the live word.
    • __weavec_rt_give compares the word with the expected live word, stores the released word, zeroes the object and pushes the slot.
    • There is no compare-and-swap (S0 measured it), no allocation or release record, no counter, and no depth increment.
    • An entry that is empty or full refills or flushes half its room from the pool's shared list (refillEntry, flushEntry). A pool that grows takes fresh slots by the half entry (joinPool).
    • The entries are off, and the slow paths are RFC 0050's, when the program has any of: a checked or verify unit, heap stacks, counters, or its own allocator.
  • realloc keeps a block's type (§3, D3).
    • __weavec_rt_realloc_typed gives a block from the old block's pool. The site's record types only a null or untyped block.
    • A conversion of the grown block to another type now stops (bad-cast).
  • One contract for Safe C and Safe C++ (§2, D2).
    • The reference test (O4) runs only in the checked and verify builds.
    • A program with Safe C keeps its pooled slots' shadow.
    • A checked safe unit of either language announces tests_released.
  • Pools are keyed by layout as well as name (G10).
    • Two definitions of one C++ name, one of them in a library that definition-mismatch never sees, shared a pool. This is also true on main.
  • The library table names the typed forms typed(take, F), typed(give, F) (a clause a release row may now carry) and typed(realloc_typed, 0).
  • Versions: runtime ABI 50, ledger 15 (adds typedReleases, and the summary line counts typed releases).
  • Pages updated: guarantees.md (O4, Y1), the Safe C and Safe C++ guides, the CLI reference, architecture, AGENTS.md, the roadmap.

The in-line forms (§6) were built and measured in S0 (c). They gained 1.7% and 1.3% on two programs and nothing on the other two, short of the bar of 2% on two, so per D4 they are not shipped.

Measured (darwin-arm64, Release compilers, medians of 9; ratio to Clang -O2)

Program main, fast this, fast Rust
json C++ (RFC 0049) 1.20 0.98
json C++ (RFC 0044) 1.36 1.21 0.94
json Safe C (RFC 0054) 1.96 1.19 0.65–0.67
interpreter 1.45 1.28
alloc+release pair 20.0 ns (system: 17.2) 8.8–9.1 ns
  • On Linux (container): a pair costs 9.0–9.7 ns against glibc's 11.1–12.0.
  • Checked builds: within 1.01 of main.
  • Peak memory: at or below main's for every program but the interpreter (+0.9%). The Safe C port uses half of main's.
  • Guarded code: objects are unchanged but for the ABI number in the frame word.

Gates not met as written, so the RFC stays Accepted:

  • G2 and G3: RFC 0044's port and the Safe C port. The heap is now at parity with the system allocator; what is left is WeaveC's own code, about 110 ms on json49 (amendment 15).
  • G5: tinyxml2 is 1.4% slower than main. That program makes about 200 allocations a document, so this is code layout: with -falign-functions=64 on both builds the gap is 0.9%.
  • G7: the interpreter's peak memory is 0.9% over main's.
  • Not run: several conversions (yaml-cpp, cJSON, sds, inih, jsmn, tinyexpr), RFC 0044's seeded bugs, RFC 0046's indexer, guarded Lua, compile time.

Tests

  • darwin-arm64: every lit test, case suite, unit test and rt_test passes. rt_test gains entries-on-and-off, entries-refill-and-flush, entries-across-threads (1,000 runs pass) and realloc-keeps-type.
  • New lit tests: test/Guards/realloc-typed.c and test/Guards/pool-layouts.cpp.
  • Linux container: ci-release CTest passes 56 of 56, and entries-across-threads passes 200 runs.
  • Other checks: hygiene, spelling, format, documentation examples and the site build pass.
  • clang-tidy (Release, in the container) reports two analyzer findings in lines this change does not touch: GuardLibrary.cpp:1007 and GuardSafe.cpp:1164.

Follow-ups noticed

  • The G10 round's side finding: a Safe C bound is kept from before a realloc, so a read of a grown block's new element is a false pointer-out-of-bounds. This is on main too and is not this RFC's.

RFC 0055, accepted as drafted. A fast safe unit's heap is one design for
C and C++:

- Each thread keeps an entry of free slots for each pool it uses. A typed
  allocation (__weavec_rt_take) is a pop and one stored word; a typed
  release (__weavec_rt_give) compares the slot's word, stores the
  released one, zeroes the object and pushes. No compare-and-swap, no
  records, no counters. Refills and flushes move half an entry, and a
  pool that grows takes fresh slots by the half entry.
- A realloc keeps its block's type (__weavec_rt_realloc_typed).
- The reference test (O4) belongs to the checked build in Safe C as in
  Safe C++, so a program with Safe C keeps its pooled slots' shadow.
- Pools are keyed by layout as well as name.

The in-line forms (section 6) were measured in S0 and not shipped.

Breaking: runtime ABI 50, ledger 15. The library table's typed clauses
name take, give, calloc_typed and realloc_typed. A fast Safe C build no
longer stops at a stale reference before the slot is reused; test with
-fweavec-safe-checks=full.
A full entry whose room is odd (the classes of 3072, 3584, 5120, 10240,
12288 and 28672 bytes) gave half its slots to its pool's shared list and
kept half, losing the last one: it stayed claimed in its pool and was
never reused. The flush now gives the larger half. rt_test
entries-odd-room pins it.

A typed release no longer reads the table of the slots' pools when it
could not be mapped. The comment on an entry's room names its real
bound, 256 KiB.
A checked unit loaded with dlopen while other threads run fast code does
not see every released block as released: slots pooled before it loaded,
and those another thread releases before its next slow pool operation,
keep their shadow. Amendment 7 and (O4) now say so.
@owenthcarey owenthcarey changed the title feat!: give fast safe code a heap of per-thread pool entries (RFC 0055) feat!: give fast safe code a heap of per-thread pool entries Nov 30, 2026
@owenthcarey owenthcarey changed the title feat!: give fast safe code a heap of per-thread pool entries feat!: per-thread pool entries for fast safe code (RFC 0055) Nov 30, 2026
@owenthcarey owenthcarey changed the title feat!: per-thread pool entries for fast safe code (RFC 0055) feat!: give fast safe code a heap of per-thread pool entries Nov 30, 2026
@owenthcarey
owenthcarey merged commit 207bebd into main Nov 30, 2026
13 checks passed
@owenthcarey
owenthcarey deleted the rfc0055-a-heap-for-safe-code branch November 30, 2026 03:56
owenthcarey added a commit that referenced this pull request Nov 30, 2026
RFC 0055 (#82) took ABI 50, which this branch had also used for
__weavec_rt_cast's new signature. RFC 0054 amendment 22.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant