Repository navigation
Perf: function_exists binary search, reflection without quadratic scans or per-call rebuilds, O(log n) class-name dispatch - #562
Merged
Conversation
…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
…_vars and get_class_vars getProperties() now drops them as php does; the two instance-layout walkers read them through ReflectionClass::__props(null, true).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_existswalked the whole name table (~1.9kstrcmps). The table is now sorted and binary-searched, asmethod_existsandis_aalready were. symfony-appGET /, 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 withnew ReflectionMethod($class, $name), whose constructor found the row three times by linearstrcmpscan. 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 ofnew ReflectionClassper call.new ReflectionClass(...)no longer does an extra__MC_ClosureShapelookup on every call;getParentClass,getConstructor,getMethod,getPropertypass the handle/row they already have;getShortNameno longer allocates per character.getMethods()/getProperties()no longer list a parent's private members, andReflectionMethod/getMethod/getPropertythrow 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 ofif ($cls === "...")over every class, i.e. O(classes). They now walk a balanced tree keyed bycrc32($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.
getMethods(80 rows)getProperties(80 rows)getDocComment(method)getShortNamenewInstanceWithoutConstructor(3000 classes)unserialize(3000 classes)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:refl76/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 underwrkwith 1 and 4 workers.Docs
Nothing user-facing changes: no new API, and
docs/mysqli.md's note that the private$__mcshows inReflectionClass::getProperties()still holds for the declaring class.docs/builtins.mdregenerates 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(LowerPreludeis compiler code).