Closed chopraanmol1 closed 6 years ago
Benchmarking Script
require 'memory_profiler'
require 'request_store'
require 'benchmark/ips'
module RequestStore
def self.fetch_without_proc_creation(key)
# Suggested
store[key] = yield unless exist?(key)
store[key]
end
def self.fetch_with_proc_creation(key, &block)
# current
store[key] = yield unless exist?(key)
store[key]
end
end ; nil
Benchmark.ips do |x|
x.config(:time => 5, :warmup => 2)
x.report("with &block") { RequestStore.fetch_with_proc_creation(:a){0} }
x.report("without &block") {RequestStore.fetch_without_proc_creation(:b){1}}
x.compare!
end ; nil
MemoryProfiler.report{ 100.times{ RequestStore.fetch_with_proc_creation(:aa){0} } }.pretty_print(detailed_report: false, allocated_strings: false, retained_strings: 0)
MemoryProfiler.report{ 100.times{ RequestStore.fetch_without_proc_creation(:bb){1} } }.pretty_print(detailed_report: false, allocated_strings: false, retained_strings: 0)
Neat! I am happy to merge, but can't release a new version right this second; expect me to do so within a week. Thank you so much!
Ya sure no problem
Hey, @steveklabnik just wanted to remind you about merging this PR as it has already been 2 weeks.
Yes, sorry. cutting a release. sorry for the delay.
published as 1.4.1
Nice work @chopraanmol1!
&block inside method definitions creates additional proc which consumes more time and memory.
One of the usecase of RequestStore is to memoize value on per request basis. Currently, in this scenario while using RequestStore#fetch unused proc will be created multiple times
Benchmark: