

Optimize CurrentAttributes method generation by casperisfine · Pull Request #435...
source link: https://github.com/rails/rails/pull/43584
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The bulk of the optimization is to generate code rather than use define_method
with a closure.
Warming up --------------------------------------
original 207.468k i/100ms
code-generator 340.849k i/100ms
Calculating -------------------------------------
original 2.127M (± 1.1%) i/s - 10.788M in 5.073860s
code-generator 3.426M (± 0.9%) i/s - 17.383M in 5.073965s
Comparison:
code-generator: 3426241.0 i/s
original: 2126539.2 i/s - 1.61x (± 0.00) slower
require 'benchmark/ips' require 'active_support/all' class Original < ActiveSupport::CurrentAttributes attribute :foo end class CodeGen < ActiveSupport::CurrentAttributes class << self def attribute(*names) ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner| names.each do |name| owner.define_cached_method(name, namespace: :current_attributes) do |batch| batch << "def #{name}" << "attributes[:#{name}]" << "end" end owner.define_cached_method("#{name}=", namespace: :current_attributes) do |batch| batch << "def #{name}=(value)" << "attributes[:#{name}] = value" << "end" end end end ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner| names.each do |name| owner.define_cached_method(name, namespace: :current_attributes_delegation) do |batch| batch << "def #{name}" << "instance.#{name}" << "end" end owner.define_cached_method("#{name}=", namespace: :current_attributes_delegation) do |batch| batch << "def #{name}=(value)" << "instance.#{name} = value" << "end" end end end end end attribute :foo end Benchmark.ips do |x| x.report('original') { Original.foo } x.report('code-generator') { CodeGen.foo } x.compare! end
Recommend
-
4
Copy link Contributor casperisfine c...
-
8
Copy link Contributor casperisfine c...
-
8
Context By experimenting with #42085, I was able to better profile our application, which made me realize that define_attribute_methods was responsible f...
-
9
Copy link Contributor casperisfine c...
-
13
Copy link Contributor casperisfine c...
-
8
Copy link Contributor casperisfine commented
-
5
Copy link Contributor casperisfine
-
6
Copy link Contributor casperisfine
-
8
Collaborator rustbot commented
-
10
Friday, January 20, 2023 Active Record regroup, CurrentAttributes name restrictions and more! Posted by gregmolnar Hola, this is
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK