Skip to content

Instantly share code, notes, and snippets.

@azimux azimux/hwia_perf.rb
Created Dec 7, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.