require_relative "./lib/cuba"
Cuba.define do
on "users" do
on ":id" do |id|
on root do
res.write "User #{id}"
end
on "projects" do
on ":project_id" do |project_id|
res.write "User #{id}, Project #{project_id}"
end
end
end
end
end
env1 = { "PATH_INFO" => "/users/1", "SCRIPT_NAME" => "/" }
env2 = { "PATH_INFO" => "/users/1/projects/2", "SCRIPT_NAME" => "/" }
time = Time.now
30_000.times do
Cuba.call(env1)
Cuba.call(env2)
end
puts Time.now - time
On my machine it takes about 4 seconds to complete.
I run the snippet above and it now takes 2 seconds.
Twice as fast!
Do you think this is a good change? I think it's harmless: the routes of an app are fixed (not dynamic) so that REGEXES hash will reach a reasonable maximum size. Plus it can help with routes like ":id" where they might be used in several places.
Hi @soveran! 😄
I noticed that to consume segments a regex is created each and every time the segment tries to be matched:
https://github.com/soveran/cuba/blob/f66109654ff1c16ee46060330738a902c7a1a7fc/lib/cuba.rb#L214
So I made this benchmark:
On my machine it takes about 4 seconds to complete.
Now I cache the regexes with this diff:
I run the snippet above and it now takes 2 seconds.
Twice as fast!
Do you think this is a good change? I think it's harmless: the routes of an app are fixed (not dynamic) so that REGEXES hash will reach a reasonable maximum size. Plus it can help with routes like ":id" where they might be used in several places.
What do you think?