Skip to content

Instantly share code, notes, and snippets.

@byroot
Created December 18, 2014 20:36
Show Gist options
  • Save byroot/8361b2d85dfba45834ac to your computer and use it in GitHub Desktop.
Save byroot/8361b2d85dfba45834ac to your computer and use it in GitHub Desktop.
Calculating -------------------------------------
thread-safe 72.674k i/100ms
-------------------------------------------------
thread-safe 2.128M (± 5.8%) i/s - 10.610M
Calculating -------------------------------------
thread-unsafe 109.385k i/100ms
-------------------------------------------------
thread-unsafe 5.545M (± 7.9%) i/s - 27.565M
require 'benchmark/ips'
require 'thread_safe/cache'
cache = ThreadSafe::Cache.new
Benchmark.ips do |x|
x.report('thread-safe') { cache[:foo] = 42 }
end
# All MriCacheBackend methods are just synchronizing a mutex before calling the parent implementation
# In a non thread safe environment like Shopify, it is simply a waste of time.
%i(
[]= compute_if_absent compute_if_present compute merge_pair replace_pair
replace_if_exists get_and_set delete delete_pair clear
).each do |method|
ThreadSafe::MriCacheBackend.send(:define_method, method, ThreadSafe::NonConcurrentCacheBackend.instance_method(method))
end
Benchmark.ips do |x|
x.report('thread-unsafe') { cache[:foo] = 42 }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment