Skip to content

Instantly share code, notes, and snippets.

@tduehr
Created January 11, 2019 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tduehr/ca21fb7c9ec401b4c0bd59d981b6fed9 to your computer and use it in GitHub Desktop.
Save tduehr/ca21fb7c9ec401b4c0bd59d981b6fed9 to your computer and use it in GitHub Desktop.
benchmark-ips PoC
require File.expand_path(File.join(File.dirname(__FILE__), "ips_helper"))
require 'benchmark/ips'
require 'ffi'
class TestPtr < FFI::AutoPointer
def self.release(ptr)
LibC.free(ptr);
end
end
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :malloc, [ :long ], TestPtr, :ignore_error => true
attach_function :malloc2, :malloc, [ :long ], :pointer, :ignore_error => true
attach_function :free, [ :pointer ], :void, :ignore_error => true
def self.finalizer(ptr)
proc { LibC.free(ptr) }
end
end
report = []
report << benchmark("malloc", :hold => true) do |x|
x.report("AutoPointer.new #{RUBY_ENGINE}#{RUBY_VERSION} with") {
LibC.malloc(4+4)
}
x.report("AutoPointer.new #{RUBY_ENGINE}#{RUBY_VERSION} without") {
LibC.malloc(4)
}
end
report << benchmark("finalizer", :hold => true) do |x|
x.report("ObjectSpace finalizer #{RUBY_ENGINE}#{RUBY_VERSION} with") {
ptr = LibC.malloc2(4+4)
ptr2 = FFI::Pointer.new(ptr)
ObjectSpace.define_finalizer(ptr2, LibC.finalizer(ptr))
}
x.report("ObjectSpace finalizer #{RUBY_ENGINE}#{RUBY_VERSION} without") {
ptr = LibC.malloc2(4)
ptr2 = FFI::Pointer.new(ptr)
ObjectSpace.define_finalizer(ptr2, LibC.finalizer(ptr))
}
end
report.each{|x| x.run_comparison} if report.first.data.size > 1
def benchmark(name, hold: false, quiet: true)
Benchmark.ips(quiet: quiet) do |x|
x.config(:stats => :bootstrap, :confidence => 95)
yield x
x.hold! "#{name}#{RUBY_ENGINE}#{RUBY_VERSION}" if hold
end
end
# Deprecation warnings removed.
narwhal:ffi eroot$ rake ips:all
install -c build/x86_64-darwin17/ffi_c/2.6.0/ffi_c.bundle lib/ffi_c.bundle
cp build/x86_64-darwin17/ffi_c/2.6.0/ffi_c.bundle build/x86_64-darwin17/stage/lib/ffi_c.bundle
/Users/eroot/.rvm/rubies/ruby-2.6.0/bin/ruby -Ilib -Ibuild ips/ips_autoptr.rb
Pausing here -- run Ruby again to measure the next benchmark...
Pausing here -- run Ruby again to measure the next benchmark...
/Users/eroot/.rvm/rubies/ruby-2.6.0/bin/ruby -Ilib -Ibuild ips/ips_helper.rb
narwhal:ffi eroot$ rake ips:all
install -c build/x86_64-darwin17/ffi_c/2.6.0/ffi_c.bundle lib/ffi_c.bundle
cp build/x86_64-darwin17/ffi_c/2.6.0/ffi_c.bundle build/x86_64-darwin17/stage/lib/ffi_c.bundle
/Users/eroot/.rvm/rubies/ruby-2.6.0/bin/ruby -Ilib -Ibuild ips/ips_autoptr.rb
Comparison:
AutoPointer.new ruby2.6.0 without: 442732.6 i/s
AutoPointer.new ruby2.6.0 with: 418828.2 i/s - same-ish: difference falls within error
with 95.0% confidence
Comparison:
ObjectSpace finalizer ruby2.6.0 without: 534030.1 i/s
ObjectSpace finalizer ruby2.6.0 with: 497763.6 i/s - 1.07x (± 0.02) slower
with 95.0% confidence
/Users/eroot/.rvm/rubies/ruby-2.6.0/bin/ruby -Ilib -Ibuild ips/ips_helper.rb
narwhal:ffi eroot$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment