well. implementing these feature testing scripts is kinda waste of time. so I rather to pick up autotools, which provides a integrated features.
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.
Hi! your implementation is inspiring to me. so I mixed some other ideas to implement this (for fun and experimentation).
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.
The behavior of (strndiff / strdiff) is different from strncmp or strcmp. strn?cmp does not return the offset. they are different functions.
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:
- 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.
Yes you can use APC, opcache or XCache to load php bytecode in runtime. but you are still calling php functions/methods or creating a lot of objects in runtime. that's the overhead.
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.
First of all, for your information, the benchmark code and details are already there: https://github.com/c9s/router-benchmark sorry I forgot to put the link. :p
We firstly tested the pure dispatching benchmark by a simple benchmark tool. (without ab), code is here.
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.