From b474d06d5ea646c8145b07c8acbf1181c9e0b2aa Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 12 Dec 2013 18:40:21 -0700 Subject: Perf: save ~9% of object allocations on heavy requests. The per-thread registry is keyed on the class name, and each request for the class name returns a new string. This is in the hot path for a lot of Active Record behavior, so we easily accumulate thousands of repeated strings. To fix, we simply cache the key when the class is first extended with the module. TODO: Eliminate this module. The per-thread instance concept is common, but this technique confuses and obfuscates. --- activesupport/lib/active_support/per_thread_registry.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/per_thread_registry.rb b/activesupport/lib/active_support/per_thread_registry.rb index a5e7389d16..ca2e4d5625 100644 --- a/activesupport/lib/active_support/per_thread_registry.rb +++ b/activesupport/lib/active_support/per_thread_registry.rb @@ -32,12 +32,15 @@ module ActiveSupport # # If the class has an initializer, it must accept no arguments. module PerThreadRegistry + def self.extended(object) + object.instance_variable_set '@per_thread_registry_key', object.name.freeze + end + def instance - Thread.current[name] ||= new + Thread.current[@per_thread_registry_key] ||= 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| -- cgit v1.2.3