Skip to content

Instantly share code, notes, and snippets.

@benglewis
Created May 3, 2017 22:28
Show Gist options
  • Save benglewis/c5b1b89967db6d6f5f1a93848f052e03 to your computer and use it in GitHub Desktop.
Save benglewis/c5b1b89967db6d6f5f1a93848f052e03 to your computer and use it in GitHub Desktop.
Ruby BSON symbol performance improvements
require 'benchmark/ips'
unless ((defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby') || RUBY_VERSION < "2.1")
require 'memory_profiler'
end
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require 'bson'
puts '----- Benchmarking Symbol performance (start) -----'
if ((defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby') || RUBY_VERSION < "2.1")
puts "We can\'t run memory profiling in JRuby or Ruby < 2.1"
else
old_report = MemoryProfiler.report top: 2 do
100_000.times do
:some_longer_field_name.to_s.to_bson
end
end
puts '--- Old implementation memory use data (start) ---'
old_report.pretty_print
puts '--- Old implementation memory use data (end) ---'
new_report = MemoryProfiler.report top: 2 do
100_000.times do
:some_longer_field_name.to_bson
end
end
puts '--- New implementation memory use data (start) ---'
new_report.pretty_print
puts '--- New implementation memory use data (end) ---'
end
Benchmark.ips do |x|
x.report('Old implementation') { :hello_world.to_s.to_bson }
x.report('New implementation') { :hello_world.to_bson }
x.report('Using a string value instead') { 'hello_world'.to_bson }
x.compare!
end
puts '----- Benchmarking Symbol performance (end) -----'
puts '----- Benchmarking Hash performance when using symbol keys (start) -----'
MY_CONST = 'THING'
sample_hash = {
:my_key => 'something',
:another_key => 'something_else',
:third_key => true,
:fourth_key => MY_CONST
}
sample_hash_2 = {
'my_key' => 'something',
'another_key' => 'something_else',
'third_key' => true,
'fourth_key' => MY_CONST
}
Benchmark.ips do |x|
x.report('New implementation') { sample_hash.to_bson }
x.report('Using a string value instead') { sample_hash_2.to_bson }
x.compare!
end
if ((defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby') || RUBY_VERSION < "2.1")
puts "We can\'t run memory profiling in JRuby or Ruby < 2.1"
else
report = MemoryProfiler.report top: 2 do
100_000.times do
sample_hash.to_bson
end
end
puts '--- New implementation memory consumption (start) ---'
report.pretty_print
puts '--- New implementation memory consumption (end) ---'
end
module BSON::OldSymbol
def to_bson_key(validating_keys = Config.validating_keys?)
to_s.to_bson_key(validating_keys)
end
end
Symbol.prepend BSON::OldSymbol
Benchmark.ips do |x|
x.report('Old implementation') { sample_hash.to_bson }
end
if ((defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby') || RUBY_VERSION < "2.1")
puts "We can\'t run memory profiling in JRuby or Ruby < 2.1"
something_else
report = MemoryProfiler.report top: 2 do
100_000.times do
sample_hash.to_bson
end
end
puts '--- Old implementation memory consumption (start) ---'
report.pretty_print
puts '--- Old implementation memory consumption (end) ---'
end
puts '----- Benchmarking Hash performance when using symbol keys (end) -----'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment