Skip to content

Performance sweep of basic functions: quadratic behaviour removed, O(1) array_shift, ~20 bugs fixed - #333

Merged
blacktrs merged 69 commits into
mainfrom
perf/basic-fns-sweep
Oct 18, 2026
Merged

blacktrs merged 69 commits into
mainfrom
perf/basic-fns-sweep

Conversation

@blacktrs

Copy link
Copy Markdown
Contributor

Summary

A sweep of ~150 basic string/array/SPL/date/regex functions at 20k and 80k inputs (tools/bench/basic_fns_sweep.php, added here) for quadratic behaviour and for the gap to php 8.5, plus the bugs the sweep and its agents hit on the way. Quadratic first, slow-linear second, memory third.

Quadratic behaviour removed

Case Before (80k) After
array_diff / array_intersect / *_key 1.1 s – 15 s 7 – 11 ms (hash sets)
ArrayObject / ArrayIterator foreach, element read of a mixed property 1.7 s at 20k (O(n) array copy per read) 0.8 ms
fwrite to php://memory 120 ms at 40k 0.9 ms
.= through a string &$s parameter 12.7 ms at 40k 0.4 ms
array_shift / array_unshift loop x17 x3.7 (O(1) amortized, ABI v22)
stripos($s, $n, $offset) in a loop 3 ms 23 us
similar_text on periodic input 770 ms 4 us

What is still quadratic is quadratic in php too (array_merge in a loop, mb_substr in a loop, prepend, array by value, substr_replace).

Slow-linear functions brought to php speed (selection)

in_array / array_search (7-95x), ksort / asort (native stable assoc sort), urlencode / urldecode, addslashes, substr_count, str_replace family, str_word_count, hex2bin, date() (10x), mb_* upper/lower/len, version_compare, levenshtein, ctype_*, html_entity_decode, array_fill, array_column, str_split, strtr with an array, preg_* (in-place ovector, JIT, chunked preg_quote), SPL heaps, and calling a function by runtime name (22 us -> 5 ns per call with an inline cache).

Final sweep: 109 of 151 cases within 1.2x of php, 87 faster; peak RSS never above php except parse_str. Still >= 2x slower: base64 (needs SIMD), nested_arr_read_modify, http_build_query, str_ireplace, ArrayObject, strtotime.

Bugs fixed (found on the way)

Fixes #161 Fixes #209 Fixes #259 Fixes #283 Fixes #298 Fixes #300 Fixes #304 Fixes #305 Fixes #306 Fixes #307 Fixes #308 Fixes #309 Fixes #310 Fixes #311 Fixes #312 Fixes #313 Fixes #314 Fixes #315 Fixes #316 Fixes #317

Notable: a float passed to an int parameter arrived as its raw IEEE bits (#298); array_merge(...$l) emptied the source's inner arrays (#309); a bare array parameter written with another element type stored raw pointers (#307); numeric strings ordered as bytes by < and sort (#283, #308); array_shift / array_pop leaked and kept destructors waiting (#314, #315).

Partly fixed: #332 (by-name call of a stdlib function with an object argument no longer reads the pointer as string bytes; the TypeError mismatch of the lenient cell-to-string cast remains). New bugs found and filed, not fixed here: #319-#325, #329, #332.

Risk and what was run

  • Memory ABI v22: array_shift / array_unshift slide the array header (ARRAY_HEAD_GAP_OFFSET); every free / realloc site recovers the block start. docs/design/memory-abi.md updated.
  • Ownership changes: array_pop / array_shift results, builtin-argument element sharing, SELF_REBUILD store mode, cell alias of a property read retained instead of cloned.
  • PCRE JIT is new: unverified on musl / Alpine (falls back to the interpreter silently if JIT is unavailable).
  • Run on macOS arm64: full bash tests/aot/run.sh -j 0 = 2220 passed, 1 failed (async_socketpair_blocking_read, passes alone 3/3 - load flake), 2 skipped (no expected output); xfail.sh 0 XPASS; symfony-console (15/22 invocations identical to Zend, the other 7 are 3 program-name artifacts and the known JSON/XML descriptor bugs symfony-console: list --format=xml segfaults in McXmlDoc::newNode #296 / symfony-console: JSON descriptor returns empty nested arrays (help) and crashes (list, help list) #297, same on main); symfony-app builds, cold boot without var/ serves 200 / 404 in prod and dev, RSS flat over 600 requests.
  • Not run: Linux gate, amd64, difftest, selfhost fixpoint. The Linux gate is owed before merge (ABI change, JIT, ownership).

…m) (80k ints: 1.1 s -> 7 ms); add the basic-functions sweep probe
…Iterator foreach 1.7 s -> 0.8 ms at 20k) and O(n) php://memory append
…d instead of a strcmp chain (20k calls 22 ms -> 1.2 ms)
…ion lazily (2.2 us -> 0.34 us per call); pad year to 4 digits in Y and r
…tr_replace's worker binary-safe (substr_count 16x faster)
…n and mb_substr seek without a call per ASCII byte (mb_strlen+substr+upper 2.3 ms -> 0.05 ms at 20k)
…sures (closure-bound array_search over ints 8.9 ms -> 0.06 ms); add the native assoc-sort primitive
…rt/krsort/asort/arsort (ksort 22.6 ms -> 3.3 ms at 20k)
…form ranks, trailing dots, overflow, ValueError on a bad operator (5k calls 5.0 ms -> 1.0 ms)
…-string arrays (ints 1.6 ms -> 0.7 ms at 20k)
…n (native hex check replaces a per-byte strspn)
…, cached POSIX-rule instants and last transition interval (3.1 us -> 1.4 us per strtotime)
…te skip, bound prune): 250x250 0.9 ms -> 0.28 ms
…ode when nothing escapes, scalar fast path in array_merge_recursive; match php for an empty separator and an empty parse_str name
…ipos in a loop 60x faster, str_ireplace native)
…of byte-wise appends (strip_tags 2.5x faster)
…array (spread of $l[k][] arrays emptied the source, Fixes #309)
…l column values kept (#319 filed for is_object on enum cases)
…all per comparison, parallel data/priority arrays
… and variadic functions callable by name (Fixes #317)
…s, ASCII icmp (func_str_dispatch 80k calls 9800 -> 430 us)
… channel; own the rebuild

A bare `array` param specialized from its callers (vec[int], vec[string], ...) took
stores of another element kind raw into the caller's typed buffer. The element
scans now widen such a param (and a closure's by-value capture) to a cell element
the way they widen a local literal: by-value is rebuilt at entry, by-ref is retyped
and its callers follow. The entry rebuild is a new SELF_REBUILD ownership mode that
drops the owned predecessor, so the converted copy no longer leaks its elements.

Fixes #307, Fixes #259, Fixes #161
…nt meets a scalar parameter: it was invalid IR that broke the symfony-app build
…l gets: an object or array arrived as a raw word read as string bytes (#332, partly)
…mpiler rejects the param-aliasing form (bootstrap-from-pin)
@blacktrs
blacktrs merged commit 2af5bae into main Oct 18, 2026
2 of 8 checks passed
@blacktrs
blacktrs deleted the perf/basic-fns-sweep branch October 18, 2026 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment