25faa5466b
This makes the value supplied to the `default` option of `thread_mattr_accessor` to be set in descendant classes as well as in any new Thread that starts. Previously, the `default` value provided was set only at the moment of defining the attribute writer, which would cause the attribute to be uninitialized in descendants and in other threads. For instance: ```ruby class Processor thread_mattr_accessor :mode, default: :smart end class SubProcessor < Processor end SubProcessor.mode # => :smart (was `nil` prior to this commit) Thread.new do Processor.mode # => :smart (was `nil` prior to this commit) end.join ``` If a non-`nil` default has been specified, there is a small (~7%) performance decrease when reading non-`nil` values, and a larger (~45%) performance decrease when reading `nil` values. Benchmark script: ```ruby # frozen_string_literal: true require "benchmark/ips" require "active_support" require "active_support/core_ext/module/attribute_accessors_per_thread" class MyClass thread_mattr_accessor :default_value, default: "default" thread_mattr_accessor :string_value, default: "default" thread_mattr_accessor :nil_value, default: "default" end MyClass.string_value = "string" MyClass.nil_value = nil Benchmark.ips do |x| x.report("default_value") { MyClass.default_value } x.report("string_value") { MyClass.string_value } x.report("nil_value") { MyClass.nil_value } end ``` Before this commit: ``` default_value 2.075M (± 0.7%) i/s - 10.396M in 5.010585s string_value 2.103M (± 0.7%) i/s - 10.672M in 5.074624s nil_value 1.777M (± 0.9%) i/s - 8.924M in 5.023058s ``` After this commit: ``` default_value 2.008M (± 0.7%) i/s - 10.187M in 5.072990s string_value 1.967M (± 0.7%) i/s - 9.891M in 5.028570s nil_value 1.144M (± 0.5%) i/s - 5.770M in 5.041630s ``` If no default or a `nil` default is specified, there is no performance impact. Fixes #43312. Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> |
||
---|---|---|
.. | ||
bin | ||
lib | ||
test | ||
.gitignore | ||
activesupport.gemspec | ||
CHANGELOG.md | ||
MIT-LICENSE | ||
Rakefile | ||
README.rdoc |
= Active Support -- Utility classes and Ruby extensions from Rails Active Support is a collection of utility classes and standard library extensions that were found useful for the Rails framework. These additions reside in this package so they can be loaded as needed in Ruby projects outside of Rails. You can read more about the extensions in the {Active Support Core Extensions}[https://edgeguides.rubyonrails.org/active_support_core_extensions.html] guide. == Download and installation The latest version of Active Support can be installed with RubyGems: $ gem install activesupport Source code can be downloaded as part of the Rails project on GitHub: * https://github.com/rails/rails/tree/main/activesupport == License Active Support is released under the MIT license: * https://opensource.org/licenses/MIT == Support API documentation is at: * https://api.rubyonrails.org Bug reports for the Ruby on Rails project can be filed here: * https://github.com/rails/rails/issues Feature requests should be discussed on the rails-core mailing list here: * https://discuss.rubyonrails.org/c/rubyonrails-core