Skip to content

[TypeDeclaration] Speed up ReturnTypeFrom* rules by checking return shape before class method override guard - #8659

Open
TomasVotruba wants to merge 1 commit into
mainfrom
tv-perf-return-shape-before-override-guard
Open

TomasVotruba wants to merge 1 commit into
mainfrom
tv-perf-return-shape-before-override-guard

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Performance refactor of 8 return-type-inference rules. No behavior change - all 100 fixtures pass unchanged.

Each rule bailed out via ClassMethodReturnTypeOverrideGuard::shouldSkipClassMethod() before checking the cheap structural precondition that there is a single/only return with an expression. That guard is reflection-heavy: it resolves class reflection, walks parent methods and checks type-guarded/abstract/interface state.

The cheap return-shape check (findReturnsScoped() + hasOnlyReturnWithExpr(), a local AST pass that needs no scope) is now done first, so methods that cannot match never pay for the override guard. ReturnTypeFromStrictConstantReturnRector already followed this order - the rest are aligned to it.

Rules updated:

  • NumericReturnTypeFromStrictReturnsRector
  • NumericReturnTypeFromStrictScalarReturnsRector
  • BoolReturnTypeFromBooleanConstAndStrictReturnsRector
  • BoolReturnTypeFromBooleanConstReturnsRector
  • BoolReturnTypeFromBooleanStrictReturnsRector
  • ReturnTypeFromMockObjectRector
  • ReturnTypeFromReturnNewRector
  • ReturnTypeFromStrictTypedCallRector (also moved the cheap check ahead of collectStrictReturnTypes())

Reordering is safe: all moved checks are side-effect-free bail conditions that must all pass to proceed.

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