Skip to content

Instantly share code, notes, and snippets.

@panthomakos
Created March 12, 2012 06:16
Show Gist options
  • Save panthomakos/2020228 to your computer and use it in GitHub Desktop.
Save panthomakos/2020228 to your computer and use it in GitHub Desktop.
Rails ActiveSupport::Autoload Benchmark
# 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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment