mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
5584ddc43d
This saved about 46 array allocations per request on an extremely simple application. The delegation happened in the notification subsystem which is a hotspot, so this should result in even more savings with larger apps. Squashed commit of the following: commit 41eef0d1479526f7de25fd4391d98e61c126d9f5 Author: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed Nov 6 16:32:31 2013 -0800 speed up notifications commit 586b4a18656f66fb2c518fb8e8fee66a016e8ae6 Author: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed Nov 6 16:31:05 2013 -0800 speed up runtime registry methods commit b67d074cb4314df9a88438f785868cef77e583d7 Author: Aaron Patterson <aaron.patterson@gmail.com> Date: Wed Nov 6 16:28:12 2013 -0800 change method name and make it public
50 lines
1.5 KiB
Ruby
50 lines
1.5 KiB
Ruby
module ActiveSupport
|
|
# This module is used to encapsulate access to thread local variables.
|
|
#
|
|
# Instead of polluting the thread locals namespace:
|
|
#
|
|
# Thread.current[:connection_handler]
|
|
#
|
|
# you define a class that extends this module:
|
|
#
|
|
# module ActiveRecord
|
|
# class RuntimeRegistry
|
|
# extend ActiveSupport::PerThreadRegistry
|
|
#
|
|
# attr_accessor :connection_handler
|
|
# end
|
|
# end
|
|
#
|
|
# and invoke the declared instance accessors as class methods. So
|
|
#
|
|
# ActiveRecord::RuntimeRegistry.connection_handler = connection_handler
|
|
#
|
|
# sets a connection handler local to the current thread, and
|
|
#
|
|
# ActiveRecord::RuntimeRegistry.connection_handler
|
|
#
|
|
# returns a connection handler local to the current thread.
|
|
#
|
|
# This feature is accomplished by instantiating the class and storing the
|
|
# instance as a thread local keyed by the class name. In the example above
|
|
# a key "ActiveRecord::RuntimeRegistry" is stored in <tt>Thread.current</tt>.
|
|
# The class methods proxy to said thread local instance.
|
|
#
|
|
# If the class has an initializer, it must accept no arguments.
|
|
module PerThreadRegistry
|
|
def instance
|
|
Thread.current[name] ||= new
|
|
end
|
|
|
|
protected
|
|
|
|
def method_missing(name, *args, &block) # :nodoc:
|
|
# Caches the method definition as a singleton method of the receiver.
|
|
define_singleton_method(name) do |*a, &b|
|
|
instance.public_send(name, *a, &b)
|
|
end
|
|
|
|
send(name, *args, &block)
|
|
end
|
|
end
|
|
end
|