Skip to content

Commit 6e15d5a

Browse files
committed
Rm array allocation in {en,dis}able_query_cache!
When `dirties` was [added][1] as an option to `uncached`, `{en,dis}able_query_cache!` were changed to use multiple assignment. However, multiple assignment allocates an array and is not really necessary for these methods. [1]: 5d528ba Surprisingly, this showed up in a production profile as a large allocator. Reproduction: ```ruby require "active_record" require "memory_profiler" ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") report = MemoryProfiler.report do 10000.times do ActiveRecord::Base.connection.enable_query_cache! end end report.pretty_print ``` Before: ``` bundle exec ruby eqc.rb | rg "allocated objects by class" -A 10 allocated objects by class ----------------------------------- 10093 Array 1551 String 12 Class 7 Hash 3 File 2 Proc 2 Thread::Backtrace 2 Thread::Backtrace::Location 2 Thread::Mutex ``` After: ``` bundle exec ruby eqc.rb | rg "allocated objects by class" -A 10 allocated objects by class ----------------------------------- 1551 String 93 Array 12 Class 7 Hash 3 File 2 Proc 2 Thread::Backtrace 2 Thread::Backtrace::Location 2 Thread::Mutex ```
1 parent dee911b commit 6e15d5a

File tree

1 file changed

+4
-2
lines changed
  • activerecord/lib/active_record/connection_adapters/abstract

1 file changed

+4
-2
lines changed

activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,13 @@ def enable_query_cache
157157
end
158158

159159
def enable_query_cache!
160-
query_cache.enabled, query_cache.dirties = true, true
160+
query_cache.enabled = true
161+
query_cache.dirties = true
161162
end
162163

163164
def disable_query_cache!
164-
query_cache.enabled, query_cache.dirties = false, true
165+
query_cache.enabled = false
166+
query_cache.dirties = true
165167
end
166168

167169
def query_cache_enabled

0 commit comments

Comments
 (0)