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

Draft: Intersect optimization for large unions #1471

Merged
merged 3 commits into from Jun 24, 2022

Conversation

neclimdul
Copy link
Contributor

No description provided.

@@ -638,11 +638,13 @@ public static function intersect(Type ...$types): Type
$topLevelUnionSubTypes = [];
$innerTypes = $type->getTypes();
usort($innerTypes, $sortTypes);
$slice1 = array_slice($types, 0, $i);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi,

Just out of curiosity, why creating intermediary variables here?

Copy link
Contributor

Choose a reason for hiding this comment

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

otherweise these arrays get sliced within the loop over and over again

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh you're right, for some reason I haven't seen the foreach loop ! Well done.

Copy link
Contributor

Choose a reason for hiding this comment

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

@neclimdul I feel this array_slice change is a no-brainer and should be submitted as a seperate PR.
the other change breaks the tests atm, so needs further inspection

Copy link
Member

Choose a reason for hiding this comment

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

I agree, please send a microoptimization PR, then we'll discuss the rest.

neclimdul and others added 3 commits June 24, 2022 13:25
Array slice spends a good bit of time allocating memory for new arrays
even for small arrays. For larger computed unions this can be a
non-trivial amount of time. We can minimize the cost by not repeating
the slice for ever iteration of the inner loop since it doesn't depend
on the loop.
Don't know if this will have side effects so testing needed but if it
works... oh lord.

Fixes #7421 Slow processing of Class::* type hints
@ondrejmirtes ondrejmirtes force-pushed the type-specification-optimization branch from eca9bf2 to 9abc52c Compare June 24, 2022 11:25
@ondrejmirtes
Copy link
Member

Ah whatever, I really like the optimization, the result can be slightly worse in this case :)

@ondrejmirtes
Copy link
Member

I pushed test adjustments, will merge it if it comes back green.

@ondrejmirtes ondrejmirtes merged commit f26cff0 into phpstan:1.7.x Jun 24, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@@ -422,7 +422,7 @@ public function dataCondition(): array
new Variable('foo'),
new Variable('bar'),
),
['$foo' => 'Bar', '$bar' => 'Bar'],
['$foo' => 'Bar', '$bar' => 'mixed'], // could be '$bar' => 'Bar'
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really annoying to not have anymore :/

I might spend some time on this in the future to bring back.

Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry, it looked pretty esoteric to me as a way to narrow down types :)

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can still do it only for objects, or something like that...

Copy link
Contributor

Choose a reason for hiding this comment

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

If it annoys me enough I will find a way to bring it back without a huge performance loss :)

@herndlm
Copy link
Contributor

herndlm commented Jun 24, 2022

I'm kind of travelling right now and could not check - do we know if this improves other big union operations too? E.g. some constant array perf problems also lead to many unions. Or is this a too different case?

@neclimdul
Copy link
Contributor Author

@herndlm likely very specific.

@mvorisek
Copy link
Contributor

@neclimdul this PR has introduced phpstan/phpstan#7550 issue, will you fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants