Skip to content

[perf] Speed up ExplicitReturnNullRector with single scoped walk and cheap bail before return type inference - #8656

Merged
TomasVotruba merged 1 commit into
mainfrom
tv-perf-explicit-return-null
Oct 15, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
tv-perf-explicit-return-null

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Performance refactor of ExplicitReturnNullRector. No behavior change - all 32 fixtures pass unchanged.

The rule's only heavy operation is ReturnTypeInferer::inferFunctionLike(), which resolves class reflection and the native type of every return expression. It previously ran on every method/function that lacks a native return type.

What changed

  • A single scoped traversal now collects everything the rule needs up front: whether a goto exists, whether any value-returning return exists, and the bare return; nodes to rewrite. This replaces a separate findFirstInFunctionLikeScoped() goto scan plus a second traverseNodesWithCallable() pass.
  • Cheap guards hoisted above the inference call:
    • bail on bodyless methods (stmts === null, abstract/interface);
    • bail when there is no value-returning return - the inferred type can then never be a value|void union, so the rule could never fire.

Inference is only reached once both cheap conditions hold.

Measured effect

Rule run with per-call counters over a corpus of untyped methods:

  • 1000 untyped void-only methods (no value return): inferFunctionLike calls 1000 -> 0.
  • 20 methods that genuinely return value|void (rule fires): 20 -> 20, no false skip.

The expensive inference is now avoided entirely on the common legacy shape, while the firing path is untouched.

…lk and cheap bail before return type inference
@TomasVotruba TomasVotruba changed the title [CodeQuality] Speed up ExplicitReturnNullRector with single scoped walk and cheap bail before return type inference [perf] Speed up ExplicitReturnNullRector with single scoped walk and cheap bail before return type inference Oct 15, 2026
@TomasVotruba
TomasVotruba merged commit e16e845 into main Oct 15, 2026
49 checks passed
@TomasVotruba
TomasVotruba deleted the tv-perf-explicit-return-null branch October 15, 2026 10:52
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