Project

General

Profile

Feature #17055 ยป t.rb

Realistic benchmark for uninitialized instance variables - jeremyevans0 (Jeremy Evans), 08/03/2020 04:28 PM

 
1
require 'benchmark/ips'
2
require 'sequel'
3

    
4
if ARGV.last == 'plugin'
5
  class Sequel::Model
6
    plugin :accessed_columns
7
    plugin :active_model
8
    plugin :association_pks
9
    plugin :auto_validations
10
    plugin :columns_updated
11
    plugin :composition
12
    plugin :dirty
13
    plugin :forbid_lazy_load
14
    plugin :insert_conflict
15
    plugin :instance_filters
16
    plugin :instance_hooks
17
    plugin :json_serializer
18
    plugin :serialization
19
    plugin :serialization_modification_detection
20
    plugin :tactical_eager_loading
21
    plugin :update_primary_key
22
    plugin :validation_contexts
23
  end
24
end
25

    
26
if ARGV.first == 'eager_initialize'
27
  module Sequel::Model::ClassMethods
28
    def call(values)
29
      o = allocate
30
      o.instance_variable_set(:@values, values)
31
      o.instance_variable_set(:@new, nil)
32
      o.instance_variable_set(:@modified, nil)
33
      o.instance_variable_set(:@singleton_setter_added, nil)
34
      o.instance_variable_set(:@errors, nil)
35
      o.instance_variable_set(:@this, nil)
36
      o.instance_variable_set(:@server, nil)
37
      o.instance_variable_set(:@skip_validation_on_next_save, nil)
38
      o.instance_variable_set(:@changed_columns, nil)
39
      o
40
    end
41
  end 
42

    
43
  module Sequel::Model::Associations::ClassMethods
44
    def call(values)
45
      o = super
46
      o.instance_variable_set(:@associations, nil)
47
      o.instance_variable_set(:@set_associated_object_if_same, nil)
48
      o
49
    end
50
  end
51

    
52
  if ARGV.last == 'plugin'
53
    module Sequel::Plugins
54
      module AccessedColumns
55
        module ClassMethods
56
          def call(values)
57
            o = super
58
            o.instance_variable_set(:@accessed_columns, nil)
59
            o
60
          end
61
        end
62
      end
63

    
64
      module ActiveModel
65
        module ClassMethods
66
          def call(values)
67
            o = super
68
            o.instance_variable_set(:@_to_partial_path, nil)
69
            o.instance_variable_set(:@destroyed, nil)
70
            o.instance_variable_set(:@rollback_checker, nil)
71
            o
72
          end
73
        end
74
      end
75

    
76
      module AssociationPks
77
        module ClassMethods
78
          def call(values)
79
            o = super
80
            o.instance_variable_set(:@_association_pks, nil)
81
            o
82
          end
83
        end
84
      end
85

    
86
      module AutoValidations
87
        module ClassMethods
88
          def call(values)
89
            o = super
90
            o.instance_variable_set(:@_skip_auto_validations, nil)
91
            o
92
          end
93
        end
94
      end
95

    
96
      module ColumnsUpdated
97
        module ClassMethods
98
          def call(values)
99
            o = super
100
            o.instance_variable_set(:@columns_updated, nil)
101
            o
102
          end
103
        end
104
      end
105

    
106
      module Composition
107
        module ClassMethods
108
          def call(values)
109
            o = super
110
            o.instance_variable_set(:@compositions, nil)
111
            o
112
          end
113
        end
114
      end
115

    
116
      module Dirty
117
        module ClassMethods
118
          def call(values)
119
            o = super
120
            o.instance_variable_set(:@initial_values, nil)
121
            o.instance_variable_set(:@missing_initial_values, nil)
122
            o.instance_variable_set(:@previous_changes, nil)
123
            o
124
          end
125
        end
126
      end
127

    
128
      module ForbidLazyLoad
129
        module ClassMethods
130
          def call(values)
131
            o = super
132
            o.instance_variable_set(:@forbid_lazy_load, nil)
133
            o
134
          end
135
        end
136
      end
137

    
138
      module InsertConflict
139
        module ClassMethods
140
          def call(values)
141
            o = super
142
            o.instance_variable_set(:@insert_conflict_opts, nil)
143
            o
144
          end
145
        end
146
      end
147

    
148
      module InstanceFilters
149
        module ClassMethods
150
          def call(values)
151
            o = super
152
            o.instance_variable_set(:@instance_filters, nil)
153
            o
154
          end
155
        end
156
      end
157

    
158
      module InstanceHooks
159
        module ClassMethods
160
          def call(values)
161
            o = super
162
            o.instance_variable_set(:@instance_hooks, nil)
163
            o
164
          end
165
        end
166
      end
167

    
168
      module JsonSerializer
169
        module ClassMethods
170
          def call(values)
171
            o = super
172
            o.instance_variable_set(:@json_serializer_opts, nil)
173
            o
174
          end
175
        end
176
      end
177

    
178
      module Serialization
179
        module ClassMethods
180
          def call(values)
181
            o = super
182
            o.instance_variable_set(:@deserialized_values, nil)
183
            o
184
          end
185
        end
186
      end
187

    
188
      module SerializationModificationDetection
189
        module ClassMethods
190
          def call(values)
191
            o = super
192
            o.instance_variable_set(:@original_deserialized_values, nil)
193
            o
194
          end
195
        end
196
      end
197

    
198
      module TacticalEagerLoading
199
        module ClassMethods
200
          def call(values)
201
            o = super
202
            o.instance_variable_set(:@retrieved_by, nil)
203
            o.instance_variable_set(:@retrieved_with, nil)
204
            o
205
          end
206
        end
207
      end
208

    
209
      module UpdatePrimaryKey
210
        module ClassMethods
211
          def call(values)
212
            o = super
213
            o.instance_variable_set(:@pk_hash, nil)
214
            o
215
          end
216
        end
217
      end
218

    
219
      module ValidationContexts
220
        module ClassMethods
221
          def call(values)
222
            o = super
223
            o.instance_variable_set(:@validation_context, nil)
224
            o
225
          end
226
        end
227
      end
228
    end
229
  end
230
end
231

    
232
DB = Sequel.connect(ENV["DATABASE_URL"] || 'sqlite:/')
233

    
234
DB.create_table!(:ts){String :s}
235

    
236
class T < Sequel::Model; end
237

    
238
DB[:ts].import([:s], [[""]] * 1000)
239

    
240
Benchmark.ips do |x|
241
  x.report("Retrieve 1000 rows"){T.all}
242
end
243