2014-05-14 12:18:59 -04:00
|
|
|
R3
|
2014-05-11 18:52:36 -04:00
|
|
|
================
|
|
|
|
|
2014-05-16 08:11:59 -04:00
|
|
|
R3 is an URI router library with high performance, thus, it's implemented in C.
|
|
|
|
It compiles your route paths into a prefix trie.
|
|
|
|
|
|
|
|
By using the constructed prefix trie in the start-up time, you can dispatch
|
2014-05-16 07:17:59 -04:00
|
|
|
routes with efficiency.
|
2014-05-11 18:52:36 -04:00
|
|
|
|
2014-05-16 08:11:59 -04:00
|
|
|
|
|
|
|
Requirement
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
* cmake
|
|
|
|
* check
|
|
|
|
* pcre
|
|
|
|
* jemalloc
|
|
|
|
|
2014-05-11 20:24:22 -04:00
|
|
|
Pattern Syntax
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
/blog/post/{id} use [^/]+ regular expression by default.
|
|
|
|
/blog/post/{id:\d+} use `\d+` regular expression instead of default.
|
|
|
|
|
2014-05-11 18:52:36 -04:00
|
|
|
|
2014-05-16 03:29:25 -04:00
|
|
|
C API
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
```c
|
|
|
|
// create a router tree with 10 children capacity (this capacity can grow dynamically)
|
2014-05-16 06:57:36 -04:00
|
|
|
n = r3_tree_create(10);
|
2014-05-16 03:29:25 -04:00
|
|
|
|
|
|
|
int route_data = 3;
|
|
|
|
|
|
|
|
// insert the route path into the router tree
|
2014-05-16 06:57:36 -04:00
|
|
|
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 );
|
2014-05-16 03:29:25 -04:00
|
|
|
|
|
|
|
// let's compile the tree!
|
2014-05-16 06:57:36 -04:00
|
|
|
r3_tree_compile(n);
|
2014-05-16 03:29:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
// dump the compiled tree
|
2014-05-16 06:57:36 -04:00
|
|
|
r3_tree_dump(n, 0);
|
2014-05-16 03:29:25 -04:00
|
|
|
|
|
|
|
// match a route
|
2014-05-16 06:57:36 -04:00
|
|
|
node *matched_node = r3_tree_match(n, "/foo/bar", strlen("/foo/bar") );
|
2014-05-16 03:29:25 -04:00
|
|
|
matched_node->endpoint; // make sure there is a route end at here.
|
|
|
|
int ret = *( (*int) matched_node->route_ptr );
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2014-05-11 18:52:36 -04:00
|
|
|
|
2014-05-16 07:17:59 -04:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2014-05-16 07:19:10 -04:00
|
|
|
### The benchmarking route paths
|
2014-05-16 07:18:47 -04:00
|
|
|
|
|
|
|
The route path generator is from <https://github.com/stevegraham/rails/pull/1>:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
#!/usr/bin/env ruby
|
|
|
|
arr = ["foo", "bar", "baz", "qux", "quux", "corge", "grault", "garply"]
|
|
|
|
paths = arr.permutation(3).map { |a| "/#{a.join '/'}" }
|
|
|
|
paths.each do |path|
|
|
|
|
puts "r3_tree_insert_path(n, \"#{path}\", NULL);"
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2014-05-11 18:52:36 -04:00
|
|
|
Use case in PHP
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
```php
|
|
|
|
// Here is the paths data structure
|
|
|
|
$paths = [
|
2014-05-13 04:13:20 -04:00
|
|
|
'/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' ] ,
|
2014-05-11 18:52:36 -04:00
|
|
|
];
|
2014-05-16 07:13:24 -04:00
|
|
|
$rs = r3_compile($paths, 'persisten-table-id');
|
|
|
|
$ret = r3_dispatch($rs, '/blog/post/3' );
|
2014-05-11 18:52:36 -04:00
|
|
|
list($complete, $route, $variables) = $ret;
|
|
|
|
|
2014-05-16 07:13:24 -04:00
|
|
|
// matched conditions aren't done yet
|
|
|
|
list($error, $message) = r3_validate($route); // validate route conditions
|
2014-05-11 18:52:36 -04:00
|
|
|
if ( $error ) {
|
|
|
|
echo $message; // "Method not allowed", "...";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2014-05-16 08:35:16 -04:00
|
|
|
Install
|
2014-05-16 08:11:59 -04:00
|
|
|
----------------------
|
|
|
|
|
2014-05-16 08:35:16 -04:00
|
|
|
cmake CMakeLists.txt -Wno-dev
|
2014-05-16 08:11:59 -04:00
|
|
|
make
|
2014-05-16 08:35:16 -04:00
|
|
|
sudo make install
|
2014-05-16 08:11:59 -04:00
|
|
|
|