Skip to content

Speed up CombinationHelper#4825

Merged
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom
kaja47:2.1.x
Jan 27, 2026
Merged

Speed up CombinationHelper#4825
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom
kaja47:2.1.x

Conversation

@kaja47
Copy link
Contributor

@kaja47 kaja47 commented Jan 26, 2026

This change swaps loops to reduce number of intermediate arrays allocated and flips direction in which results are constructed to eliminate explicit append loop.

For 3 arrays with 10 elements each this leads to 5x speedup.

This change swaps loops to reduce number of intermediate arrays
allocated and flips direction in which results are constructed to
eliminate explicit append loop.

For 3 arrays with 10 elements each this leads to 5x speedup.
@staabm
Copy link
Contributor

staabm commented Jan 27, 2026

to 5x speedup.

how did you measure the speedup?

@ondrejmirtes ondrejmirtes merged commit dead87b into phpstan:2.1.x Jan 27, 2026
625 of 639 checks passed
@ondrejmirtes
Copy link
Member

Thank you very much!

@kaja47
Copy link
Contributor Author

kaja47 commented Jan 27, 2026

how did you measure the speedup?

PHP 8.4.16, JIT turned off, CPU i5-4570 and timed this loop

$ass = array_fill(0,3, range(1,10));
$sink = 0;
for ($i = 0; $i < 20_000; $i++) {
  $sink += count(iterator_to_array(CombinationsHelper::combinations($ass)));
}
var_dump($sink);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants