Speeding up the symfony bundle with Zephir
hstore, as well as field type hstorefor Doctrine ORM, for transparent work with such fields. Everything is designed in the Symfony bundle Intaro \ HStoreBundle . But the whole thing is not about the bundle. It's about how we tried to optimize it with Zephir .
The fact is that the bundle uses HStoreParserconverting string representations of arrays of values that come from PostgreSQL to PHP objects. And parsing starts to take place a considerable time, when we need to output a large number of records from the database in which there are hstore fields, and dozens of values are stored in each of the fields.
With a similar problem at the time faced a template developers the Twig , when they sags over time is one very commonly used function. They solved the problem by porting to the C-extension for PHP.
We decided to try this approach too, but we implemented it not in pure C, but using Zephir . To understand Zephir syntax, expand it and port the class
HStoreParserObjectively, it took a little time. One of Zephir’s biggest advantages is that its implementation is quite similar to the original PHP implementation. For comparison, the implementation is in PHP and Zephir . To evaluate the profit and verify the identity of class behavior, we prepared a test set of hstore data and a test . The run was carried out on a 2 GHz Intel Core i7 machine with PHP 5.4.26.
The results of the run are as follows:
- PHP HStoreParser: 614.228963852 ms for 1000 strings
- Zephir HStoreParser: 432.605981827 ms for 1000 strings
In addition, the Zephir code can be improved when some expected features appear (passing parameters by reference, internal static class variables, closures).
In the dry residue. Zephir-implementation of slow sections of code can be effective while not requiring strong rewriting of the code. In general, it is even able to support PHP and Zephir implementations in parallel so that the project works both in pure PHP and with C-extensions.
UPD Updated the PHP HStoreParser metrics, the initial run was done with xdebug enabled.