-
Notifications
You must be signed in to change notification settings - Fork 546
Made closure php-src cachable #4724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.1.x
Are you sure you want to change the base?
Conversation
|
@iluuu1994 could you have another look at this PR. intention is, that php-src will be able to cache the closure, while it could not before. does this make sense like that and will it have the expected behaviour? |
|
regarding other examples: do you think it would make things faster if we transform such closures into e.g. __invoke classes or similar? Some callsites of |
|
Basically the requirement for being cached is that it has to be stateless. So:
In some cases it might be worth considering if a plain old function makes more sense |
dktapps
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR changes look good to me wrt. cacheability, although it might be worth considering if a private static function would make more sense than a closure for larger functions
|
@dktapps thank you
can this be measured somehow? |
|
This pull request has been marked as ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think it would make things faster if we transform such closures into e.g. __invoke classes or similar?
The static closure patch avoids an allocation each time the closure is used (not called). Allocating an object rather than a closure shouldn't make a significant difference, unless you can avoid allocating the object each time. For closures that have usees, that might be easier to achieve with __invokeable objects with properties I suppose.
src/Command/CommandHelper.php
Outdated
| $stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output))); | ||
|
|
||
| $errorOutput = (static function () use ($input, $output): Output { | ||
| $errorOutput = (static function ($input, $output): Output { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of this static closure in the first place? The other case has an include (so possibly there to hide local vars?) and does some control flow. Why not just drop the closure entirely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point. removed this closure.
thank you.
Not sure what you mean by this, but for an explicitly static closure it just can't have used vars or local static vars. The PR you mentioned on php-src is mostly focused on inferring static when it's not explicitly specified. |
does this means, after making a closure cachable we might see a runtime improvement or memory_get_peak improvement? |
| path: src/Analyser/RuleErrorTransformer.php | ||
|
|
||
| - | ||
| rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes I wonder why this errors show up now, but it seems they were not reported when 1:1 the same code was part of MutatingScope class. I could not find ignore-error rules or something which would swallow them either.
besides that, I think this PR is ready to merge.
I would do more *Traverser-extractions in separate PRs, as we have too many of them.
It depends on how hot the code is, you might see a performance improvement. I wouldn't expect to see much difference in memory usage |
That's the idea. It should be beneficial for code that creates closures in a loop. Of course, you could already avoid this performance hit by moving the closure out of the loop, but the idea here is of course is that it happens automatically. Regarding |
see #4701 (comment)