LXF41 JUNE 2003
veryone knows that PHP is faster than a speeding ticket,but can it be made to go faster? C programmers have formany years trumpeted the fact that their language isextremely fast and therefore capable of handlingperformance-critical tasks. However, very often you’ll find thatwhen C programmers
need performance, they use inlineassembly code.Open up your Linux kernel source (you
have the kernelsource to hand, right?), and pick what you consider to be a CPU-intensive operation. I chose arch/i386/lib/mmx.c, the code thathandles MMX/3dNow! instructions on compatible chips. Insidethis file you’ll see lots of quite complicated C code, but alsoextended instances of assembly code wherever speed is optimal.In fact, if you change directory to the root of the Linux kerneltree, try this command:
grep “__asm__” ./ -rl | wc -l
That searches all the files in the Linux source distribution forinstances of assembly, and counts the number of files that match.In 2.5.65, the number of files in the kernel source that useassembly once or more is the rather ominous number of 666!So, C programmers using assembly is quite a widespread thing.PHP programmers, although blessed with a naturally fastlanguage, can also use a lower-level language for speed-criticaloperations – although in our case, C is next in the food chain.While it’s possible to use assembly code from PHP (through C, as Cprogrammers do), there’s more than enough speed improvement just switching to C, so that’s what we will be covering here.Please note that, within the extent of the space available, thisis a no-holds-barred article – prior knowledge of C is required,knowledge of assembly would be good, and
goodknowledge of PHP is mandatory. Furthermore, in order to providethe most detailed description of how things work, this tutorial hasbeen split into two parts. I hope you will agree it’s worth it!
The C Perspective
PHP itself is written in C, as are Flex and Bison (see this month’s
), the lexer and parser that PHP uses internally. Theprocess of executing PHP code works by matching various parts ofcode against pre-defined lists of acceptable grammar. For example:
T_IF T_LEFTBRACK T_CONDITION T_RIGHTBRACKT_LEFTBRACE T_STATEMENT T_RIGHTBRACE
In that piece of pseudo-grammar, T stands for “Type”. It willmatch a statement that starts with
then an opening bracket,followed by any boolean condition, followed by a close bracket,then an opening brace
, a statement, then a closing brace
.Sound familiar? PHP uses the same sort of rules – although on amuch more complicated level – to parse your code.PHP has hundreds of such rules, and, when it matches them,it calls appropriate internal C functions to handle the statement.For example, when PHP matches the following rule (this is directfrom the PHP source code):
T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME ‘[‘expr ‘]’ ‘}’
will, amongst other things, call
fetch_array_begin(&$$, &$2, &$4 TSRMLS_CC)
, which usesitems 2 and 4 of the rule (
) toread and return an array item. So, as you should be able toguess, that particular handler is for accessing arrays inside strings,eg:
Because the code to execute your script is just compiled Ccode, it means that no matter how fast your PHP code is, it stillhas to be interpreted then executed as normal C code. PHP isnot compiled to native machine code at any point, so there isnever any chance of it out-performing C, or generally evencoming close to the performance of C.
Faster and faster
So, the way to make your PHP code faster is to replace chunks ofit with pure, compiled C. In PHP, this can be done in three ways:writing your own module, editing the PHP source code, or editingthe
.Writing a module for PHP is the accepted way to addfunctionality, and there are many modules available in PHP to doall sorts of tasks. However, modules are the slowest way to addfunctionality, particularly if calls to
are required to dynamicallyload the module each time a script needs it.Writing functions directly into the PHP source code is fasterthan using modules, but only really possible if you’re working onyour own server. Finally, writing functions directly into the
provides the biggest performance boost, but basicallyconfines your script to your own machine – not many would bewilling to patch their
code to try out your code! Thereis actually a surprising boost for shifting code into the
– when Andrei Zmievski converted
statement asopposed to a function, he reported a 25% speed boost.With such a big gain to be offered, you’re probably thinking
should be put directly into the
. However,it’s important to realise that there’s a big trade-off between speed
For more informationabout Flex and Bison see the first part of our new
tutorial seriesstarting on page 11.
What’s faster than PHP code? Surely nothing!
shows you how to make your scripts run 326 times faster!
LXF41.tut_php 5/2/03 12:47 PM Page 74