Skip to content

Native containers: SplFixedArray on a native buffer, Manticore\Ds typed arrays, SPL heaps and O(1) queues - #108

Merged
blacktrs merged 209 commits into
mainfrom
ds-own
Oct 7, 2026
Merged

blacktrs merged 209 commits into
mainfrom
ds-own

Conversation

@blacktrs

@blacktrs blacktrs commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Why

Real-world PHP ported to Manticore keeps its hot data in PHP arrays and in an array-backed SplFixedArray: a cell per element, a hash or vector buffer under it, offsetGet dispatch on every read. There was no fixed-layout, statically typed container a program could ask for, and several SPL data structures were missing or quadratic.

What changes for users

Native containers (Manticore\Ds, superset — see docs/ds.md)

  • Typed arrays over one native buffer runtime: Int8/16/32/64Array, UInt8/16/32/64Array, Float32/64Array, BitArray, and ByteBuffer (typed, endianness-aware reads and writes at a byte offset).
  • Growable: push(), pop(), $a[] = $v; whole-array sum(), min(), max(), equals().
  • Bound to an element type through the normal generics docblocks: /** @var UInt16Array<TokenKind> */ stores at 2 bytes and reads back as the #[TypeDef] class, with its methods. A binding the array cannot hold (wrong width or signedness) is a compile error.
  • For a receiver in a local, a property of a local or a static property, $a[$i], $a[$i] = $v and $a[] = $v compile to an inline bounds check plus a load or store; a counted loop loads the buffer and its length once.

SPL, with php parity (fuzzed against php's own classes)

  • SplFixedArray on the native buffer.
  • SplHeap, SplMinHeap, SplMaxHeap, SplPriorityQueue — new.
  • SplDoublyLinkedList, SplQueue, SplStack rewritten: O(1) shift / dequeue, LIFO offsets, add(), frozen stack / queue modes.

Compiler fixes found on the way

  • Generics keep a #[TypeDef] binding (short class names in Foo<Bar> resolve through use; specializations are built over the TypeDef; @return T refines a compatible native hint; $o[$k] on a bound receiver is typed like offsetGet()).
  • json_encode honours JsonSerializable and emits a backed enum's value; print_r / var_dump show object properties with php's visibility markers; __unserialize receives int keys; pack / unpack float codes; prelude code no longer traps on array_slice / array_splice; several crashes on values held in mixed (ArrayAccess writes, enum cases, float ??).
  • analyze reports front-end errors instead of answering "No errors found".

Measured (macOS arm64)

before after
10M get/set, Int32Array held in a property 168 ms 6.4 ms (array<int,int>: ~39 ms)
10M appends, Int32Array 600 ms 27 ms (array: 51 ms)
Int32Array->sum(), 10M elements 8 ms 0.7 ms
2M getUInt32 on a ByteBuffer vs unpack('V', $s, $o) 568 ms 9 ms
php-cs-fixer fork (Tokens extends SplFixedArray), symfony corpus, peak RSS 3307 MB 2595 MB

php-cs-fixer output is byte-identical before and after.

Compatibility

  • Memory ABI v18 (main's v17 and this branch's buffer layout are different layouts). No seed needed; bin/build carries it.
  • Manticore\Ds is a superset with no php oracle; its cases carry hand-written expected output.

Testing

macOS: AOT suite 1711 passed / 0 failed, known-bug repros 56 XFAIL / 0 XPASS, analyzer 22 / 0. Linux: this PR's CI.

Known gaps (tracked)

Fixes #92
Fixes #39
Fixes #57
Fixes #93
Fixes #94
Fixes #95
Fixes #96
Fixes #98
Fixes #99
Refs #101

…callbacks, and tools/ownflow_dump.php to run it on MIR stopped before InsertMemoryOps
…nside try, unresolved gotos and out-of-range break levels throw; add tests/flow fixtures with tools/ownflow_check.sh
… for InsertMemoryOps, classifyTemp arms for EmitLlvm); corpus IR byte-identical
…GEN), FFI-call drift row, ctx trimmed to what is read (moduleFns)
…t is a sentinel, not null (a nullable typed array is a cell natively and came back NaN-boxed)
…place: a typed array returned through array<K,V>|null came back NaN-boxed
…o, as the one-hop coalesce does: $n->type->element ?? X handed back a null object
…th out of the body, before each loop-leaving break/continue too, and an if/else never unplants such an exit box-back: the slot left the loop raw and var_dump($v) past it read a string pointer as a float
…local, or an erased conditional's arm, moves), a live representation mismatch fails the build with function, name and line, self-copies take no +1, the trace env is read once, and a forced round clears stale insertions
…body also stores to it: a break taken before the store left the element raw in a slot read as a cell
…the return took no +1 on moves the owned arm that ran and drops the rest by identity; the pass no longer guesses which erased returns the emitter retains
# Conflicts:
#	src/Compile/Mir/Passes/EmitLlvmExpr.php
#	src/Compile/Mir/Passes/EmitLlvmModule.php
…name-level rc track, the transfer veto and the element-share scan are deleted (container moves and buffer-only shares are decided in the pass)
…s a return takes no +1 on (Return_::ownArms), and a bare-array free function returns +1 on every path so its caller's local owns the result (erasedarr class)
… the other owned locals die at return, the taken arm stays readable by getReturn()
…ocal release ops had no emitter and are deleted
… static method and closure returns +1 (Ownership::returnRetain decides, the emitter executes), callers own erased results; container stores move a local only when it is dead, else the container takes its own +1
…in / returnBorrowsObj), not the deleted emitter copies
…-callable callees; a live container store takes its +1 on the read itself; a concrete array rebind of an erased-result local shares its release class
…kes the local a cell (if/else, loop, switch, try): the join typed unknown read a string as an array; an erased-array result boxed into that cell is owned
…retained through a probe-boxed copy: the tag retain was a no-op on a raw buffer, so $c[] = $f ? $r : [] freed $r's array with the container
…n its own family (the receiver's ancestors, interfaces and module descendants), not from every method of that name
… &&, ||, and, or, ?:, ??, ??=) is a cell: expression joins read the one joinDisagrees predicate
…narrowing reconciles its local that way, and the alias store cloned an array payload and dropped the original on every call (php-cs-fixer Token::equals with an array $other)
…ic string offset as php does; 1M-operation leak-loop case over Ds arrays and SplFixedArray
…plit element bytes from the header, the failed-check arm no longer returns into the loop, and a receiver held in a property or a static property is read and written in place (refs #101)
…pend done in place while the buffer has room (refs #101)
…alize receives its array with the keys as serialized (an int key stays an int)
… order, corruption protocol, dump and serialization (refs #101)
…efined-function trap for array_slice / array_splice behind SplQueue); var_dump and print_r read a mangled __debugInfo key as a protected / private property, as php does
…O offsets, add(), frozen stack / queue modes, cursor that starts invalid, dump and serialization (refs #101)
…rty) is loaded once per loop too: its load takes the header TBAA tag (refs #101)
…<Bar>` resolve like any class name, a specialization is built over the TypeDef rather than its carrier, `@return T` refines a compatible native hint, and `$o[$k]` on a bound receiver is typed like offsetGet()
…es at the array's width and reads back as the #[TypeDef] class (refs #101)
…its width: a #[TypeDef] by its repr and signedness, a plain scalar by its family (refs #101)
… it to stderr and answering "No errors found"
…its as an int, take a numeric string or float as an unsigned value, and order unsigned in min/max/compare (refs #101)
…on an ICU that has it, so a program that pulls in the IntlTimeZone functions links against ICU 72; on an older ICU getIanaID() is undefined, as in php
…macOS and glibc but still crashes on Alpine arm64 (musl), so #67 is not fixed
@blacktrs
blacktrs merged commit 5436577 into main Oct 7, 2026
7 checks passed
@blacktrs
blacktrs deleted the ds-own branch October 7, 2026 08:33
blacktrs added a commit that referenced this pull request Oct 7, 2026
Three shipped designs had no design note: per-program-point ownership
(OwnershipFlow, #15/#104), the native fixed-width buffer runtime behind
SplFixedArray and Manticore\Ds (#108), and the pinned-release bootstrap
(#112/#114). Each is derived from the code it cites; AGENTS.md links the
stage0 note.
blacktrs added a commit that referenced this pull request Oct 7, 2026
…114)

* Remove the Zend cold seed; docs follow the pinned-release bootstrap

The compiler bootstraps from BOOTSTRAP_VERSION (#112), so the seed has no
consumer left. Removed: bin/compile, tools/compile_files_mir.php, bin/build
--seed/--auto-seed, gate.sh MC_ZEND_SEED, install.sh's Zend fallback, and the
dead tools composer_data_smoke.sh, typecheck_scan.sh, split_ll.php,
docker/iopoll/diag.sh, docker/symfony_probe.sh. selfhost_stability.sh rebuilds
through the manifest path instead of Zend; composer.json drops the php
requirement (the installer downloads the release or builds from the pin).

Docs: every guide, README and design note audited against the tree — seed
instructions removed or marked historical, ABI references point at
MemoryAbi::VERSION (18), memory-abi.md gains the 64-byte descriptor (json_fn),
the v14 BIGINT box and the v18 native buffer block, pass tables gain
NarrowScalarGuards, ResolveMethodFcc, ResolveOverloads, SpillFreshBases and
OwnershipFlow.

* docs/design: ownership flow, native buffers and stage0 bootstrap notes

Three shipped designs had no design note: per-program-point ownership
(OwnershipFlow, #15/#104), the native fixed-width buffer runtime behind
SplFixedArray and Manticore\Ds (#108), and the pinned-release bootstrap
(#112/#114). Each is derived from the code it cites; AGENTS.md links the
stage0 note.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment