Professional Documents
Culture Documents
TutorialPHP
SPEEDIER SCRIPTS
Practical PHP
Programming
What’s faster than PHP code? Surely nothing! Paul Hudson
shows you how to make your scripts run 326 times faster!
veryone knows that PHP is faster than a speeding ticket, then an opening brace {, a statement, then a closing brace }.
but can it be made to go faster? C programmers have for Sound familiar? PHP uses the same sort of rules – although on a
many years trumpeted the fact that their language is much more complicated level – to parse your code.
extremely fast and therefore capable of handling PHP has hundreds of such rules, and, when it matches them,
performance-critical tasks. However, very often you’ll find that it calls appropriate internal C functions to handle the statement.
when C programmers really need performance, they use inline For example, when PHP matches the following rule (this is direct
assembly code. from the PHP source code):
Open up your Linux kernel source (you do have the kernel T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME ‘[‘
source to hand, right?), and pick what you consider to be a CPU- expr ‘]’ ‘}’
intensive operation. I chose arch/i386/lib/mmx.c, the code that The Zend Engine will, amongst other things, call
handles MMX/3dNow! instructions on compatible chips. Inside fetch_array_begin(&$$, &$2, &$4 TSRMLS_CC), which uses
this file you’ll see lots of quite complicated C code, but also items 2 and 4 of the rule (T_STRING_VARNAME and expr) to
extended instances of assembly code wherever speed is optimal. read and return an array item. So, as you should be able to
In fact, if you change directory to the root of the Linux kernel guess, that particular handler is for accessing arrays inside strings,
tree, try this command: eg: {$foo[‘bar’]}
grep “_ _ asm_ _ ” ./ -rl | wc -l Because the code to execute your script is just compiled C
That searches all the files in the Linux source distribution for code, it means that no matter how fast your PHP code is, it still
instances of assembly, and counts the number of files that match. has to be interpreted then executed as normal C code. PHP is
In 2.5.65, the number of files in the kernel source that use not compiled to native machine code at any point, so there is
assembly once or more is the rather ominous number of 666! never any chance of it out-performing C, or generally even
So, C programmers using assembly is quite a widespread thing. coming close to the performance of C.
PHP programmers, although blessed with a naturally fast
language, can also use a lower-level language for speed-critical Faster and faster
operations – although in our case, C is next in the food chain. So, the way to make your PHP code faster is to replace chunks of
While it’s possible to use assembly code from PHP (through C, as C it with pure, compiled C. In PHP, this can be done in three ways:
programmers do), there’s more than enough speed improvement writing your own module, editing the PHP source code, or editing
just switching to C, so that’s what we will be covering here. the Zend Engine.
Please note that, within the extent of the space available, this Writing a module for PHP is the accepted way to add
is a no-holds-barred article – prior knowledge of C is required, functionality, and there are many modules available in PHP to do
knowledge of assembly would be good, and very good all sorts of tasks. However, modules are the slowest way to add
knowledge of PHP is mandatory. Furthermore, in order to provide functionality, particularly if calls to dl() are required to dynamically
the most detailed description of how things work, this tutorial has load the module each time a script needs it.
been split into two parts. I hope you will agree it’s worth it! Writing functions directly into the PHP source code is faster
than using modules, but only really possible if you’re working on
The C Perspective your own server. Finally, writing functions directly into the Zend
PHP itself is written in C, as are Flex and Bison (see this month’s Engine provides the biggest performance boost, but basically
Flex and Linux Pro), the lexer and parser that PHP uses internally. The confines your script to your own machine – not many would be
Bison process of executing PHP code works by matching various parts of willing to patch their Zend Engine code to try out your code! There
code against pre-defined lists of acceptable grammar. For example: is actually a surprising boost for shifting code into the Zend Engine
For more information T_IF T_LEFTBRACK T_CONDITION T_RIGHTBRACK – when Andrei Zmievski converted strlen() into a ZE statement as
about Flex and Bison see T_LEFTBRACE T_STATEMENT T_RIGHTBRACE opposed to a function, he reported a 25% speed boost.
the first part of our new
In that piece of pseudo-grammar, T stands for “Type”. It will With such a big gain to be offered, you’re probably thinking
LINUX PRO tutorial series
starting on page 11.
match a statement that starts with if, then an opening bracket, everything should be put directly into the Zend Engine. However,
followed by any boolean condition, followed by a close bracket, it’s important to realise that there’s a big trade-off between speed
TutorialPHP
TutorialPHP
Over-optimisation
There’s a fine line – cross it at your peril!
TutorialPHP
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $TAR_DIR/lib,
TAR_SHARED_LIBADD)
AC_DEFINE(HAVE_TARLIB,1,[ ])
],[
AC_MSG_ERROR([wrong tar lib version or lib not found])
],[
-L$TAR_DIR/lib -lm -ldl
])
PHP_SUBST(TAR_SHARED_LIBADD)
NEXT
directory and type ./buildconf. This generates the configure exit
script for PHP, and will include our new tar extension if all is well. php -f ext/tar/tar.php
To make sure buildconf succeeded, type ./configure – – help
and look for the line - - with-tar. If the m4 file was good, you
tar.php was created by ext_skel and calls the function
confirm_tar_compiled(), which is a default function defined in
MONTH
should see the line somewhere in there, and also Include tar tar.h and tar.c that that simply confirms the module compiled Having had a special eight
support in the column next to it. On my screen, the Include tar correctly. So, if your tar module works fine, you should see the page PHP tutorial in last
support column is one character off to the left compared to the message “Congratulations! You have successfully modified month’s issue, it just
others. If you recall, the default m4 file had a line in there saying ext/tar/config.m4. Module tar is now compiled into PHP.” LXF wasn’t possible to run
another long tutorial this
Make sure that the comment is aligned – this is what that
time – at least not without
comment was referring to. If your comment is out of alignment Make your mark renaming the mag PHP
add or remove spaces in the config.m4 file (line five, if you’ve
Brainstorms ’R’ Us Format! So, this tutorial
used the above m4 file) to correct it. will be continued next
The next step is to run: Would you like to get your name in the mag and learn about stuff month.
./configure —with-tar you're most interested in? At this point, you’ve got
We're looking out for ideas for new Linux Format Practical PHP a working extension to
You may want to add other PHP extensions to your configure line if
tutorials, and where better to look than to you, the reader? If, while PHP – although it doesn’t
you use them, but the above is enough to test our new extension.
reading past issues of Practical PHP, you've thought “I wish they’d do much. Next month
As the output from configure flies by, you should see the covered XYZ in more depth...”, or “I really want to know how to we’ll look at how to use
following three lines somewhere in there: use...”, then now’s the time to get your voice heard! libtar in the extension by
checking for tar support... yes Send an email to paul.hudson@futurenet.co.uk with your ideas – writing a function
checking for tar files in default path... found in /usr all the good suggestions that you send in will be covered in future tar_list(). If you want to
checking for tar_open in -ltar... yes issues. So far, the topics we have covered in some depth include create your own extension
MySQL, XML, CLI, GUIs, media generation, templates, and more. in the future, simply
The first line signals yes if - - with-tar was specified on the
If you're short of ideas, you’re certainly welcome to write in with repeat the steps covered
command line. If - - with-tar was used, configure checks for the
comments about prior issues – we're always looking to improve the in this issue – next tutorial
location of the header file we specified (libtar.h), and outputs overall quality of tutorials. will be libtar-specific.
where it found it, which is line two. The final line is our library