Skip to content

Performance sweep of string and array functions, with the bugs it found - #274

Merged
blacktrs merged 25 commits into
mainfrom
perf/str-repeat-doubling
Oct 16, 2026
Merged

blacktrs merged 25 commits into
mainfrom
perf/str-repeat-doubling

Conversation

@blacktrs

@blacktrs blacktrs commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What

Performance sweep over the standard string and array functions, plus the bugs the sweep turned up. The sweep measured ~40 functions at 20 000 and 80 000 byte inputs (native vs php 8.5); everything was linear except array_unique, and a dozen were 5-150x slower than php (tracking issue #278).

Speed (arm64 macOS, native, before -> after; php in brackets)

function before after
str_repeat (64 KB, 4-byte pattern) 77 us 1.9 us (0.9)
array_unique (10 000 strings) 528 ms 0.95 ms (0.16)
strrev (64 KB) 248 us 6.6 us
ucwords / nl2br / trim 20.7 / 13.1 / 0.34 ns/B 1.3 / 3.3 / 0.15
base64_encode / decode (20 KB) 134 / 167 us 9 / 20 us
bin2hex / hex2bin 312 / 213 us 13 / 11 us
str_pad / strtr array form 41 / 113 us 2.7 / 30 us
serialize / unserialize (2000 strings) 3.3 / 6.6 ns/B 0.47 / 1.0 (0.56 / 0.9)
in_array miss, typed strings 40 ns/el 3.3
array_slice / array_reverse / array_merge (int list) 10.5 / 14 / 7.8 ns/el ~1
range 6.1 ns/el 1.0 (1.1)
sort 10k strings, near-sorted 780 ns/el 83-145 (120-160)
applyMask in prelude/websocket.php (64 KB) 74 us 5.8 us (php 45.8)

How: str_repeat copies by doubling instead of one memcpy per repetition (this was the whole cost of the websocket mask); array_unique keeps a seen-set; strrev, base64/hex, serialize/unserialize and the list copies are native IR routines, each shipped with a same-named PHP body per the bootstrap rule; sort is a native stable merge sort for int and string lists.

Bugs fixed

Left open (filed)

array_flip and array_splice (#285), sort of numeric strings (#283), range of numeric-string endpoints (#284), unserialize accepting malformed floats (#282). Each has a repro under tests/aot/repro.

Tests

One case per function with sizes around the word and block boundaries (0, 1, 7, 8, 9, 15, 16, 17, 100, 4097), high bytes and NUL, expected output from php. Run filtered: closure (91), str_repeat, array_unique, strrev, ucwords, nl2br, trim, addslashes, base64, hex, str_pad, strtr, string_arg, in_array, array_bulk, range, sort_native, serialize: all pass on macOS arm64. Also built from the pinned release alone (bin/build with no bin/manticore) before the merge with main, then rebuilt after it.

Not run: the whole AOT suite, difftest, the Linux gate, selfhost_fixpoint.sh.

Review notes

  • strtr byte form: main and this branch each added a native version; main's (no allocation when nothing translates) is kept.
  • Two src/ edits worth a look: reprOrHintIr in UnifiedArrayRuntime.php (skips the element walk for raw int/float/bool hints) and the loose string compare in InlineClosures.php.

…cal: fixes double release of a captured array on mutation (Fixes #275)
…cwords in-place (20.7 -> 1.3 ns/B), nl2br chunked appends (13.1 -> 3.3 ns/B)
…umeric needle (typed 40 -> 3.3 ns/el, erased 40 -> 10 ns/el)
…okup tables; base64_decode follows php's loose/strict rules (20 KB: b64 enc 134 us -> 11 us, dec 167 us -> 27 us, bin2hex 312 us -> 15 us, hex2bin 213 us -> 13 us)
… candidate first bytes with strcspn (20 KB: 3-arg 50 us -> 7 us, array 116 us -> 30 us)
…t: 14/10.5/7.8 -> 0.4/1.0/1.0 ns/el; php 2-3); raw scalar element lists skip the retain/release walk
…d or non-hex input, an empty pad or bad pad type, an empty strtr key (Fixes #281)
…em; objects, references and enums fall back to the PHP walker (list of 2000 strings 3.3 -> 0.47 ns/B, ints 4.6 -> 1.1, assoc 5.4 -> 1.7, nested 6.0 -> 0.9; php 0.56 / 1.26 / 3.95 / 1.8) (#280)
…he PHP parser keeps objects, r:/R:, enums and every error path (list of 2000 strings 6.6 -> 1.7 ns/B, ints 9.1 -> 1.0, assoc 11.3 -> 3.4, nested 14.6 -> 2.9; php 0.85 / 1.8 / 3.2 / 1.9); serialize_codec test; 'i:;' is an error (#280)
…s 780 -> 83 ns/el sorted-ish; php 160; shuffled cell strings 1015 -> 262, php 151)
…ted by the element hint the static type implies (closure and nested-array lists stay on the loop, no extra return type); int lists 14/10.5/7.8 -> 0.3/0.2/0.2 ns/el, php 2-3
# Conflicts:
#	src/Compile/Mir/Passes/InferCalls.php
# Conflicts:
#	src/Compile/Mir/Ownership.php
#	src/Compile/Mir/Passes/EmitLlvmBuiltins.php
#	src/Compile/Mir/Passes/LowerFns.php
@blacktrs blacktrs changed the title str_repeat copies by doubling Performance sweep of string and array functions, with the bugs it found Oct 16, 2026
@blacktrs
blacktrs merged commit b394c2d into main Oct 16, 2026
15 of 16 checks passed
@blacktrs
blacktrs deleted the perf/str-repeat-doubling branch October 16, 2026 14:24
blacktrs added a commit that referenced this pull request Oct 16, 2026
….0 (#294)

* Add repros for ReflectionParameter default values missing for trait, interface and prelude methods (#287, #288)

* Synthesize parameter-default factories for trait, interface and prelude methods, in the declaring class scope. Fixes #287 Fixes #288

* Resolve class constants through every extended interface, not only the first (Fixes #287, Fixes #288 end-to-end: symfony-app dev boots)

* Add repro for ReflectionProperty::setValue on prelude-class properties (#289)

* Rebind a closure held in an erased receiver via bindTo instead of routing it through an unrelated __call (Fixes #290)

* Release 0.15.0

* Re-baseline dump-llvm goldens drifted by #274 (stdlib sig change)

* Give throwable properties reflection accessors and report readonly setValue as Error (Fixes #289)

* Recapture CLI behaviour golden after the prelude IR change

* Add repros for method calls on erased receivers routed to an unrelated __call (#292, #293)

* Make the CLI golden test codegen-independent

* Run tests/cli in CI

* Remove the weekly gate.yml workflow; difftest is a manual staleness check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant