Skip to content

[perf] Decide named constructor from node visibility before reflection - #8721

Merged
TomasVotruba merged 1 commit into
mainfrom
tv-hoist-named-constructor
Oct 19, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
tv-hoist-named-constructor

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

What

ClassMethodManipulator::isNamedConstructor() resolved the class reflection up front for every __construct, then looked at visibility. But visibility is local to the node:

  • a private __construct is always a named constructor,
  • a public __construct is never one (the only reflection-dependent branch, isFinalByKeyword, can only return false for it anyway),
  • only a protected __construct still needs reflection, to read the class isFinalByKeyword().

So the check now answers private and public (the common cases) from the node and resolves reflection only for the protected case.

Bench

bench/named_constructor_visibility_bench.php - 150 classes with ordinary public constructors, isNamedConstructor() driven over each (9000 calls), CPU time (user+sys), warmup excluded.

before (reflection first): 3.841 us/call
after  (visibility first):  2.753 us/call   (~1.4x)

Modest - the resolve is scope-backed, not full native reflection - but a real, repeatable CPU drop on the common path.

Behaviour

Equivalent for all valid code. The one edge: a private __construct whose enclosing class cannot be resolved previously returned false (via the null-reflection bail) and now returns true. A __construct always has a resolvable enclosing class, so this only differs for degenerate input. The sole caller (RemoveEmptyClassMethodRector) test suite is green (33 tests).

@TomasVotruba
TomasVotruba merged commit 1779451 into main Oct 19, 2026
49 checks passed
@TomasVotruba
TomasVotruba deleted the tv-hoist-named-constructor branch October 19, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant