Skip to content

Commit 58df9a4

Browse files
committed
Generate 45% faster query methods
`DEFAULT_VALUES` access is a bit slower, avoiding that makes about 45% faster query methods. https://gist.github.com/kamipo/e64439f7a206e1c5b5c69d92d982828e Before (ff299f1): ``` Warming up -------------------------------------- #limit_value 263.800k i/100ms #limit_value = 1 241.758k i/100ms Calculating ------------------------------------- #limit_value 7.044M (± 1.5%) i/s - 35.349M in 5.019511s #limit_value = 1 6.044M (± 2.3%) i/s - 30.220M in 5.003292s ``` After (this change): ``` Warming up -------------------------------------- #limit_value 246.295k i/100ms #limit_value = 1 244.398k i/100ms Calculating ------------------------------------- #limit_value 10.303M (± 2.1%) i/s - 51.722M in 5.022568s #limit_value = 1 6.021M (± 6.1%) i/s - 30.061M in 5.015545s ```
1 parent ff299f1 commit 58df9a4

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,25 @@ def not_behaves_as_nor?(opts)
8383
FROZEN_EMPTY_HASH = {}.freeze
8484

8585
Relation::VALUE_METHODS.each do |name|
86-
method_name = \
86+
method_name, default =
8787
case name
88-
when *Relation::MULTI_VALUE_METHODS then "#{name}_values"
89-
when *Relation::SINGLE_VALUE_METHODS then "#{name}_value"
90-
when *Relation::CLAUSE_METHODS then "#{name}_clause"
88+
when *Relation::MULTI_VALUE_METHODS
89+
["#{name}_values", "FROZEN_EMPTY_ARRAY"]
90+
when *Relation::SINGLE_VALUE_METHODS
91+
["#{name}_value", name == :create_with ? "FROZEN_EMPTY_HASH" : "nil"]
92+
when *Relation::CLAUSE_METHODS
93+
["#{name}_clause", "DEFAULT_VALUES[:#{name}]"]
9194
end
95+
9296
class_eval <<-CODE, __FILE__, __LINE__ + 1
93-
def #{method_name} # def includes_values
94-
default = DEFAULT_VALUES[:#{name}] # default = DEFAULT_VALUES[:includes]
95-
@values.fetch(:#{name}, default) # @values.fetch(:includes, default)
96-
end # end
97-
98-
def #{method_name}=(value) # def includes_values=(value)
99-
assert_mutability! # assert_mutability!
100-
@values[:#{name}] = value # @values[:includes] = value
101-
end # end
97+
def #{method_name} # def includes_values
98+
@values.fetch(:#{name}, #{default}) # @values.fetch(:includes, FROZEN_EMPTY_ARRAY)
99+
end # end
100+
101+
def #{method_name}=(value) # def includes_values=(value)
102+
assert_mutability! # assert_mutability!
103+
@values[:#{name}] = value # @values[:includes] = value
104+
end # end
102105
CODE
103106
end
104107

0 commit comments

Comments
 (0)