Skip to content
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

speed up new router implementation #279

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/hanami/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ def _params(env, params)
end

env[PARAMS].merge!(::Rack::Utils.parse_nested_query(env[::Rack::QUERY_STRING]))
env[PARAMS].merge!(params)
env[PARAMS].merge!(params) unless params.empty?
Copy link
Member

Choose a reason for hiding this comment

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

Is this change related? If the params are often not empty, then this check is slower than just merging the empty hash. If if it's often empty then it can be a useful optimization, but I think we should discuss that separately to decide if the tradeoff is worth it. I guess it might be negligible either way.

env[PARAMS] = Params.deep_symbolize(env[PARAMS])
env
end
Expand Down
13 changes: 2 additions & 11 deletions lib/hanami/router/leaf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ class Leaf
#
# @api private
# @since 2.2.0
attr_reader :to, :params
attr_reader :to, :params, :matcher

# @api private
# @since 2.2.0
def initialize(route, to, constraints)
@route = route
@to = to
@constraints = constraints
@params = nil
@matcher = Mustermann.new(route, type: :rails, version: "5.0", capture: constraints)
end

# @api private
Expand All @@ -29,14 +28,6 @@ def match(path)

match
end

private

# @api private
# @since 2.2.0
def matcher
@matcher ||= Mustermann.new(@route, type: :rails, version: "5.0", capture: @constraints)
end
end
end
end
10 changes: 8 additions & 2 deletions lib/hanami/router/trie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Trie
# @since 2.0.0
def initialize
@root = Node.new
@segments_map = {}
end

# @api private
Expand Down Expand Up @@ -47,15 +48,20 @@ def find(path)

# @api private
# @since 2.0.0
SEGMENT_SEPARATOR = /\//
SEGMENT_SEPARATOR = "/"
private_constant :SEGMENT_SEPARATOR

# @api private
# @since 2.2.0
def segments_from(path)
if @segments_map[path]
@segments_map[path]
else
_, *segments = path.split(SEGMENT_SEPARATOR)


Copy link
Member

Choose a reason for hiding this comment

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

There's an empty line here that should be removed, and the contents of the else branch need to be indented

@segments_map[path] = segments
segments
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
segments

Hash assignment returns the value being assigned so I don't think this last line does anything

end
end
end
end
Expand Down
Loading