Tag Archives: PHP Extension

A WordPress PHP Extension

I’ve been playing around with this idea for a while and I think it would be very interesting concept to complete. The concept is simple and most of the code will be easy. The concept part is take what is most often used and least optimized and rewrite it in C/C++. The easy part is that you can sort of do similar coding structure, albeit with pointers and zvals, but the code can be structured in the same way, well mostly.

The disadvantage of taking an almost 1:1 approach from PHP to C/C++ is that the result code may only be slightly faster (between 15% to 30% increase of performance). Comparable, it might be even faster to instead use C and C++ to the full advantage and use algorithms based completely on them.

However, it would be interesting to enable the Plugin API to execute in linear time with some of the logic. The Plugin API would be a really great first testament of the PHP Extension and it would be interesting to implement other features within it. However, I think just having every function within C/C++ would be a good initial effort.

The initial release has to be released quickly, be optimized as much as possible. If you don’t optimize the C code as well, then you can have just as bad performance. It should be fun, but I hope that I’ll have the fortitude to actually finish the plugin API part to enable benchmarking between the PHP and PHP Extension.

Possibly Related Posts:


Loading PHP Files in a PHP Extension

Just so I don’t forget again. Special thanks to Johannes S. on the PHP Internals list.

However, my problem is that I want to have the included PHP files with every PHP process, but only do that once. If the PHP files were in the include path, then they would still have to be included, but somehow I want the extension to include them and have them available.

First Method

The first method is not so much that it is impossible as it is impractical. Mostly when an extension is made, it is either a DLL or a SO file, or it is compiled into PHP. The PHP files can’t be included in the compilation, so they won’t make it in the DLL or SO or be compiled into PHP.

Therefore, it can be assumed in that case, that providing a relative link from C++ is impossible, because there is no way to know where the files will end up (unless you use PEAR, but even then it isn’t absolute). So you can’t just say.

PHP_MINIT(something)
{
    // Get file handle by using zend file handle struct

    zend_execute_scripts(...); // Run in a loop to include all files.
}

Unless you could get the PHP include path (which I believe you can, but I’m unsure how) and check each of those for the files. Then however, the reason you would use PEAR is to automate everything. Somehow, the PHP files how to end up in the PEAR directory or some directory that PHP has access to or is in the include path.

Second Method

The second method is using PEAR to automate the process of moving the PHP files, however if the files are in the include path, then there is no need to use zend_execute_scripts().

In this way, if the User didn’t know about the files, they wouldn’t have to try to include them. If they don’t try to include them, then my extension can do so and during the PHP_MINIT() process.

So the process then becomes:

  1. Build PEAR package
  2. Get PHP include Path
  3. In MINIT(), Search for files and if they exist loop through zend_execute_scripts()

Best Method

The best method would be to just rewrite the files as C/C++ which would increase performance better. However, it is in my opinion, that while that is the case, if you are depending on an external PHP library and want some performance, and want to only compile the scripts once per process (if the process is kept running for a long time), which might at least keep the library in memory for each PHP run after the first providing at least some advantage. If you don’t want to keep playing catchup with the external library, then the second method will work best.

If playing catchup every time a new release is made is no issue because of performance, then converting the PHP to C/C++ would work best. I’m of the mind, that using both methods to write the most used in C/C++ and then having the rest in PHP files is better.

I’m not going to write any code at this time.

Possibly Related Posts: