fork of https://github.com/c9s/r3 because they cant into cmake or dependency management
cmake_modules | ||
include | ||
src | ||
tests | ||
.gitignore | ||
bench.html | ||
CMakeLists.txt | ||
demo.c | ||
gen_routes.rb | ||
HACKING.md | ||
main.c | ||
main.h | ||
README.md |
R3
R3 is an URI router library. It compiles your route paths into a prefix trie. By using the constructed prefix trie in the start-up time, so you can dispatch routes with efficiency.
Pattern Syntax
/blog/post/{id} use [^/]+ regular expression by default.
/blog/post/{id:\d+} use `\d+` regular expression instead of default.
C API
// create a router tree with 10 children capacity (this capacity can grow dynamically)
n = r3_tree_create(10);
int route_data = 3;
// insert the route path into the router tree
r3_tree_insert_pathn(n , "/zoo" , strlen("/zoo") , &route_data );
r3_tree_insert_pathn(n , "/foo/bar" , strlen("/foo/bar") , &route_data );
r3_tree_insert_pathn(n , "/bar" , strlen("/bar") , &route_data );
r3_tree_insert_pathn(n , "/post/{id}" , strlen("/post/{id}") , &route_data );
// let's compile the tree!
r3_tree_compile(n);
// dump the compiled tree
r3_tree_dump(n, 0);
// match a route
node *matched_node = r3_tree_match(n, "/foo/bar", strlen("/foo/bar") );
matched_node->endpoint; // make sure there is a route end at here.
int ret = *( (*int) matched_node->route_ptr );
Benchmark
The routing benchmark from stevegraham/rails' PR https://github.com/stevegraham/rails/pull/1:
omg 10462.0 (±6.7%) i/s - 52417 in 5.030416s
And here is the result of the router journey:
omg 9932.9 (±4.8%) i/s - 49873 in 5.033452s
r3 uses the same route path data for benchmarking, and here is the benchmark:
5000000 iterations finished in 0.905591 seconds
5521256.22 i/sec
The matching speed of r3 is 527+ times faster than rails' trie router.
Use case in PHP
// Here is the paths data structure
$paths = [
'/blog/post/{id}' => [ 'controller' => 'PostController' , 'action' => 'item' , 'method' => 'GET' ] ,
'/blog/post' => [ 'controller' => 'PostController' , 'action' => 'list' , 'method' => 'GET' ] ,
'/blog/post' => [ 'controller' => 'PostController' , 'action' => 'create' , 'method' => 'POST' ] ,
'/blog' => [ 'controller' => 'BlogController' , 'action' => 'list' , 'method' => 'GET' ] ,
];
$rs = r3_compile($paths, 'persisten-table-id');
$ret = r3_dispatch($rs, '/blog/post/3' );
list($complete, $route, $variables) = $ret;
// matched conditions aren't done yet
list($error, $message) = r3_validate($route); // validate route conditions
if ( $error ) {
echo $message; // "Method not allowed", "...";
}