PHP 7.2's "switch" optimisations
PHP 7.2 is around the corner soon, and comes with many optimisations. Many new optimisations are implemented in opcache, but some others are implemented in PHP itself. One optimisation that falls in the latter category is an optimisation of the switch/case construct.
Before PHP 7.2, PHP considers each case statement in order. Let's say we have the following (trivial code):
<?php
$cc = "no";
switch ( $cc )
{
case 'de': echo "DEUTSCH"; break;
case 'en': echo "English"; break;
case 'nl': echo "Nederlands"; break;
case 'no': echo "Norsk"; break;
default:
echo "unknown"; break;
}
?>
The PHP interpreter approaches the executing of this switch/case structure as if it was written like:
<?php
$cc = "no";
if ($cc == 'de') {
echo "DEUTSCH";
} else if ($cc == 'en') {
echo "English";
} else if ($cc == 'nl') {
echo "Nederlands";
} else if ($cc == 'no') {
echo "Norsk";
} else {
echo "unknown"; break;
}
?>
Yup, it really compares $cc variable to each case statement one by one. Which means that if your most commonly used switch case is further down the list, you end up wasting valuable CPU code.
With vld we can represent this in a graph. For each case statement, the graph shows that the interpreter can either go left or right, just as it was executing multiple if statements in sequence:
PHP 7.2 introduces an optimisation that converts this sequence of if statements into a jump table if all the case statements are either integers or strings. If we look at the pseudo code (PHP does not support variable labels with goto), we get something like:
<?php
$cc = "no";
$_table = [ "de" => 1, "en" => 2, "nl" => 3, "no" => 4 ];
if (gettype($cc) == 'string') {
if (array_key_exists($cc, $_table)) {
goto "jmp_{$_table[$cc]}";
} else {
goto jmp_default;
}
} else {
/* do original if/else if/else sequence */
}
jmp_1: echo "DEUTSCH"; goto end;
jmp_2: echo "English"; goto end;
jmp_3: echo "Nederlands"; goto end;
jmp_4: echo "Norsk"; goto end;
jmp_default: echo "unknown";
end:
?>
This example shows that as long as $cc is a string, it will use the jump table $_table to jump (with goto) to the immediate code representing the case statement. If $cc is not a string, it will have to do the usual if/else if/else dance. In this case, it will eventually end up on the default case.
Looking at the graph generated by vld for this optimised construct, we see this new structure as well:
Especially with big switch/case structures this is a welcome optimisation!
However…
Xdebug implements code coverage gathering functionality. This can be used with PHP_CodeCoverage to see which parts of your code, which branches, and which paths are covered while executing your tests. In order for Xdebug to figure out the branches and paths, it has to follow PHP's internal opcode structures to see where jump instructions happen. I had to add support for this new switch/case optimisation to make this work again. This has now been done, and is part of Xdebug's master branch. This branch will soon become the first beta release of Xdebug 2.6 which will provide support for PHP 7.2.
Comments
The goto jmp_5; → jmp_5; is not defined
Thank you for sharing. Quite interesting - even for those in older versions of PHP!
That`s nice to know. I will keep that in mind. Thanks for sharing with us!
- Is it strict ? eg switch($num) {
-
case 1: echo 'its a num'; break; case '1': echo 'its a string': break;
}
Yes, it is strict. If there are mixed types in case statements, it won't create a jump table at all, and fall back to the original PHP 7.1 behaviour.
Life Line
Merge branch 'xdebug_3_5'
Merged pull request #1071
Fixed issue #2411: Native Path Mapping is not applied to the initial …
Created 2 waste_baskets; Updated 3 waste_baskets, 2 benches, and 2 other objects; Deleted a waste_basket
I walked 7.9km in 1h45m36s
RE: https://phpc.social/@phpc_tv/116274041642323081
Now that phpc.tv and phpc.social are part of the same umbrella, I've upped my yearly contributions to their Open Collective: https://opencollective.com/phpcommunity/projects/phpc-social
Merge branch 'xdebug_3_5'
Merged pull request #1070
I walked 7.2km in 1h10m26s
Fixed issue #2405: Handle minimum path in .xdebug directory discovery
I've published a new blog post: "Human Creations", on the difference in content generation by LLMs, and the creation of text, art and code by humans.
You can find it at https://derickrethans.nl/human-creations.html or at @blog
I walked 7.8km in 1h38m32s
RE: https://phpc.social/@afilina/116274024588235234
It's good to see that more and more people are realising that the Web can be for-good, without all the enshittification.
That's why I'm happy to see endeavours like phpc.tv springing up, and helping out where I can.
Taking back the control of how the Web is for people, by people, without big tech making it all shit.
Created a waste_basket; Updated 5 crossings and a bicycle_parking
I walked 10.7km in 2h35m10s
If you're in the UK (south, preferably), the International Space Station is going to be visible straight above you in about half an hour!
If you're further north than London, it moves further to the south.
Updated 2 crossings
Created 3 crossings, 2 waste_baskets, and 2 trees; Updated a crossing
Created 2 post_boxes and a crossing; Updated a newsagent shop
Updated a crossing
Updated a post_box
Created a bench; Updated a bench
Created an information; Updated 2 benches
Updated a crossing and a supermarket shop
Confirmed a restaurant


Shortlink
This article has a short URL available: https://drck.me/php72switch-dgj