Skip to content

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

Merged
TomasVotruba merged 1 commit into
mainfrom
tv-perf-return-shape-before-override-guard
Oct 15, 2026
Merged

TomasVotruba merged 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.

@TomasVotruba TomasVotruba changed the title [TypeDeclaration] Speed up ReturnTypeFrom* rules by checking return shape before class method override guard [perf] Speed up ReturnTypeFrom* rules by checking return shape before class method override guard Oct 15, 2026
@TomasVotruba
TomasVotruba merged commit 2e19201 into main Oct 15, 2026
49 checks passed
@TomasVotruba
TomasVotruba deleted the tv-perf-return-shape-before-override-guard branch October 15, 2026 10:46
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