Switch Table Extension

The Switch Table Extension for PHP is an experimental extension that optimizes PHP’s execution of switch() statements by removing unnecessary jumps, using jump tables and caching the value of constants. Right now the extension is considered experimental and should not be used on production servers. Right now it is limited to optimize a maximum of 1024 switch() switch statements.

Overview

  • optimizes the execution of switch() statements
  • handles CASEs using simple data types like int, string
  • handles CASEs that use simple constants like PHP_INT_MAX, YOUR_CONSTANT
  • does not yet handle CASEs with class constants
  • does not yet handle CASEs using constant arithmethic
  • does not (and will not) touch CASEs using variables or function calls

Why?

Let’s consider the following simple switch statement. It defines five possible cases and also defines a default action action_default() that is triggered when none of the five cases matches. Additionally three of the cases result in the same action.

<?php
switch ($option)
{
    case 'option_1':
        action_1();
        break;
    case 'option_2a':
    case 'option_2b':
    case 'option_2c':
        action_2();
        break;
    case 'option_3':
        action_3();
        break;
    default:
        action_default();
}
?>

When the previous example is compiled by PHP code is generated that has the following, quite complex CFG (code flow graph).

spacer

This example shows that the PHP compiler does on the one hand generate lots of unnecessary JMP instructions and that lacking an N-way jumping instruction forces the compiler to generate code similar to chained if () then {} else {} constructs. Translated to PHP this means using a switch() in PHP is like coding the following construct.

<?php
 
if ($option == 'option_1') {
action_1();
} else if ($option == 'option_2a') {
action_2();
} else if ($option == 'option_2b') {
action_2();
} else if ($option == 'option_2c') {
action_2();
} else if ($option == 'option_3') {
action_3();
} else {
action_default();
}
 
?>

It should be obvious that this construct is very different from a table lookup that for example a C compiler generates for switch() statements. A chain of IF statements is really slow, especially when many different cases exist and the matching case is always one of the last. This also means that the execution speed of a PHP switch() statement can be increased by sorting the more likely cases to the front.

Because of this problem and because I am using switch() statements with several hundred cases that are executed many times the Switch Table Extension for PHP was born. It increases execution speed of PHP scripts with many switch() statements by implementing a mixture of jump optimization, jump table usage and constant cache. When the extension is running the CFG of the PHP script is reduced after the first execution to the following easier and faster graph.

spacer

Get the code

The current version 0.1.0 of the Switch Table Extension can be downloaded from the following URL.

www.suspekt.org/downloads/switchtable-0.1.0.tgz

Copyright

Copyright (c) 2008, Stefan Esser