Skip to content

Greatly improve static path performance #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2018
Merged

Conversation

stakach
Copy link

@stakach stakach commented Nov 4, 2018

Comparing luislavena/radix with a hash for static route lookup.

Radix tree with a mix of static and route params paths
image
A radix tree with only static routes was 2.3 times slower than the hash.

I also removed the call to .upcase when parsing the request as the HTTP standard specifies that this should be case sensitive - as such all browsers will make the request in the required case already.

@tbrand
Copy link
Owner

tbrand commented Nov 4, 2018

That's amazing improvement! Thanks! 👍

@tbrand tbrand merged commit ac3ca87 into tbrand:master Nov 4, 2018
@tbrand
Copy link
Owner

tbrand commented Nov 4, 2018

Your PR is shipped on https://github.com/tbrand/router.cr/releases/tag/v0.2.6 🎉

@stakach
Copy link
Author

stakach commented Nov 4, 2018

When compiled with the --release flag the difference is even greater

image

The code I used to benchmark:

    tree = Radix::Tree(String).new

    path = "/GET/path/"
    hash = {
        "/GET/path/" => "woot!",
        "/GET/path/:id" => "other!",
        "/POST/path/:id" => "other2!"
    }
    tree.add("/GET/path/", "woot!")
    tree.add("/GET/path/:id", "other!")
    tree.add("/POST/path/:id", "other2!")

    Benchmark.ips do |x|
      x.report("Radix Lookup") do
          result = tree.find(path)
          raise "error" unless result.payload == "woot!"
     end
      x.report("Hash Lookup") do
          result = hash[path]
          raise "error" unless result == "woot!"
      end
    end

@tbrand
Copy link
Owner

tbrand commented Nov 4, 2018

Amazing! 🎉

waghanza pushed a commit to the-benchmarker/web-frameworks that referenced this pull request Nov 4, 2018
* update router.cr for improved performance

See: tbrand/router.cr#29

* use crystal 0.27.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants