Skip to content

Commit 8632cc6

Browse files
committed
Make route mapping scope lookups faster
Scopes are backed by an array of hashes, but we could make lookup faster by just merging inherited values into the immediate hash. This us from needless iterations for deeply nested routes.
1 parent f91b940 commit 8632cc6

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Speed up `ActionDispatch::Routing::Mapper::Scope#[]` by merging frame hashes.
2+
3+
*Gannon McGibbon*
4+
15
* Allow bots to ignore `allow_browser`.
26

37
*Matthew Nguyen*

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,9 +2293,9 @@ class Scope # :nodoc:
22932293

22942294
attr_reader :parent, :scope_level
22952295

2296-
def initialize(hash, parent = NULL, scope_level = nil)
2297-
@hash = hash
2296+
def initialize(hash, parent = ROOT, scope_level = nil)
22982297
@parent = parent
2298+
@hash = parent ? parent.frame.merge(hash) : hash
22992299
@scope_level = scope_level
23002300
end
23012301

@@ -2308,7 +2308,7 @@ def null?
23082308
end
23092309

23102310
def root?
2311-
@parent.null?
2311+
@parent == ROOT
23122312
end
23132313

23142314
def resources?
@@ -2353,23 +2353,22 @@ def new_level(level)
23532353
end
23542354

23552355
def [](key)
2356-
scope = find { |node| node.frame.key? key }
2357-
scope && scope.frame[key]
2356+
frame[key]
23582357
end
23592358

2359+
def frame; @hash; end
2360+
23602361
include Enumerable
23612362

23622363
def each
23632364
node = self
2364-
until node.equal? NULL
2365+
until node.equal? ROOT
23652366
yield node
23662367
node = node.parent
23672368
end
23682369
end
23692370

2370-
def frame; @hash; end
2371-
2372-
NULL = Scope.new(nil, nil)
2371+
ROOT = Scope.new({}, nil)
23732372
end
23742373

23752374
def initialize(set) # :nodoc:

0 commit comments

Comments
 (0)