Created
December 7, 2018 02:55
-
-
Save azimux/be20b8bcc3c4b880c31d2df5e602e9b7 to your computer and use it in GitHub Desktop.
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
require "active_support/hash_with_indifferent_access" | |
require "active_support/core_ext/object/deep_dup" | |
require "active_support/version" | |
require "securerandom" | |
require "benchmark" | |
class BigHwiaPerfBench | |
def run! | |
make_sure_on_active_support_5! | |
apply_patch if apply_patch? | |
do_bench | |
end | |
def do_bench | |
5.times do | |
big_hwia = build_big_hwia | |
GC.disable | |
puts Benchmark.measure { big_hwia.deep_dup } | |
GC.enable | |
end | |
end | |
def make_sure_on_active_support_5! | |
raise "not on 5" unless ActiveSupport::VERSION::MAJOR == 5 | |
end | |
def apply_patch? | |
ENV["APPLY_PATCH"] == "true" | |
end | |
def apply_patch | |
HashWithIndifferentAccess.class_eval do | |
def initialize(constructor = {}) | |
if constructor.respond_to?(:to_hash) | |
super() | |
update(constructor) | |
hash = constructor.is_a?(Hash) ? constructor : constructor.to_hash | |
self.default = hash.default if hash.default | |
self.default_proc = hash.default_proc if hash.default_proc | |
else | |
super(constructor) | |
end | |
end | |
end | |
end | |
def build_big_hwia | |
outer_hash = HashWithIndifferentAccess.new | |
100.times do | |
inner_hash = outer_hash[SecureRandom.hex] = HashWithIndifferentAccess.new | |
100.times do | |
inner_inner_hash = inner_hash[SecureRandom.hex] = HashWithIndifferentAccess.new | |
100.times do | |
inner_inner_hash[SecureRandom.hex] = SecureRandom.hex | |
end | |
end | |
end | |
outer_hash | |
end | |
end | |
BigHwiaPerfBench.new.run! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment