Repository navigation
perf: a generic static access calls the engine's statics directly - #153
Merged
Merged
Conversation
Every access of a generic class's static called its hidden `#entry`, which called `\Sharp\Internal\statics` for the entry of its type arguments. That frame cost 1.78 to 2.44 times master's access before #142. A static whose initial value runs as code also bound the entry on every call, and its check cost 138 instructions a compound assignment. Every access now calls `\Sharp\Internal\statics(C::class, <the text of its type arguments>)` itself. The text is `SHARP_TYPE_OF` over the whole list, the engine's own spelling, so `#0` inside the class and `App.Order` outside it give one key. An access costs 0.90 to 1.04 times master's before #142. - The call that adds an entry runs the class's hidden `#init` with the type arguments the key spells. `#init` sets each initial value that runs as code, and a throw there leaves the entry as it is. - A use that reads a static such a throw can leave unset passes its name, and the engine throws PHP's own "Typed static property App\Counter::$made must not be accessed before initialization" when the entry does not hold it. A compound assignment, `++` and `--` read first, so they pass it too. - `#entry`, `$created`, the bridge's guard, the `$statics#N` names and `PlaceUse::Update` are gone.
heyJordanParker
added a commit
that referenced
this pull request
Oct 16, 2026
Problem #153 landed while the first merge was pushed, and #136 conflicted with master c4adf74 in two files, which `gh pr update-branch` cannot resolve. Solution - docs/architecture/slices/06-properties.md: a static runs its initial value once per app, in #153's hidden `#init`. - ext/sharp/sharp_arginfo.h: regenerated from the merged stub with build/gen_stub.php, so it holds #153's stub and Sharp\Internal\runScoped. docs/architecture/slices/06-properties.md ext/sharp/sharp_arginfo.h
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.
Fixes: #142's generic static access, which cost 1.78 to 2.44 times master's access before #142.
Problem
#142 gave a generic class with static fields a hidden static method,
#entry, which every access called with its type arguments.#entrycalled\Sharp\Internal\staticsfor the entry of those type arguments. Its frame cost 1.78 to 2.44 times master's access before #142, in every shape. A class with an initial value that runs as code also bound the entry to a reference on every call. A static that such a throw can leave unset added a guard in the bridge, which cost 138 instructions a compound assignment, through a hidden$statics#Neach update bound.Solution
Every access now calls
\Sharp\Internal\statics(C::class, <the text of its type arguments>)itself, and one mechanism replaces#entry,$createdand the guard. The design is indocs/architecture/slices/11-generics-rest.md, "Generic statics".SHARP_TYPE_OFover the whole list of its type arguments, so#0inside the class andApp.Orderoutside it give one key.#init. A class with an initial value that runs as code declares it.\Sharp\Internal\staticscopies the defaults, then calls#initwith the type arguments the key spells, which sets each initial value that runs as code, once per type argument. A throw there reaches the access and leaves the entry as it is, so nothing runs again.\Sharp\Internal\statics(C::class, …, 'made')['made'], and the engine throws PHP's own "Typed static property App\Counter::$made must not be accessed before initialization" when the entry does not hold it. A compound assignment,++and--read first, so they name it too.=and??=set it.\Sharp\Internal\staticstakes?string $field = nullin place of?bool &$created = null.#entry,$created, the bridge'sarray_key_existsguard, the$statics#Nnames andPlaceUse::Update.ZEND_ACC_RETURN_REFERENCEin the bridge lost its only user.The cost of an access
Instructions retired per access, from
/usr/bin/time -l, release builds of each head configured--disable-all --disable-cgi --disable-phpdbg --without-pear, the interpreter, 10,000,000 passes, median of 3 runs, every head measured the same hour, each with its ownmago. Per access is the loop's instructions less the same loop without the static, divided by the passes.24bd9909cf. It refused a static whose initial value runs as code, somadeis compared with itscount.830d4aac13, with feat: every rule of section 11, generics, compiles, refuses or runs as the spec says #142's#entry.Every shape is within 2x master before #142.
countwith every initial value constant, all three ofcountbeside a code initial value, and=ofmade.madeby 4%, and+=ofmadeby 3%. Namingmadecosts 23 instructions a read and 22 a compound assignment.#entry, at 0.40 to 0.56 times its cost.Tests
One test per rule, with forms as rows. Each new row was seen failing before its code.
Zend/tests/sharp/type_arguments_call_statics.phptholds the rows of a static a throw left unset. Its read,+=and++throw PHP's error, of a field and through accessors, until a write sets it.OutOfRangeException: Undefined array key "broken".+=and++passing no field, both warnedUndefined array key "broken"and wrote a value built from null.crates/sharp-bridge/tests/lower.rspins the trees:a_static_method_of_a_generic_class_takes_the_class_type_arguments_as_its_owna_generic_class_runs_its_statics_initial_values_and_accessors_with_its_type_arguments, with#init, and no#initfor a class whose initial values are all constanta_read_of_a_generic_static_whose_initial_value_runs_as_code_names_it_for_the_engine_to_checka_write_through_a_class_value_writes_the_static_field_of_the_type_arguments_it_carriesRun locally on this head:
type_arguments_call_statics.phpt,type_arguments_preload.phptandstructs.phptpassed 50 times each withUSE_ZEND_ALLOC=0 MallocScribble=1, without opcache and with it.type_arguments_call_statics.phpt's script printed its whole expected output on classes preloaded fromtype_arguments_calls.inc, withCalls\Fragileamong the 47 preloaded classes.magoworkspace test passed: 179 suites.cargo fmt --checkandcargo clippy --workspace --all-targets -D warningsare clean.