Skip to content

Instantly share code, notes, and snippets.

@casperisfine
Created April 11, 2021 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casperisfine/bc27e0f2395707baa4b504948690fc73 to your computer and use it in GitHub Desktop.
Save casperisfine/bc27e0f2395707baa4b504948690fc73 to your computer and use it in GitHub Desktop.
require 'active_support/all'
require 'benchmark/ips'
class Module
def fake_cattr_accessor(sym)
private_getter = :"_#{sym}"
private_setter = :"_#{sym}="
singleton_class.attr_accessor(private_getter)
singleton_class.send(:private, private_getter, private_setter)
owner = self
singleton_class.define_method(sym) { owner.send(private_getter) }
singleton_class.define_method(:"#{sym}=") { |a| owner.send(private_setter, a) }
end
end
class Foo
cattr_accessor :cattr
fake_cattr_accessor :fake
end
Foo.cattr = Foo.fake = 42
class Bar < Foo
end
raise "Implementation error" unless Bar.cattr == Bar.fake
5.times do
puts "== #{Foo.ancestors.size} ancestors =="
Benchmark.ips do |x|
x.report('cattr') { Bar.cattr }
x.report('fake') { Bar.fake }
x.compare!
end
5.times { Foo.include(Module.new) }
end
== 7 ancestors ==
Warming up --------------------------------------
cattr 1.110M i/100ms
fake 795.780k i/100ms
Calculating -------------------------------------
cattr 11.123M (± 0.6%) i/s - 56.627M in 5.091436s
fake 7.978M (± 0.7%) i/s - 40.585M in 5.087327s
Comparison:
cattr: 11122521.5 i/s
fake: 7978023.5 i/s - 1.39x (± 0.00) slower
== 12 ancestors ==
Warming up --------------------------------------
cattr 875.986k i/100ms
fake 797.623k i/100ms
Calculating -------------------------------------
cattr 8.768M (± 0.7%) i/s - 44.675M in 5.095467s
fake 7.967M (± 0.8%) i/s - 39.881M in 5.006234s
Comparison:
cattr: 8768066.1 i/s
fake: 7966818.6 i/s - 1.10x (± 0.00) slower
== 17 ancestors ==
Warming up --------------------------------------
cattr 709.909k i/100ms
fake 797.053k i/100ms
Calculating -------------------------------------
cattr 7.146M (± 0.8%) i/s - 36.205M in 5.067162s
fake 7.958M (± 0.8%) i/s - 39.853M in 5.008240s
Comparison:
fake: 7957872.6 i/s
cattr: 7145567.0 i/s - 1.11x (± 0.00) slower
== 22 ancestors ==
Warming up --------------------------------------
cattr 573.736k i/100ms
fake 797.993k i/100ms
Calculating -------------------------------------
cattr 5.731M (± 0.5%) i/s - 28.687M in 5.006109s
fake 7.969M (± 0.6%) i/s - 39.900M in 5.006868s
Comparison:
fake: 7969285.8 i/s
cattr: 5730523.3 i/s - 1.39x (± 0.00) slower
== 27 ancestors ==
Warming up --------------------------------------
cattr 485.756k i/100ms
fake 791.246k i/100ms
Calculating -------------------------------------
cattr 4.849M (± 0.5%) i/s - 24.288M in 5.008705s
fake 7.970M (± 0.5%) i/s - 40.354M in 5.063216s
Comparison:
fake: 7970114.7 i/s
cattr: 4849224.3 i/s - 1.64x (± 0.00) slower
== 7 ancestors ==
Warming up --------------------------------------
cattr 1.039M i/100ms
fake 712.698k i/100ms
Calculating -------------------------------------
cattr 10.364M (± 0.7%) i/s - 51.932M in 5.011092s
fake 7.120M (± 0.8%) i/s - 35.635M in 5.005114s
Comparison:
cattr: 10364004.2 i/s
fake: 7120207.6 i/s - 1.46x (± 0.00) slower
== 12 ancestors ==
Warming up --------------------------------------
cattr 836.979k i/100ms
fake 702.612k i/100ms
Calculating -------------------------------------
cattr 8.362M (± 0.8%) i/s - 41.849M in 5.005256s
fake 7.018M (± 0.7%) i/s - 35.131M in 5.006255s
Comparison:
cattr: 8361520.1 i/s
fake: 7017642.9 i/s - 1.19x (± 0.00) slower
== 17 ancestors ==
Warming up --------------------------------------
cattr 690.479k i/100ms
fake 707.572k i/100ms
Calculating -------------------------------------
cattr 7.041M (± 0.7%) i/s - 35.214M in 5.001448s
fake 7.093M (± 0.8%) i/s - 36.086M in 5.088152s
Comparison:
fake: 7092636.8 i/s
cattr: 7041212.5 i/s - same-ish: difference falls within error
== 22 ancestors ==
Warming up --------------------------------------
cattr 580.160k i/100ms
fake 708.509k i/100ms
Calculating -------------------------------------
cattr 5.778M (± 1.0%) i/s - 29.008M in 5.020674s
fake 7.081M (± 0.7%) i/s - 35.425M in 5.003460s
Comparison:
fake: 7080549.4 i/s
cattr: 5778287.1 i/s - 1.23x (± 0.00) slower
== 27 ancestors ==
Warming up --------------------------------------
cattr 498.150k i/100ms
fake 710.501k i/100ms
Calculating -------------------------------------
cattr 4.970M (± 0.5%) i/s - 24.908M in 5.011529s
fake 7.102M (± 0.8%) i/s - 35.525M in 5.002325s
Comparison:
fake: 7102201.9 i/s
cattr: 4970190.2 i/s - 1.43x (± 0.00) slower
@casperisfine
Copy link
Author

>> require 'active_record'
=> true
>> ActiveRecord::Base.ancestors.size
=> 62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment