aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/journey/nodes
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2015-08-17 15:57:06 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2015-08-17 15:57:06 -0700
commit559e7f94506ac3c3e8553f9510f43923e7c804da (patch)
tree26998a77988644fb7e99f791fe91d4c157b32906 /actionpack/lib/action_dispatch/journey/nodes
parent8d7b883f33034732996f80f73f46d050c7dc9210 (diff)
downloadrails-559e7f94506ac3c3e8553f9510f43923e7c804da.tar.gz
rails-559e7f94506ac3c3e8553f9510f43923e7c804da.tar.bz2
rails-559e7f94506ac3c3e8553f9510f43923e7c804da.zip
drop object allocation during routes setup
This commit introduces a functional Path AST visitor and implements `each` on the AST in terms of the functional visitor. The functional visitor doesn't maintain state, so we only need to allocate one of them. Given this benchmark route file: ```ruby require 'action_pack' require 'action_dispatch' route_set = ActionDispatch::Routing::RouteSet.new routes = ActionDispatch::Routing::Mapper.new route_set ObjectSpace::AllocationTracer.setup(%i{path line type}) result = ObjectSpace::AllocationTracer.trace do 500.times{|i| routes.resource :omglol } end result.find_all { |k,v| k.first =~ /git\/rails/ }.sort_by { |k,v| v.first }.each { |k,v| p k => v } ``` node.rb line 17 was in our top 3 allocation spot: ``` {["/Users/aaron/git/rails/actionpack/lib/action_dispatch/journey/nodes/node.rb", 17, :T_OBJECT]=>[31526, 0, 28329, 0, 2, 1123160]} {["/Users/aaron/git/rails/actionpack/lib/action_dispatch/routing/mapper.rb", 2080, :T_IMEMO]=>[34002, 0, 30563, 0, 2, 1211480]} {["/Users/aaron/git/rails/actionpack/lib/action_dispatch/routing/mapper.rb", 2071, :T_IMEMO]=>[121934, 1, 109608, 0, 7, 4344400]} ``` This commit eliminates allocations at that place.
Diffstat (limited to 'actionpack/lib/action_dispatch/journey/nodes')
-rw-r--r--actionpack/lib/action_dispatch/journey/nodes/node.rb6
1 files changed, 3 insertions, 3 deletions
diff --git a/actionpack/lib/action_dispatch/journey/nodes/node.rb b/actionpack/lib/action_dispatch/journey/nodes/node.rb
index 2b57c368dc..542e8c6577 100644
--- a/actionpack/lib/action_dispatch/journey/nodes/node.rb
+++ b/actionpack/lib/action_dispatch/journey/nodes/node.rb
@@ -14,15 +14,15 @@ module ActionDispatch
end
def each(&block)
- Visitors::Each.new(block).accept(self)
+ Visitors::Each::INSTANCE.accept(self, block)
end
def to_s
- Visitors::String.new.accept(self)
+ Visitors::String::INSTANCE.accept(self, '')
end
def to_dot
- Visitors::Dot.new.accept(self)
+ Visitors::Dot::INSTANCE.accept(self)
end
def to_sym