Skip to content

Manticore\Ds, next parts: check hoisting, typed element reprs, growable and byte buffers, SPL family on the native buffer #101

Closed

Description

@blacktrs

Problem

#92 delivers the native buffer runtime, SplFixedArray on it and the Manticore\Ds fixed-width typed arrays. Measured on the php-cs-fixer fork, swapping the storage under SplFixedArray gives about 3% CPU and 22% RSS: a faster container does not help a program whose hot data is still a cell per element. The containers have to cover more shapes of data, and the compiler has to remove the per-access cost that is left.

Proposal

Next parts of the Ds line, each small enough to ship alone, ordered by usefulness to real applications (first consumer: the php-cs-fixer fork). Status after PR #108 (merged 2026-10-07):

  1. php-cs-fixer fork on typed arrays — sandbox only, not a deliverable. A/B on the fork: about -3% CPU, -22% RSS; SplFixedArray storage is not the lever. No further work planned.
  2. Growable typed vector — done. No separate class: every Manticore\Ds\*Array has push(), pop() and $a[] = $v. 10M appends 600 ms → 27 ms (array 51 ms).
  3. Rest of SPL — done for SplDoublyLinkedList / SplQueue / SplStack (O(1) shift, Zend LIFO/modes) and SplHeap / SplMinHeap / SplMaxHeap / SplPriorityQueue; both families fuzzed against php (5000 / 6000 rounds, no mismatch). Storage is a PHP list, not the native buffer (a cell buffer buys nothing there). ArrayObject / ArrayIterator stay array-backed. Not emulated: mutating a list while a cursor is live, the Serializable methods of the list, var_dump of a heap subclass with own properties.
  4. Access-cost leftovers — mostly done: length and handle loads hoisted out of counted loops, inline access for a receiver held in a property of a local or a static property (10M get/set: local Int32Array 5.4 ms, property-held 7.8 ms, was 168 ms). Loop versioning of the bounds compare is dropped: measured on Int32Array, a read-only loop is already guarded once and vectorised by LLVM, and a read-write loop is bound by the element range check (a ValueError contract), not by the bounds compare (2 of ~11 instructions per iteration). SplFixedArray TBAA is dropped: measured 5.7 ns per $a[$i] = $a[$i] + $i on 1M elements, already below a PHP array (6.8 ns); its elements are cells, so the remaining cost is cell traffic, not the holder or length loads. Inline stores now also take bitwise values (& | ^ << >> ~; 16.7 → 0.12 ns per element on Int32Array).
  5. Whole-array operations — sum, min, max, equals and apply(callable) (in-place map; 6.6 ns per element, array_map 10.3) done.
  6. Byte buffer and views — ByteBuffer with endianness-aware typed reads/writes done. Views moved to Manticore\Ds: non-owning subarray views over a shared buffer #272 (no consumer: websocket/http use Buffer\ByteBuffer, a string + cursor).
  7. #[TypeDef(repr)] element types — done for scalar reprs (i8 … u32, f32, f64): a typed array bound to a TypeDef class via docblock, with a width check on store. Enum-backed ints and packed structs moved to Manticore\Ds: #[TypeDef] element types for enum-backed ints and packed structs #273.
  8. UInt64Array — done. Typed string array moved to Manticore\Ds: typed string array on one byte buffer #271.
  9. Polyfill package — generator exists (php tools/ds_polyfill.php). Publishing manticorephp/ds is dropped from this issue: a full polyfill is unlikely to be wanted, stubs are the probable shape, and superset features need not run under Zend.

Out of scope

Hash maps and sets with typed keys, persistent / immutable collections, concurrent containers. (Native Map / Set / Vec are tracked separately.)

Acceptance criteria

  • Each part lands with cases that print the same bytes natively and under php where an oracle exists (SPL parts: plain php; Ds classes have no Zend oracle).
  • Part 4: IR of a counted Int32Array read loop has one length guard and no per-iteration bounds branch (LLVM hoists it); the write loop keeps the element range check by contract.
  • docs/ds.md covers the new classes (UInt64Array, ByteBuffer, #[TypeDef] binding, polyfill).
  • Linux suite green for the ds / SplFixedArray cases (confirmed by CI).

Activity

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

    @blacktrs
    ContributorAuthor

    Progress on branch ds-own at e894f8b9 (not merged; macOS AOT suite 1684 passed / 0 failed; Linux not run):

    • Part 2, growable — no separate vector class: the buffer already doubles its capacity, so every Manticore\Ds\*Array got push(), pop() and $a[] = $v (this reverses the Error on [] from Native fixed-layout containers: SplFixedArray on a native buffer and Manticore\Ds typed arrays #92). The append is done in place while the buffer has room: 10M appends 600 ms → 27 ms (array 51 ms).
    • Part 3, SPL — SplHeap, SplMinHeap, SplMaxHeap, SplPriorityQueue did not exist; added with Zend's sift order, corruption protocol, dump and serialization. SplDoublyLinkedList / SplQueue / SplStack rewritten: O(1) shift / dequeue (was array_shift), LIFO offsets, add(), frozen stack / queue modes, a cursor that is invalid until rewind(). Both families were fuzzed against php's own classes (5000 and 6000 random rounds: operations, exceptions, var_dump, print_r, serialize, iteration) with no mismatch. Storage is a PHP list, not the native buffer — a cell buffer buys nothing over list<mixed> here.

    Not emulated: mutating a list while a cursor is live (php keeps a node pointer); the Serializable methods serialize() / unserialize() of the list; var_dump of a heap subclass with its own properties lists them after the heap internals. ArrayObject / ArrayIterator stay array-backed by nature.

    Fixed in the compiler on the way: var_dump marks protected / private properties; var_dump / print_r read a mangled __debugInfo key; __unserialize receives int keys as ints; a prelude file's own calls now pull in the array-function prelude (the array_slice / array_splice undefined-function trap behind SplQueue).

  3. blacktrs commented on Oct 16, 2026

    @blacktrs
    ContributorAuthor

    Done in #108 and the follow-ups on main (apply, inline bitwise stores). Remaining parts split out: #271 typed string array, #272 views, #273 TypeDef enum/packed-struct. Bounds-check versioning, SplFixedArray TBAA and a published polyfill were measured or judged not worth doing; reasoning is in the issue body.

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