Skip to content
New issue

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

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

Already on GitHub? # to your account

Move request method matching to the bottom level instead of the top #30

Merged
merged 2 commits into from
Aug 5, 2020
Merged

Move request method matching to the bottom level instead of the top #30

merged 2 commits into from
Aug 5, 2020

Conversation

matthewmcgarvey
Copy link
Member

@matthewmcgarvey matthewmcgarvey commented Jul 29, 2020

Summary

Fixes #27

This change increases the speed of the routing library when a valid request is made.

Beyond just increasing the speed, this is another step towards a larger refactor of the internals. The big idea is to move the decision logic all into one place so that pieces can then be refactored to bring more clarity and conciseness. At this point, I do not believe that the library is any more or less clear but by moving this down into the Fragment class, we can refactor the PartProcessor and MatchFinder classes later on.

NOTE: this is definitely a breaking change for the internal API if that is a concern

Benchmark (with --release)

  • Before: 215 ms
  • After: 204 ms
  • Difference: 11 ms
  • Improvement: 5%

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice speed bump! As you mentioned I don't know i this is any clearer to me now than before but I'm excited to see how this opens up future opportunities for refactoring.

Comment on lines -24 to -44
# The matcher stores routes based on the HTTP method
#
# Each route key is a `Fragment(T)`. Where `T` is the type of the payload. See
# `Fragment` for details on how it works
#
# ## Example
#
# ```
# router = LuckyRouter::Matcher(Symbol).new
# router.add("get", "/users/:user_id", :index)
#
# # Will make @routes look like:

# {
# "get" => Fragment(T) # The fragment for this route
# }
# ```
#
# So if trying to match a POST request it will not even try because the request
# method does not match any of the known routes.
@routes = Hash(HttpMethod, Fragment(T)).new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could add a comment similar to this to help readers visualize how things are mapped to the router internals that'd be awesome. Could be done in a separate PR anytime. But this isn't a blocker so no worries if you don't have time. C

@paulcsmith paulcsmith merged commit 7314801 into luckyframework:master Aug 5, 2020
@matthewmcgarvey matthewmcgarvey deleted the routes-update branch August 5, 2020 19:22
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize for valid routes rather than missing ones
2 participants