Skip to content

Instantly share code, notes, and snippets.

@byroot
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byroot/919e7756be7f89ec19af to your computer and use it in GitHub Desktop.
Save byroot/919e7756be7f89ec19af to your computer and use it in GitHub Desktop.
$ bundle exec ruby per_thread_registry_benchmark.rb
Calculating -------------------------------------
thread-safe 25.176k i/100ms
thread-unsafe 30.069k i/100ms
-------------------------------------------------
thread-safe 1.480M (± 6.5%) i/s - 7.377M
thread-unsafe 2.480M (± 7.2%) i/s - 12.328M
ruby per_thread_registry_benchmark.rb
Calculating -------------------------------------
thread-safe 25.525k i/100ms
thread-unsafe 29.837k i/100ms
-------------------------------------------------
thread-safe 1.691M (± 7.5%) i/s - 8.398M
thread-unsafe 2.440M (± 7.8%) i/s - 12.114M
require 'active_support/all'
require 'benchmark/ips'
class ThreadSafeRegistry
extend ActiveSupport::PerThreadRegistry
attr_accessor :foo
end
module GlobalRegistry
include ActiveSupport::PerThreadRegistry
private
def instance
@instance ||= new
end
def per_thread_registry_instance
@instance ||= new
end
end
class ThreadUnsafeRegistry
extend GlobalRegistry
attr_accessor :foo
end
ActiveSupport::PerThreadRegistry.send(:define_method, :instance, GlobalRegistry.instance_method(:instance))
ActiveSupport::PerThreadRegistry.module_eval { private :instance }
Benchmark.ips do |x|
x.report('thread-safe') { ThreadSafeRegistry.foo }
x.report('thread-unsafe') { ThreadUnsafeRegistry.foo }
end
$ ruby monkey_patch.rb
Calculating -------------------------------------
thread-safe 30.865k i/100ms
thread-unsafe 30.602k i/100ms
-------------------------------------------------
thread-safe 2.725M (± 7.8%) i/s - 13.519M
thread-unsafe 2.765M (± 7.3%) i/s - 13.740M
require 'active_support/all'
require 'benchmark/ips'
class ThreadSafeRegistry
extend ActiveSupport::PerThreadRegistry
attr_accessor :foo
end
module GlobalRegistry
include ActiveSupport::PerThreadRegistry
private
def instance
@instance ||= new
end
def per_thread_registry_instance
@instance ||= new
end
end
class ThreadUnsafeRegistry
extend GlobalRegistry
attr_accessor :foo
end
Benchmark.ips do |x|
x.report('thread-safe') { ThreadSafeRegistry.foo }
x.report('thread-unsafe') { ThreadUnsafeRegistry.foo }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment