Skip to content

Perf: function_exists binary search, reflection without quadratic scans or per-call rebuilds, O(log n) class-name dispatch - #562

Merged
blacktrs merged 6 commits into
mainfrom
perf/fn-exists-bsearch
Nov 3, 2026
Merged

blacktrs merged 6 commits into
mainfrom
perf/fn-exists-bsearch

Conversation

@blacktrs

@blacktrs blacktrs commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Why

Profiling the compiled symfony-app (tools/prof/serve.sh, #561) and a reflection micro-benchmark against php found places where the generated code did work proportional to the size of the program on every call.

Changes

1. function_exists($runtimeName): binary search (EmitLlvmModule)
Symfony asks function_exists('App\Controller\X') for an invokable controller on every request; the answer is a miss and __mir_fn_exists walked the whole name table (~1.9k strcmps). The table is now sorted and binary-searched, as method_exists and is_a already were. symfony-app GET /, one worker, alternating old/new binaries: 12.1k -> 13.6k RPS (+12%).

2. Reflection (prelude/reflection.php)

  • getMethods() / getProperties() were quadratic: each member was built with new ReflectionMethod($class, $name), whose constructor found the row three times by linear strcmp scan. The row is now handed to the constructor, and the by-name path does one lookup.
  • getDocComment() / getStartLine() rebuilt the doc table of the whole class on every call. It is built once per class handle, and the lookups go by handle instead of new ReflectionClass per call.
  • new ReflectionClass(...) no longer does an extra __MC_ClosureShape lookup on every call; getParentClass, getConstructor, getMethod, getProperty pass the handle/row they already have; getShortName no longer allocates per character.
  • Fixes two differences from php: getMethods() / getProperties() no longer list a parent's private members, and ReflectionMethod / getMethod / getProperty throw php's messages (Method Square::nope() does not exist, Class "X" does not exist, Property Square::$nope does not exist).

3. Class-name dispatch (LowerPrelude)
__mc_refl_alloc (newInstanceWithoutConstructor) and __mc_unser_alloc (unserialize) were generated as a chain of if ($cls === "...") over every class, i.e. O(classes). They now walk a balanced tree keyed by crc32($cls) and compare strings only inside a group of equal crc.

Measured

Micro-benchmarks, native vs php (class with 80 methods and 80 properties; a program with 3000 classes). The machine was shared and loaded, so treat small differences as noise.

before after php
getMethods (80 rows) 50.8 us 3.9 us 2.4 us
getProperties (80 rows) 28.8 us 3.3 us 2.4 us
getDocComment (method) 514 ns 64 ns 33 ns
getShortName 62 ns 15 ns 42 ns
newInstanceWithoutConstructor (3000 classes) 4.5-11.8 us 60-83 ns 48 ns
unserialize (3000 classes) 8-12 us 335-435 ns 200 ns

symfony-app startup (~0.12 s) and request throughput are unchanged by the reflection and dispatch parts: they are not on that app's request path. Their effect is for programs that reflect large classes or hydrate/unserialize many objects (Doctrine, serializers, validators).

Tests

New: function_exists_dynamic_sorted, reflection_rows_and_doc_cache (inheritance, filters, repeated doc comments, php's exception texts, private parent members), class_name_dispatch_tree (prefix names, namespaces, case, unknown names); all equal php's output. Also green: refl 76/76, function_exists, class_name, unser, serial, attr, doc, var_export, var_dump, json, clone.
symfony-console: 26 invocations compared with php (list in txt/json/xml/md/raw, help, version, greet with flags, completion bash/zsh/fish, unknown/ambiguous commands) — identical. symfony-app: / 200, unknown route 404, POST / 405, HEAD / 200, no crash under wrk with 1 and 4 workers.

Docs

Nothing user-facing changes: no new API, and docs/mysqli.md's note that the private $__mc shows in ReflectionClass::getProperties() still holds for the declaring class. docs/builtins.md regenerates with unrelated drift (wrapping_* from #556), so it is left alone. Reflection gains three public static internals (ReflectionMethod::$__row, ReflectionProperty::$__row, ::$__h, ReflectionClass::$__h, __docByHandle) in the style of the existing __handle() / __docInfo().

Not run

Full AOT suite, Linux gate, difftest, tools/selfhost_fixpoint.sh (LowerPrelude is compiler code).

…e instead of scanning it (symfony-app +12% RPS)
…nd and cache each class's doc table (getMethods x8, getDocComment x40 on an 80-member class)
…o private parent members in getMethods/getProperties, php's exception texts
… balanced strcmp tree instead of a chain of comparisons (3000 classes: 4-12 us -> 0.4-0.6 us)
… classes: 0.4 us -> 0.06 us); getParentClass/getConstructor/getMethod/getProperty hand the row to the constructor; no per-call ClosureShape lookup
@blacktrs blacktrs changed the title function_exists with a runtime name: binary search instead of a linear scan (symfony-app +12% RPS) Perf: function_exists binary search, reflection without quadratic scans or per-call rebuilds, O(log n) class-name dispatch Nov 3, 2026
…_vars and get_class_vars

getProperties() now drops them as php does; the two instance-layout walkers
read them through ReflectionClass::__props(null, true).
@blacktrs
blacktrs merged commit 0801135 into main Nov 3, 2026
49 checks passed
@blacktrs
blacktrs deleted the perf/fn-exists-bsearch branch November 3, 2026 12:17
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