Skip to content

Native fixed-layout containers: SplFixedArray on a native buffer and Manticore\Ds typed arrays #92

Closed

Description

@blacktrs

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:

  1. SplFixedArray on a native block of cells — same public behaviour as Zend (dumps, serialization, exceptions and messages, subclasses, clone), no PHP array underneath.
  2. Manticore\Ds\* typed fixed-width arrays (superset): Int8Array, Int16Array, Int32Array, Int64Array, UInt8Array, UInt16Array, UInt32Array, Float32Array, Float64Array, BitArray, over an abstract TypedArray 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 (OutOfBoundsException out of range, TypeError on a bad offset type, Error on $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)");
  • Float32Array stores and returns float32-rounded values;
  • isset is true for any in-range index, unset zeroes 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).
  • SplFixedArray on the buffer; existing cases plus dump/serialize/clone/subclass/element-destructor parity with php.
  • Manticore\Ds\* classes; every case prints the same bytes under php + 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 Int32Array is no slower than over list<int>.
  • No leak over 1M-operation loops; object elements destructed exactly when php destructs them.
  • docs/ds.md describes 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

  1. added
    enhancement
    on Oct 5, 2026
  2. blacktrs commented on Oct 5, 2026

    @blacktrs
    ContributorAuthor

    Progress on branch ds (not merged):

    Remaining: SplFixedArray on 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.

  3. blacktrs commented on Oct 5, 2026

    @blacktrs
    ContributorAuthor

    Branch ds at dab4f498 (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 #N and a promoted case): #95 erased ArrayAccess element write, #94 JsonSerializable (new json_fn descriptor 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.

  4. blacktrs commented on Oct 6, 2026

    @blacktrs
    ContributorAuthor

    Branch ds-own at bdfad098 = this work + main b40d04be (ownership, #15). Not merged. macOS AOT suite 1677 passed / 0 failed at a71240ca; Linux and difftest not run on this head.

    Done since the last note:

    • SplFixedArray on the native CELL buffer behind a compiler-owned hidden slot (drop / clone / property walks), inline $f[$i] read and write.
    • isset / empty / offsetExists with a non-numeric string offset throw TypeError as php does.
    • 1M-operation leak-loop case over Int32Array, Float64Array and SplFixedArray with object elements: flat.

    php-cs-fixer fork (Tokens extends SplFixedArray), same fork source built by main b40d04be (array-backed SplFixedArray) and by ds-own (native buffer), macOS arm64:

    corpus / rules mode metric main ds-own delta
    manticore src (259 files), @PSR12 sequential, 3 interleaved runs user CPU 11.69 s 11.34 s -3%
    wall 12.05 s 11.84 s -2%
    peak RSS ~220 MB ~223 MB same
    symfony (9200 files), @Symfony parallel, median of 3, machine busy user+sys CPU 147.1 s 130.8 s -11% (noisy)
    peak tree RSS 3307 MB 2595 MB -22%

    --dry-run --diff output on the src corpus 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 SplFixedArray storage but in how Tokens lays out its data — tracked in the follow-up.

    Found on the way, not caused by this branch (reproduces with the main build too): on the symfony corpus 65 files fail with InvalidArgumentException: 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.

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

    enhancementfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions