aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-11-10 11:50:34 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2014-11-10 11:50:34 -0800
commit5035526b534a5d7c9d95eb6b66ade349c3a947f0 (patch)
treedfa6336fc7c3f527ff16a8d5eedac727db778c01
parent9eb4a1c633894a367ad99e93e4e0000f7acf1e50 (diff)
downloadrails-5035526b534a5d7c9d95eb6b66ade349c3a947f0.tar.gz
rails-5035526b534a5d7c9d95eb6b66ade349c3a947f0.tar.bz2
rails-5035526b534a5d7c9d95eb6b66ade349c3a947f0.zip
cache the dispatch table on the depth first visitor
We know the API for the depth first visitor in advance, so it's OK to calcuate this cache in advance
-rw-r--r--lib/arel/visitors/depth_first.rb6
-rw-r--r--lib/arel/visitors/visitor.rb21
2 files changed, 21 insertions, 6 deletions
diff --git a/lib/arel/visitors/depth_first.rb b/lib/arel/visitors/depth_first.rb
index a434f404c7..d7d85cfcc6 100644
--- a/lib/arel/visitors/depth_first.rb
+++ b/lib/arel/visitors/depth_first.rb
@@ -173,6 +173,12 @@ module Arel
def visit_Hash o
o.each { |k,v| visit(k); visit(v) }
end
+
+ DISPATCH = dispatch_cache
+
+ def get_dispatch_cache
+ DISPATCH
+ end
end
end
end
diff --git a/lib/arel/visitors/visitor.rb b/lib/arel/visitors/visitor.rb
index 146ae216f6..2152da9f05 100644
--- a/lib/arel/visitors/visitor.rb
+++ b/lib/arel/visitors/visitor.rb
@@ -2,7 +2,17 @@ module Arel
module Visitors
class Visitor
def initialize
- @dispatch = Hash.new do |hash, class_name|
+ @dispatch = get_dispatch_cache
+ end
+
+ def accept object
+ visit object
+ end
+
+ private
+
+ def self.dispatch_cache
+ dispatch = Hash.new do |hash, class_name|
hash[class_name] = "visit_#{(class_name || '').gsub('::', '_')}"
end
@@ -10,16 +20,15 @@ module Arel
# instance, but we can do that later.
self.class.private_instance_methods.sort.each do |name|
next unless name =~ /^visit_(.*)$/
- @dispatch[$1.gsub('_', '::')] = name
+ dispatch[$1.gsub('_', '::')] = name
end
+ dispatch
end
- def accept object
- visit object
+ def get_dispatch_cache
+ self.class.dispatch_cache
end
- private
-
def dispatch
@dispatch
end