public
Created

  • Download Gist
test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
 
require "benchmark"
 
class Module
def delegate_new(*methods)
options = methods.pop
unless options.is_a?(Hash) && to = options[:to]
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
end
 
if options[:prefix] == true && to.to_s =~ /^[^a-z_]/
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
end
 
prefix = options[:prefix] ? "#{options[:prefix] == true ? to : options[:prefix]}_" : ''
 
file, line = caller.first.split(':', 2)
line = line.to_i
 
methods.each do |method|
method_name = prefix + method.to_s
module_eval(<<-EOS, file, line - 1)
def #{method_name}(*args, &block)
ActiveSupport::Delegation.perform(self, #{to}, #{method.inspect}, #{method_name.inspect}, #{options[:allow_nil].inspect}, #{options[:to].inspect}, args, block)
end
EOS
end
end
 
 
def delegate_old(*methods)
options = methods.pop
unless options.is_a?(Hash) && to = options[:to]
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
end
 
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
end
 
prefix = options[:prefix] ? "#{options[:prefix] == true ? to : options[:prefix]}_" : ''
 
file, line = caller.first.split(':', 2)
line = line.to_i
 
methods.each do |method|
on_nil =
if options[:allow_nil]
'return'
else
%(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
end
 
module_eval(<<-EOS, file, line - 5)
def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
#{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)
rescue NoMethodError # rescue NoMethodError
if #{to}.nil? # if client.nil?
#{on_nil} # return # depends on :allow_nil
else # else
raise # raise
end # end
end # end
EOS
end
end
 
end
 
module ActiveSupport
module Delegation #:nodoc:
def self.perform(object, target, method, method_name, allow_nil, to, args, block)
target.__send__(method, *args, &block)
rescue NoMethodError
if target.nil?
return nil if allow_nil
raise "#{object.class}##{method_name} delegated to #{to}.#{method}, but #{to} is nil: #{object.inspect}"
else
raise
end
end
end
end
 
 
AMOUNT = 1000
class A
AMOUNT.times do |i|
define_method("hello#{i}") do
nil
end
end
end
class Old
def initialize
@a = A.new
end
end
 
class New
def initialize
@a = A.new
end
end
 
Benchmark.bmbm do |x|
x.report do
AMOUNT.times do |i|
Old.delegate_old "hello#{i}", :to => "@a"
end
end
x.report do
AMOUNT.times do |i|
New.delegate_new "hello#{i}", :to => "@a"
end
end
end
 
o = Old.new
n = New.new
 
 
Benchmark.bmbm do |x|
x.report do
AMOUNT.times do |i|
o.send("hello#{i}")
end
end
x.report do
AMOUNT.times do |i|
n.send("hello#{i}")
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.