public
Created

Rails ActiveSupport::Autoload Benchmark

  • Download Gist
benchmark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# Rails 3.2 @ d5d241cb2c696f13e2c16efca0d24565a6e1c0a5
 
require 'active_support/dependencies/autoload'
require 'benchmark'
 
GC.disable
 
FILES = (1..1_000_000).map do |file|
["Base#{file}".to_sym, "base/#{file}"]
end
 
def count_objects
start = ObjectSpace.count_objects[:TOTAL]
yield
finish = ObjectSpace.count_objects[:TOTAL]
puts "Object Count: #{finish-start}"
end
 
$add_autoloads = lambda do
class Support
FILES.each do |file|
autoload(*file)
end
end
end
 
def super_benchmark(benchmark, name)
count_objects do
benchmark.report(name) do
$add_autoloads.call
end
end
end
 
Benchmark.bm do |x|
# Ruby w/o ActiveSupport::Autoload benchmark.
class Support; end
super_benchmark(x, 'ruby')
 
# Original ActiveSupport::Autoload benchmark.
Object.send(:remove_const, :Support)
class Support; end
Support.extend(ActiveSupport::Autoload)
super_benchmark(x, 'orig')
 
# Modified ActiveSupport::Autoload benchmark.
module ActiveSupport
module Autoload
def autoload(const_name, path = @@at_path)
unless path
full = [name, @@under_path, const_name.to_s, path].compact.join("::")
path = Inflector.underscore(full)
end
 
if @@eager_autoload
@@autoloads[const_name] = path
end
 
super const_name, path
end
end
end
 
Object.send(:remove_const, :Support)
class Support; end
Support.extend(ActiveSupport::Autoload)
 
super_benchmark(x, 'modi')
end
results.txt
1 2 3 4 5 6 7
user system total real
ruby 1.620000 0.170000 1.790000 ( 1.802548)
Object Count: 3000184
orig 6.350000 0.500000 6.850000 ( 7.040592)
Object Count: 9000139
modi 2.660000 0.180000 2.840000 ( 3.014158)
Object Count: 3000184

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.