aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2015-08-18 15:12:35 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2015-08-18 15:12:44 -0700
commit01d88953e2b4049c0a13f6f2343403bd71696b31 (patch)
treeb0e095cf60b0eb503fc8e09a0b6bbedb79c175b6
parenta31686021b127f2586588a7569f08f17296e66d3 (diff)
downloadrails-01d88953e2b4049c0a13f6f2343403bd71696b31.tar.gz
rails-01d88953e2b4049c0a13f6f2343403bd71696b31.tar.bz2
rails-01d88953e2b4049c0a13f6f2343403bd71696b31.zip
drop string allocations for each resource
Eagerly calculate and cache the name of Symbol objects in the path AST. This drops about 26 string allocations per resource: ```ruby require 'action_pack' require 'action_dispatch' require 'benchmark/ips' 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 do routes.resources :foo end end sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last) sorted.each do |k,v| next if v == 0 p k => v end __END__ Before: {:T_SYMBOL=>11} {:T_REGEXP=>17} {:T_STRUCT=>6500} {:T_MATCH=>12004} {:T_OBJECT=>99009} {:T_DATA=>116084} {:T_HASH=>122015} {:T_STRING=>172647} {:T_IMEMO=>371132} {:T_ARRAY=>433056} After: {:T_SYMBOL=>11} {:T_REGEXP=>17} {:T_STRUCT=>6500} {:T_MATCH=>12004} {:T_OBJECT=>99009} {:T_DATA=>100088} {:T_HASH=>122015} {:T_STRING=>159637} {:T_IMEMO=>363134} {:T_ARRAY=>433056} ```
-rw-r--r--actionpack/lib/action_dispatch/journey/nodes/node.rb2
1 files changed, 2 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/journey/nodes/node.rb b/actionpack/lib/action_dispatch/journey/nodes/node.rb
index 542e8c6577..35fe456a3e 100644
--- a/actionpack/lib/action_dispatch/journey/nodes/node.rb
+++ b/actionpack/lib/action_dispatch/journey/nodes/node.rb
@@ -72,11 +72,13 @@ module ActionDispatch
class Symbol < Terminal # :nodoc:
attr_accessor :regexp
alias :symbol :regexp
+ attr_reader :name
DEFAULT_EXP = /[^\.\/\?]+/
def initialize(left)
super
@regexp = DEFAULT_EXP
+ @name = left.tr '*:'.freeze, ''.freeze
end
def default_regexp?