padrino/padrino-framework — pull request #1692 by namusyaka (2014-06-03 13:43:23+00:00)
Title: Performance improvement of new router

# New router improvement
## Introduction
I've been working on a performance improvement of the new router.
This time, I achieved a certain result, so I would like to report it.

## Result
![graph-3](https://cloud.githubusercontent.com/assets/309329/3157633/cd53a7b0-eaf7-11e3-9024-3617ad6c61d1.png)
* Greater is faster.
* This benchmark is a summary of the results obtained using the `ab` command.

What I want to convey through this graph, it is that new router until equal to or greater than http_router, that performance was improved.

## Approach
In a nutshell, this approach reduces the amount of calculation by concatenating the regexps(generated by mustermann).
It contents of processing varies greatly in comparison to the previous one conventional.
How it works, I explain with simple code like the following.
```ruby
routes = []
routes << /\/foo/
routes << /\/bar/
routes << /\/baz/

path = "/foo"

# Recognizer (replace-with-new-router branch)
routes.select{|regexp| regexp === path } #=> [/\/foo/]

########################################################

# Compiler (this branch)
@regexp_routes = routes.map.with_index do |regexp, index|
/(?<_#{index}>#{regexp})/
end

def compile(regexps, paths = [])
return paths if regexps.length.zero?
paths << Regexp.union(regexps)
regexps.shift
compile(regexps, paths)
end

@regexps = compile(@regexp_routes.dup)
#=> ["/(?-mix:(?<_0>(?-mix:\\/foo)))|(?-mix:(?<_1>(?-mix:\\/bar)))|(?-mix:(?<_2>(?-mix:\\/baz)))/", "/(?-mix:(?<_1>(?-mix:\\/bar)))|(?-mix:(?<_2>(?-mix:\\/baz)))/", "/(?<_2>(?-mix:\\/baz))/"]

def match(pattern)
offset = 0
loop.with_object([]) do |_, candidacies|
return candidacies unless @regexps[offset] === pattern
current = 0
route = @regexp_routes[offset..-1].detect.with_index do |route, index|
result = Regexp.last_match("_#{index}")
current = index if result
result
end
candidacies << route
offset = current + 1
end
end

match(path) #=> [/(?<_0>(?-mix:\/foo))/]
```

@padrino/core-members, @all-padrino-users What do you think about this?
If OK, I'm going to continue to develop new router based on this approach.