232 karma · joined March 27, 2013
to use it for a static embedded use case, you may write your own build script to compile the library (it's not hard to write one). the cflags are describe in src/Makefile.am
and you can use pkg-config to list the flags you need.
If we consider complexity before everything, we must use PHP to implement an OS. :-p
this router library can be used for different purpose (outside of URL dispatching), although we don't gain so much performance from the point of view of each single request.
and in order to ship this package to deb, deploy to different platform, use autotools to test C features is a requirement. (we used to build the project with cmake before we use autotools)
- Pux uses simpler data structure (indexed array) to store the patterns and flags. (In PHP internals, zend_hash_index_find is faster than zend_hash_find).
- When matching routes, symfony uses a lot of function calls for each route:
https://github.com/symfony/Routing/blob/master/Matcher/UrlMa...
- Pux fetches the pattern from an indexed-array:
https://github.com/c9s/Pux/blob/master/src/Pux/Mux.php#L189
- Pux separates static routes and dynamic routes automatically, Pux uses hash table to look up static routes without looping the whole route array.
- Pux\Mux is written in C extension, method calls are faster!
- With C extension, there is no class loading overhead.
- Pux compiles routes to plain PHP array, the compiled routes can be loaded very fast. you don't need to call functions to register your routes before using it.
have checked the code, there are a lot of magic method calls in flightphp. magic method calls are even slower than normal method call.
the pure dispatching speed can be 10+x faster with C extension.
Using pure PHP, you need to load a lot of class files. and function calls, method calls, hash find are pretty slow in pure PHP.
- First of all, while using C extension, you don't need to reload these php class files again and again. it reduces the class loading overhead.
- Seconds, looping and string comparison is pretty fast in C. it's because in pure PHP, it duplicates the string when calling functions/methods in the runtime.
- Third, there are a lot of spaces to optimize the code in C rather than in pure php.
- Last, pure PHP consumes a lot of memory, but in C extension, the memory footprint is pretty small.
We firstly tested the pure dispatching benchmark by a simple benchmark tool. (without ab), code is here.
https://github.com/c9s/router-benchmark/blob/master/code/dis...
Tests including klein, ham, aura, symfony/routing. This tests pure dispatching speed, so the benchmark does not includes cache.
The dispatching benchmark result is here:
https://github.com/c9s/router-benchmark/blob/master/code/dis...
The benchmark with apache is just to show the comparison result (which shares the same configurations).
Pux is basically written in C extension, which reduces the overhead to load classes from PHP files. Also it does a different strategy on route dispatching. to compare the routes as fast as possible, pux uses indexed array to store the route pattern, pcre flag. (In PHP internals, zend_hash_index_find is faster than zend_hash_find)
it's not just iterating all routes in a big loop like Symfony.