forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
thread_mattr_accessor
default
option behavior
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 rails#43312. Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
- Loading branch information
1 parent
0fbac7f
commit 25faa54
Showing
3 changed files
with
96 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters