-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwarding_bench.rb
114 lines (87 loc) · 1.86 KB
/
forwarding_bench.rb
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
# frozen_string_literal: true
require_relative '../../lib/ruby-on-speed'
RubyOnSpeed.benchmark 'Ruby:forwarding - forward a call' do
test_truthy_results!
class CallOrigin
attr_accessor :value
def initialize
@value = 21
end
def caller
CallCaller.new(-> { @value }, ->(arg) { @value = arg })
end
end
class CallShadow
attr_reader :origin, :entity, :entity_setter
def initialize(origin, entity)
@origin = origin
@entity = entity
@entity_setter = "#{entity}="
end
def value
origin.__send__(entity)
end
def value=(arg)
origin.__send__(entity_setter, arg)
end
end
class CallFaker
attr_reader :origin
def initialize(origin, entity)
@origin = origin
@entity = "@#{entity}"
end
def value
@origin.instance_variable_get(@entity)
end
def value=(arg)
@origin.instance_variable_set(@entity, arg)
end
end
class CallCaller
def initialize(getter, setter)
@getter, @setter = getter, setter
end
def value
@getter.call
end
def value=(arg)
@setter.call(arg)
end
end
origin = CallOrigin.new
shadow = CallShadow.new(origin, :value)
faker = CallFaker.new(origin, :value)
caller = origin.caller
caller2 = CallCaller.new(-> { origin.value }, ->(arg) { origin.value = arg })
code 'origin#value' do
origin.value
end
code 'origin#value=' do
origin.value = 42
end
code 'shadow#value' do
shadow.value
end
code 'shadow#value=' do
shadow.value = 42
end
code 'faker#value' do
faker.value
end
code 'faker#value=' do
faker.value = 42
end
code 'caller#value' do
caller.value
end
code 'caller#value=' do
caller.value = 42
end
code 'caller2#value' do
caller2.value
end
code 'caller2#value=' do
caller2.value = 42
end
end