Skip to content

perf(array): native scans for in_array and implode over typed vecs - #162

Merged
blacktrs merged 7 commits into
mainfrom
feature/array-fast-paths
Oct 10, 2026
Merged

blacktrs merged 7 commits into
mainfrom
feature/array-fast-paths

Conversation

@blacktrs

@blacktrs blacktrs commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What

in_array and implode over a statically typed vec went through generic paths: a synthesized foreach with a per-element retain/release and a __mir_str_eq_ns call chain for strings, and a per-element snprintf plus temporary string for floats. They now compile to runtime scans that read the element words directly. Also fixes bench/run.sh on Linux.

Changes

  • in_array, strict string in string[] — __mir_array_in_str. Compares pointer, then header length, then bytes; no retain/release of the element. A loose string search stays on the synthesized loop because "1e1" == "10" compares numerically.
  • in_array, int / float / bool in a vec of the same kind — __mir_array_in_word. A raw packed buffer is swept eight words at a time with no branch inside the group; other buffer shapes use a one-element loop, unboxing each element by kind when the buffer is cellified. Floats compare as doubles (NAN matches nothing, 0.0 == -0.0). Mixed types (e.g. an int needle in a float[]) keep the synthesized loop.
  • implode over vec[float] — __mir_array_implode_float. A float whose %.14G form is plain digits (integral or .5, below 1e13) is written straight into the result buffer; everything else goes through __mir_float_to_str, so the text is what echo prints. A non-float buffer falls back to __mir_array_implode_cell.
  • bench/run.sh — times cases without /usr/bin/time (falls back to EPOCHREALTIME / perl) and prints - for RSS when the host cannot report it. Previously the awk expressions broke and the table showed no numbers on Linux. Adds bench/cases/in_array_int.php.

The shape selection is shared: InlineClosures::nativeScanShape decides in both the lowering pass and the builtin emitter.

Results

Linux x86_64, best of 3, bench/run.sh:

case before after php
in_array (200-string vec, strict) 0.19 s 0.073 s 0.129 s
implode_int (2000 ints + 2000 floats) 0.21 s 0.035 s 0.206 s
in_array_int (1000-int vec) ~0.107 s ~0.05 s 0.25 s

The in_array_int "before" figure is from a separate run, not a direct A/B.

Floats that are not integral or .5 (for example $i / 3) still format through snprintf and are not faster: 0.21 s against 0.18 s for php on the same case, output identical. A 14-significant-digit formatter would be the next step there.

Tests

  • New: in_array_strict_str, in_array_scalar_vec, implode_float_vec. Each compares against php output (hit/miss, NUL bytes, NAN, -0.0, PHP_INT_MIN/MAX, hashed values after unset, -0, INF, 1.0E+25, buffer regrowth, mixed arrays).

Not included

Object and enum elements (an enum element in a cell buffer is the case singleton, not an ordinal) and a hash index for repeated lookups on one array.

@blacktrs blacktrs changed the title Feature/array fast paths perf(array): native scans for in_array and implode over typed vecs Oct 9, 2026
@blacktrs
blacktrs merged commit b21c249 into main Oct 10, 2026
15 of 16 checks passed
@blacktrs
blacktrs deleted the feature/array-fast-paths branch October 10, 2026 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant