From 5035526b534a5d7c9d95eb6b66ade349c3a947f0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 10 Nov 2014 11:50:34 -0800 Subject: 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 --- lib/arel/visitors/depth_first.rb | 6 ++++++ lib/arel/visitors/visitor.rb | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'lib/arel/visitors') 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 -- cgit v1.2.3