Repository navigation
Description
Problem
Real-world PHP ported to Manticore (first consumer: the native php-cs-fixer fork) keeps its hot data in PHP arrays or in SplFixedArray, which is itself a PHP-array-backed prelude class. Every element is a cell behind a hash/vector buffer, every read goes through offsetGet dispatch, and the array-backed SplFixedArray inherits every array ownership rule (it has leaked more than once). On the symfony corpus native cs-fixer is ~2x faster than Zend --parallel but still ~10x slower in wall time than mago; container overhead is a visible share of that gap.
There is no fixed-layout, statically typed container a program can ask for.
Proposal
One shared native buffer runtime, two consumers:
SplFixedArrayon a native block of cells — same public behaviour as Zend (dumps, serialization, exceptions and messages, subclasses, clone), no PHP array underneath.Manticore\Ds\*typed fixed-width arrays (superset):Int8Array,Int16Array,Int32Array,Int64Array,UInt8Array,UInt16Array,UInt32Array,Float32Array,Float64Array,BitArray, over an abstractTypedArray implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable.
API (base): __construct(int $size = 0), fromArray, toArray, getSize, setSize, count, insert($at, $count, $value = 0), remove($at, $count), fill, copyFrom, indexOf.
Semantics:
- index rules as
SplFixedArray(OutOfBoundsExceptionout of range,TypeErroron a bad offset type,Erroron$a[] = …); - values coerce like a non-strict typed parameter; no silent wrap — out of range is
ValueError("Value 300 is out of range for Int8Array (-128..127)"); Float32Arraystores and returns float32-rounded values;issetis true for any in-range index,unsetzeroes the element.
A pure-PHP polyfill package (manticorephp/ds) ships the same class sources over an array-backed storage, so the same program runs under Zend — and the polyfill is the oracle for the native implementation (byte-identical output).
For a statically known receiver the compiler inlines $a[$i] to a bounds check plus a load/store of the element width; an unknown receiver (mixed, interface) or a subclass overriding offsetGet/offsetSet goes through normal dispatch.
Out of scope
Views/subarrays over shared memory, byte buffers and endianness, whole-array arithmetic, [] append, UInt64, a typed string array. The rest of SPL on the same buffer (SplDoublyLinkedList/SplStack/SplQueue, SplHeap/SplPriorityQueue, ArrayObject) is a follow-up.
Acceptance criteria
- Buffer runtime: one layout owned by
MemoryAbi,__mc_nbuf_*builtins each with a same-named PHP twin (bootstrap rule). -
SplFixedArrayon the buffer; existing cases plus dump/serialize/clone/subclass/element-destructor parity withphp. -
Manticore\Ds\*classes; every case prints the same bytes underphp+ polyfill and natively. - Boundary values per kind covered (min, max, min-1, max+1, fractional float, numeric / non-numeric string, bool).
- Inline get/set for statically known receivers; a 10M get/set loop over
Int32Arrayis no slower than overlist<int>. - No leak over 1M-operation loops; object elements destructed exactly when
phpdestructs them. -
docs/ds.mddescribes the classes, semantics and the polyfill. - php-cs-fixer (
Tokens extends SplFixedArray) output unchanged; CPU / RSS on the symfony corpus measured before and after.
Activity
blacktrs commentedon Oct 5, 2026
Progress on branch
ds(not merged):MemoryAbi::BUF_*(ABI v16),__mc_nbuf_*builtins with stdlib twins (the planned__mc_buf_*prefix collided with Io.php).Manticore\Ds\*classes +tools/ds_polyfill.php+docs/ds.md; five AOT cases byte-equal to the Zend polyfill.$a[$i] = $a[$i] + 1overInt32Array12 ms vs 47 ms overarray<int,int>(arm64 macOS).clonethrough a base-typed value lost the runtime class;serialize()double-escaped a namespaced class name.$m[0] = 9on a mixed value holding an ArrayAccess object crashes (SIGBUS) instead of calling offsetSet #95 (erased ArrayAccess write), An immediately-invoked closure(function (string $s): P { return unserialize($s); })(serialize(new P()))SIGSEGVs #96 (IIFE object return).Remaining:
SplFixedArrayon the buffer + hidden slot drop/clone (waits for the ownership branch),#[TypeDef(repr)]element types, loop hoisting of the length load, php-cs-fixer measurement, Linux run.blacktrs commentedon Oct 5, 2026
Branch
dsatdab4f498(not merged). macOS AOT suite 1535 passed / 0 failed; xfail set 71 XFAIL / 0 XPASS; build fixpoint byte-identical. Linux and difftest not run.Bugs fixed on the branch since the last note (each carries
Fixes #Nand a promoted case): #95 erased ArrayAccess element write, #94 JsonSerializable (newjson_fndescriptor slot), #96 closure object return from a mixed value, #57 print_r of objects, #93 pack/unpack float codes. Also: classes declared in a namespaced file lost their var_dump/serialize/print_r arm (SIGSEGV).Filed, open: #97, #98, #99.
blacktrs commentedon Oct 6, 2026
Branch
ds-ownatbdfad098= this work +mainb40d04be(ownership, #15). Not merged. macOS AOT suite 1677 passed / 0 failed ata71240ca; Linux and difftest not run on this head.Done since the last note:
SplFixedArrayon the native CELL buffer behind a compiler-owned hidden slot (drop / clone / property walks), inline$f[$i]read and write.isset/empty/offsetExistswith a non-numeric string offset throwTypeErroras php does.Int32Array,Float64ArrayandSplFixedArraywith object elements: flat.php-cs-fixer fork (
Tokens extends SplFixedArray), same fork source built bymainb40d04be(array-backedSplFixedArray) and byds-own(native buffer), macOS arm64:src(259 files),@PSR12@Symfony--dry-run --diffoutput on thesrccorpus is byte-identical between the two builds.Reading: the container swap alone is worth about 3% of single-thread CPU; the memory win is real. The remaining gap to mago is not in
SplFixedArraystorage but in howTokenslays out its data — tracked in the follow-up.Found on the way, not caused by this branch (reproduces with the
mainbuild too): on the symfony corpus 65 files fail withInvalidArgumentException: Invalid param $startIndex - not a proper block "start"(exit 76); each of them passes when run alone or with its directory, and the fork binary built on 2026-10-04 does not show it.