diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-04-22 16:19:41 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-04-22 17:02:39 +0900 |
commit | 89b86640901be4f423cef4059d970c0aa0adb648 (patch) | |
tree | a4a08390a4fd3c969292bd50d2d2a03e370a27ae /activerecord/lib/active_record | |
parent | 02b5b8cb4357fe0eda3a5cc0afcfe3ff9f300806 (diff) | |
download | rails-89b86640901be4f423cef4059d970c0aa0adb648.tar.gz rails-89b86640901be4f423cef4059d970c0aa0adb648.tar.bz2 rails-89b86640901be4f423cef4059d970c0aa0adb648.zip |
Remove useless `set_value` / `get_value` helper methods
Those helper methods makes relation values access 15% slower.
https://gist.github.com/kamipo/e64439f7a206e1c5b5c69d92d982828e
Before (02b5b8cb):
```
Warming up --------------------------------------
#limit_value 237.074k i/100ms
#limit_value = 1 222.052k i/100ms
Calculating -------------------------------------
#limit_value 6.477M (± 2.9%) i/s - 32.479M in 5.019475s
#limit_value = 1 5.297M (± 4.3%) i/s - 26.424M in 4.999933s
```
After (this change):
```
Warming up --------------------------------------
#limit_value 261.109k i/100ms
#limit_value = 1 239.646k i/100ms
Calculating -------------------------------------
#limit_value 7.412M (± 1.6%) i/s - 37.077M in 5.003345s
#limit_value = 1 6.134M (± 1.0%) i/s - 30.675M in 5.000908s
```
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 23 |
2 files changed, 10 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index ab24f67a6d..add95f6a0a 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -553,8 +553,8 @@ module ActiveRecord # # => ActiveRecord::ActiveRecordError: delete_all doesn't support distinct def delete_all invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method| - value = get_value(method) - SINGLE_VALUE_METHODS.include?(method) ? value : value.any? + value = @values[method] + method == :distinct ? value : value&.any? end if invalid_methods.any? raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}") diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 90b5e9a118..5d3cea6741 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -67,11 +67,13 @@ module ActiveRecord end class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{method_name} # def includes_values - get_value(#{name.inspect}) # get_value(:includes) + default = DEFAULT_VALUES[:#{name}] # default = DEFAULT_VALUES[:includes] + @values.fetch(:#{name}, default) # @values.fetch(:includes, default) end # end def #{method_name}=(value) # def includes_values=(value) - set_value(#{name.inspect}, value) # set_value(:includes, value) + assert_mutability! # assert_mutability! + @values[:#{name}] = value # @values[:includes] = value end # end CODE end @@ -417,7 +419,8 @@ module ActiveRecord if !VALID_UNSCOPING_VALUES.include?(scope) raise ArgumentError, "Called unscope() with invalid unscoping argument ':#{scope}'. Valid arguments are :#{VALID_UNSCOPING_VALUES.to_a.join(", :")}." end - set_value(scope, DEFAULT_VALUES[scope]) + assert_mutability! + @values[scope] = DEFAULT_VALUES[scope] when Hash scope.each do |key, target_value| if key != :where @@ -1005,17 +1008,6 @@ module ActiveRecord end private - # Returns a relation value with a given name - def get_value(name) - @values.fetch(name, DEFAULT_VALUES[name]) - end - - # Sets the relation value with the given name - def set_value(name, value) - assert_mutability! - @values[name] = value - end - def assert_mutability! raise ImmutableRelation if @loaded raise ImmutableRelation if defined?(@arel) && @arel @@ -1316,7 +1308,8 @@ module ActiveRecord def structurally_incompatible_values_for_or(other) values = other.values STRUCTURAL_OR_METHODS.reject do |method| - get_value(method) == values.fetch(method, DEFAULT_VALUES[method]) + default = DEFAULT_VALUES[method] + @values.fetch(method, default) == values.fetch(method, default) end end |