Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit b474d06d5ea646c8145b07c8acbf1181c9e0b2aa 1 parent 1792293
Jeremy Kemper jeremy authored

Showing 1 changed file with 5 additions and 2 deletions. Show diff stats Hide diff stats

  1. +5 2 activesupport/lib/active_support/per_thread_registry.rb
7 activesupport/lib/active_support/per_thread_registry.rb
@@ -32,12 +32,15 @@ module ActiveSupport
32 32 #
33 33 # If the class has an initializer, it must accept no arguments.
34 34 module PerThreadRegistry
  35 + def self.extended(object)
  36 + object.instance_variable_set '@per_thread_registry_key', object.name.freeze
  37 + end
  38 +
35 39 def instance
36   - Thread.current[name] ||= new
  40 + Thread.current[@per_thread_registry_key] ||= new
37 41 end
38 42
39 43 protected
40   -
41 44 def method_missing(name, *args, &block) # :nodoc:
42 45 # Caches the method definition as a singleton method of the receiver.
43 46 define_singleton_method(name) do |*a, &b|

0 comments on commit b474d06

Please sign in to comment.
Something went wrong with that request. Please try again.