Skip to content
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

Make callbacks marked as static #6695

Merged
merged 1 commit into from Apr 9, 2022
Merged

Conversation

SCIF
Copy link
Contributor

@SCIF SCIF commented Oct 19, 2021

Some callbacks converted to callables

@muglug
Copy link
Collaborator

muglug commented Oct 24, 2021

Is there a performance benefit from this?

@SCIF
Copy link
Contributor Author

SCIF commented Oct 25, 2021

Is there a performance benefit from this?

Don't think so (hopefully, I'm wrong :)). It's more about memory leak prevention. Analyzing Psalm codebase shows about 0.5Mb of memory save. So it's more about a proper usage of callbacks rather than optimization.

@sasezaki
Copy link
Contributor

@SCIF
Did you use any tool to convert ?

@orklah
Copy link
Collaborator

orklah commented Oct 30, 2021

I believe PHPInspectionsEA for PHPStorm has a similar feature

@SCIF
Copy link
Contributor Author

SCIF commented Oct 30, 2021

@SCIF Did you use any tool to convert ?

I've done it using regex replace :) I didn't try to find a special tool to do that

@sasezaki
Copy link
Contributor

I believe PHPInspectionsEA for PHPStorm has a similar feature

I thought PHPInspectionsEA too :)

I've done it using regex replace :) I didn't try to find a special tool to do that

Ok, I see.
( If any stabilized tool available, we can include it for CI)

@staabm
Copy link
Contributor

staabm commented Nov 6, 2021

Afair php-cs-fixer can do it

@orklah orklah marked this pull request as draft November 24, 2021 19:08
@orklah
Copy link
Collaborator

orklah commented Jan 20, 2022

If you can rebase this and @weirdan is okay with it, I'm up for merging this

@SCIF
Copy link
Contributor Author

SCIF commented Jan 20, 2022

If you can rebase this and @weirdan is okay with it, I'm up for merging this

Will do today. Thanks!

@SCIF
Copy link
Contributor Author

SCIF commented Jan 23, 2022

Sorry, I'm still in process of doing that. I'm fixing the conflicts because of replacing function () callbacks with arrow/fn().

@SCIF
Copy link
Contributor Author

SCIF commented Feb 12, 2022

Guys, I just noticed there are A LOT of array_map() calls with passed callbacks. I was curious if the reason of usage is a speed. Made a simple test:

$arr = array_fill(0, 1_000_000, 'a');
$result = [];

$start = microtime(true);
foreach ($arr as $a) {
    $result[] = $a . '!';
}
echo ((microtime(true) - $start)*1000)."\n";

$start = microtime(true);
$result = array_map(static function(string $a): string {return $a.'!';}, $arr);

echo ((microtime(true) - $start)*1000)."\n";

$start = microtime(true);
$result = array_map(static fn(string $a): string =>  $a.'!', $arr);

echo ((microtime(true) - $start)*1000)."\n";

I was quite amazed to see that foreach() is MUCH faster. It's also much readable for cases when a multiline callback is passed to array_map() so I will rework a few places to use foreach()es

@SCIF
Copy link
Contributor Author

SCIF commented Feb 12, 2022

All green apart from CS and labels. Will fix cs soon. Make this PR ready to review. Gonna squash commits prior merging, so let me know when it will look fine for everybody.

@SCIF SCIF marked this pull request as ready for review February 12, 2022 22:56
@weirdan weirdan added the release:internal The PR will be included in 'Internal changes' section of the release notes label Feb 12, 2022
@@ -445,7 +445,7 @@ private static function handleNamedCall(

$mixin_candidates_no_generic = array_filter(
$mixin_candidates,
fn($check): bool => !($check instanceof TGenericObject)
static fn($check): bool => !($check instanceof TGenericObject)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type is known here

src/Psalm/Config.php Show resolved Hide resolved
src/Psalm/Internal/Codebase/Scanner.php Outdated Show resolved Hide resolved
@SCIF
Copy link
Contributor Author

SCIF commented Feb 19, 2022

End-to-end failures looks valid to me:

ERROR: RedundantCast - src/Event/Value/TestSuite/TestSuite.php:128:21 - Redundant cast to int (see https://psalm.dev/262)
                    (int) $e->getCode(),

https://www.php.net/manual/en/throwable.getcode.php

@orklah
Copy link
Collaborator

orklah commented Feb 19, 2022

End-to-end failures looks valid to me:

They are, but they doesn't come from your changes, we have to baseline them. @weirdan would you mind? I think we have some on 4.x as weel as on master

@SCIF
Copy link
Contributor Author

SCIF commented Feb 19, 2022

Squashed the commits in the branch. Fixed all the comments.

I spoofed array_map() with my implementation and has collected the hot spots of using array_map(). Top several spots got me around 100k of array_map() calls. I reworked them into foreach(). Some other places were reworked because code just made much readable when used foreach (less indentation, no 3-5 uses needed). Reworking of Config class helped to get rid of 30k of array_map() calls as there is no need to have array_map() in a getter if a setter ensures the classes are lowercased.

Based on my app, all those changes speed it up 0.5-1s (4 threads, ~55 second in total), but it's really hard to evaluate the precise number as results fluctuate a lot. So I believe there is some speedup but it's actually not such noticeable.

src/Psalm/Internal/Algebra.php Outdated Show resolved Hide resolved
@SCIF SCIF force-pushed the make-callbacks-static branch 2 times, most recently from 5be3195 to 6baab4e Compare March 25, 2022 22:27
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to debug errors:

ERROR: Trace - src/Psalm/Internal/Algebra.php:48:49 - $anded_types: non-empty-list<non-empty-list<Psalm\Storage\Assertion>> (see https://psalm.dev/224)
                /** @psalm-trace $anded_types */;


ERROR: Trace - src/Psalm/Internal/Algebra.php:49:53 - $new_anded_types: list<Psalm\Storage\Assertion> (see https://psalm.dev/224)
                /** @psalm-trace $new_anded_types */;

It looks like continue 2; is not properly handled by psalm here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to take a closer look but I don't have much time these days. Can you add an assert($new_anded_types !== []); ? I think this should do the trick :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that helps. But that's excessive code (a performance waster) intended just to calm psalm. Tbh, adding this to baseline would be a bit better. I propose to wait a few days other suggestions :)

@@ -121,7 +121,7 @@ public function __construct(
$this->protocolReader = $reader;
$this->protocolReader->on(
'close',
function (): void {
static function (): void {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must fail. Probably the LanguageServer is not covered with tests.

@@ -56,7 +56,7 @@ public function __construct($input)
/**
* @return Generator<int, Promise<?string>, ?string, void>
*/
function () use ($input): Generator {
static function () use ($input): Generator {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must fail as well.

@SCIF
Copy link
Contributor Author

SCIF commented Mar 25, 2022

LanguageServer and ProtocolStreamReader apparently not covered with tests as my changes should have fail them. As I don't use this feature and don't know way to proper test it — I reverted back my changes as it would be too dangerous and not responsible.

@SCIF
Copy link
Contributor Author

SCIF commented Mar 25, 2022

I squashed commits, double checked my changes, reverted too dangerous changes. Please give me a hand to overcome psalm errors I highlighted above. Would highly appreciate if you would review/help to conclude this PR as it's really time consuming (I know this is unwanted refactoring and you could oppose it, then I will considering closing the PR).

@orklah
Copy link
Collaborator

orklah commented Mar 26, 2022

(I know this is unwanted refactoring and you could oppose it, then I will considering closing the PR)

I fully intend to merge this :) I haven't been able to spend a lot of time on Psalm these days, sorry if I don't always respond in time

@SCIF
Copy link
Contributor Author

SCIF commented Mar 26, 2022

sorry if I don't always respond in time

No need to be sorry. You are one of the most active contributors and in general Psalm has very good response time which a lot of OpenSource projects can dream of.

@SCIF
Copy link
Contributor Author

SCIF commented Apr 7, 2022

Don't want to bother by mentioning core devs, so just calling for the advice how to overcome the issue above. Gonna to add asserts over the weekend if no better solutions will be suggestion.

@SCIF
Copy link
Contributor Author

SCIF commented Apr 9, 2022

I have merged the PR with the latest master. The issues coming from it.

@orklah
Copy link
Collaborator

orklah commented Apr 9, 2022

I'm ready to merge if you are :)

@SCIF SCIF requested a review from weirdan April 9, 2022 10:51
@SCIF
Copy link
Contributor Author

SCIF commented Apr 9, 2022

I'm ready to merge if you are :)

I did my best and double checked the logic of all changes. The places which I suspected, I reworked in a separate file and compared with my PR's one. That strategy gave me results I reflected in comments above. Looks fine to me now :)

A review from @weirdan is locking the merge apparently.

@orklah orklah merged commit 916fddb into vimeo:master Apr 9, 2022
@orklah
Copy link
Collaborator

orklah commented Apr 9, 2022

A review from @weirdan is locking the merge apparently.

nah, that's okay :)

Thanks!

@SCIF SCIF deleted the make-callbacks-static branch April 9, 2022 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Needs work release:internal The PR will be included in 'Internal changes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants