Skip to content

perf: a generic static access calls the engine's statics directly - #153

Merged
heyJordanParker merged 2 commits into
masterfrom
fix/generic-statics-init
Oct 16, 2026
Merged

heyJordanParker merged 2 commits into
masterfrom
fix/generic-statics-init

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

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. #entry called \Sharp\Internal\statics for 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#N each update bound.

Solution

Every access now calls \Sharp\Internal\statics(C::class, <the text of its type arguments>) itself, and one mechanism replaces #entry, $created and the guard. The design is in docs/architecture/slices/11-generics-rest.md, "Generic statics".

  • The key is the engine's own spelling. The access passes SHARP_TYPE_OF over the whole list of its type arguments, so #0 inside the class and App.Order outside it give one key.
  • The call that adds an entry runs the class's hidden #init. A class with an initial value that runs as code declares it. \Sharp\Internal\statics copies the defaults, then calls #init with 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.
  • A use that reads a static a throw can leave unset names it, \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\statics takes ?string $field = null in place of ?bool &$created = null.
  • Gone: #entry, $created, the bridge's array_key_exists guard, the $statics#N names and PlaceUse::Update. ZEND_ACC_RETURN_REFERENCE in 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 own mago. Per access is the loop's instructions less the same loop without the static, divided by the passes.

                                       before #142    master    this PR   ratio to before #142
count, every initial value constant
  read                                       462.2     867.6      455.3   0.99
  =                                          460.2     865.5      453.3   0.99
  +=                                         519.3     923.0      512.5   0.99
count, beside a code initial value
  read                                       462.2    1076.0      455.4   0.99
  =                                          460.2    1074.1      454.5   0.99
  +=                                         519.3    1134.2      514.3   0.99
made, whose initial value runs as code
  read                                       462.2    1083.3      478.5   1.04
  =                                          460.2    1036.1      415.7   0.90
  +=                                         519.3    1268.1      536.0   1.03

Every shape is within 2x master before #142.

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.phpt holds 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.
    • With the engine's field check off, the first read reached OutOfRangeException: Undefined array key "broken".
    • With += and ++ passing no field, both warned Undefined array key "broken" and wrote a value built from null.
  • crates/sharp-bridge/tests/lower.rs pins the trees:
    • a_static_method_of_a_generic_class_takes_the_class_type_arguments_as_its_own
    • a_generic_class_runs_its_statics_initial_values_and_accessors_with_its_type_arguments, with #init, and no #init for a class whose initial values are all constant
    • a_read_of_a_generic_static_whose_initial_value_runs_as_code_names_it_for_the_engine_to_check
    • a_write_through_a_class_value_writes_the_static_field_of_the_type_arguments_it_carries

Run locally on this head:

  • The static tests type_arguments_call_statics.phpt, type_arguments_preload.phpt and structs.phpt passed 50 times each with USE_ZEND_ALLOC=0 MallocScribble=1, without opcache and with it.
  • Under preloading, type_arguments_call_statics.phpt's script printed its whole expected output on classes preloaded from type_arguments_calls.inc, with Calls\Fragile among the 47 preloaded classes.
  • Every engine test, 296 of 296, passed in each of these modes:
    • without opcache
    • with the file cache primed and used
    • twice per process
    • with each of the 16 optimizer passes alone
    • under the tracing and function JITs
  • Every mago workspace test passed: 179 suites. cargo fmt --check and cargo clippy --workspace --all-targets -D warnings are clean.
  • CI runs the census, the differential run and the spec run.

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
heyJordanParker merged commit c4adf74 into master Oct 16, 2026
20 checks passed
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
heyJordanParker added a commit that referenced this pull request Oct 16, 2026
sharp_arginfo.h is regenerated from the merged stub, which holds #153's statics signature and this branch's
testModule. Two #153 bridge tests keep #153's names and expectations, with Order and Tally in files of their own.
@heyJordanParker
heyJordanParker deleted the fix/generic-statics-init branch November 3, 2026 22:08
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