NodeScopeResolver: Prevent repetitive union of static types#4841
NodeScopeResolver: Prevent repetitive union of static types#4841ondrejmirtes merged 5 commits intophpstan:2.1.xfrom
Conversation
src/Analyser/NodeScopeResolver.php
Outdated
| $this->earlyTerminatingMethodNames = $earlyTerminatingMethodNames; | ||
|
|
||
| $this->nonIntKeyOffsetValueType = TypeCombinator::union( | ||
| new ArrayType(new MixedType(), new MixedType()), |
There was a problem hiding this comment.
Would be better if it was lazily initialized with ??= right before usage.
There was a problem hiding this comment.
I think the NodeScopeResolve is created once for the whole analysis and its pretty likely that we hit the path on usage.
so I think there is not much to be saved. only difference would be readability, as we would move code more to a single place (but we will also loose readonly attribute)
There was a problem hiding this comment.
so its a matter of code readability, not performance.
if you prefer I can move it to a ??=
There was a problem hiding this comment.
I worry about doing things in constructor. For example if NodeScopeResolver depended on ReflectionProvider or vice-versa, doing something related to Type objects in constructor could lead to infinite recursion.
There was a problem hiding this comment.
good points, didn't think about that.
|
Thank you! |
|
Maybe I'm missing something but can't the If not and if we're starting caching some often used Union to reduce the number of call of |
|
It's true it could be new UnionType, but this will be literally a single call during the analysis so it's not going to cost us any time. |
I thought that without the need of using But I'm not familiar at all with perf/memory costs and never work to improve a codebase about it, so I rely on your and staabm's experience. |
I wouldn't say the goal is to save all the the change was done to get rid of a hot path. it does not mean similar pattern in other less-hot path should be treated the same. |
before PR
after PR -
unionno longer appears as one of the callees