diff --git a/README.md b/README.md index 43b7f97..ea07905 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,13 @@ less time FasterPath took. They are the median of 3 runs of `LONG_RUN=10 rake pb calls of each method, 5 times) on Linux x86_64 with Ruby 2.7.8 and 2.5.7, built with Rust 1.97. Expect a few points of difference from run to run. +`rake pbench` is also the performance regression test: each method has a floor in +`test/pbench/pbench_suite.rb` (about half of the improvement in the table above) and the task +fails when a method measures below it, so a change that makes a method slower shows up in +`rake test`. On Windows, where `join` is currently slower than Ruby's +([#185](https://github.com/danielpclark/faster_path/issues/185)), the floors are reported but not +enforced. + ### Before and after the Rutie 0.10 upgrade Measured the same way, alternating runs of the code before the upgrade (built with Rutie 0.6) diff --git a/lib/faster_path.rb b/lib/faster_path.rb index b50ff00..6e20989 100644 --- a/lib/faster_path.rb +++ b/lib/faster_path.rb @@ -40,7 +40,9 @@ def self.rust_arch_bits # The Ruby architecture bit width: 64bits or 32bits # @return [Integer] def self.ruby_arch_bits - 1.size * 8 + # The width of a pointer; `1.size` is the width of a C `long`, which is + # 4 bytes on 64-bit Windows. + [0].pack('J').bytesize * 8 end # A very fast way to check if a string is blank diff --git a/test/arch_bits_test.rb b/test/arch_bits_test.rb new file mode 100644 index 0000000..25ec8bb --- /dev/null +++ b/test/arch_bits_test.rb @@ -0,0 +1,16 @@ +require "test_helper" + +class ArchBitsTest < Minitest::Test + def test_ruby_arch_bits_is_32_or_64 + assert_includes [32, 64], FasterPath.ruby_arch_bits + end + + def test_rust_arch_bits_is_32_or_64 + assert_includes [32, 64], FasterPath.rust_arch_bits + end + + def test_the_extension_is_built_for_the_running_ruby + # `1.size` used to report 32 for a 64-bit Ruby on Windows, where a C `long` is 4 bytes + assert_equal FasterPath.rust_arch_bits, FasterPath.ruby_arch_bits + end +end diff --git a/test/pbench/pbench.rb b/test/pbench/pbench.rb index bf11cab..e6f4784 100644 --- a/test/pbench/pbench.rb +++ b/test/pbench/pbench.rb @@ -53,20 +53,36 @@ def performance(baseline, new_impl) # the key is the name of the method # value :old will be a proc to execute original method behavior # value :new will be a proc to execute newer method behavior + # value :min (optional) the least improvement, in percent, the method + # must keep over the original; see `regressions` + # + # Returns a hash of the method names to their measured improvement. def run(hsh) io.send :puts, "Pinch-bench (Pbench) by Daniel P. Clark" io.send :puts, "-"*80 io.send :puts, os_lang_specs io.send :puts, "-"*80 io.flush - hsh.each_key do |k| - h = hsh[k] + hsh.each_with_object({}) do |(k, h), results| result = performance(h[:old], h[:new]) + results[k] = result io.send :puts, "Performance change for #{k} is %.1f%%" % result io.flush end end + # The benchmarks whose result fell below their :min floor, as messages. + # + # The floors are regression tests for performance: a method that has been + # changed, or a dependency that has been upgraded, must not lose the + # improvement over Ruby that this library exists for. + def regressions(results, hsh) + hsh.map do |k, h| + next unless h[:min] && results[k] < h[:min] + "%s is %.1f%% faster than Ruby, below its floor of %d%%" % [k, results[k], h[:min]] + end.compact + end + def os_lang_specs os_stats = "#{FasterPath.ruby_arch_bits}-bit #{`ruby -v`.chomp}\n" rust, detail = `rustc -Vv`.split("\n", 2) diff --git a/test/pbench/pbench_suite.rb b/test/pbench/pbench_suite.rb index a2a88e9..2d0c813 100644 --- a/test/pbench/pbench_suite.rb +++ b/test/pbench/pbench_suite.rb @@ -1,6 +1,7 @@ require 'pbench' # PBENCHES[:""] = { +# min: 50, # the least improvement over Ruby, in percent, the method must keep # new: lambda do |x| # x.times do # end @@ -10,6 +11,13 @@ # end # end # } +# +# The :min floors are about half of the improvements documented in the README, +# so that the noise of a shared CI runner doesn't trip them but a method that +# falls back towards Ruby's speed does. Methods that mostly wait on the +# filesystem (children, directory?, entries) only have to stay faster than Ruby. +# The _compat methods are documented as "stable, not performant" and have no +# floor, and neither does the allocate baseline. # SOME DEFAULTS PATHNAME_A = Pathname.new('a').freeze @@ -37,6 +45,7 @@ end } PBENCHES[:"absolute?"] = { + min: 80, new: lambda do |x| x.times do FasterPath.absolute?("/hello") @@ -51,6 +60,7 @@ end, } PBENCHES[:add_trailing_separator] = { + min: 50, new: lambda do |x| x.times do FasterPath.add_trailing_separator('/hello/world') @@ -65,6 +75,7 @@ end } PBENCHES[:basename] = { + min: 15, new: lambda do |x| x.times do FasterPath.basename("/hello/world") @@ -87,6 +98,7 @@ end } PBENCHES[:children] = { + min: 0, new: lambda do |x| (x/5).times do FasterPath.children(".") @@ -111,6 +123,7 @@ end } PBENCHES[:chop_basename] = { + min: 50, new: lambda do |x| x.times do FasterPath.chop_basename "/hello/world.txt" @@ -129,6 +142,7 @@ PATHNAME_CA1 = Pathname.new('/../.././../a').freeze PATHNAME_CA2 = Pathname.new('a/b/../../../../c/../d').freeze PBENCHES[:cleanpath_aggressive] = { + min: 80, new: lambda do |x| x.times do Pathname.new(FasterPath.cleanpath_aggressive '/../.././../a') @@ -143,6 +157,7 @@ end } PBENCHES[:cleanpath_conservative] = { + min: 80, new: lambda do |x| x.times do Pathname.new(FasterPath.cleanpath_conservative '/../.././../a') @@ -157,6 +172,7 @@ end } PBENCHES[:del_trailing_separator] = { + min: 70, new: lambda do |x| x.times do FasterPath.del_trailing_separator('/hello/world') @@ -171,6 +187,7 @@ end } PBENCHES[:"directory?"] = { + min: 0, new: lambda do |x| x.times do FasterPath.directory?("/hello") @@ -185,6 +202,7 @@ end } PBENCHES[:dirname] = { + min: 30, new: lambda do |x| x.times do FasterPath.dirname "/really/long/path/name/which/ruby/doesnt/like/bar.txt" @@ -201,6 +219,7 @@ end } PBENCHES[:entries] = { + min: 0, new: lambda do |x| (x/5).times do FasterPath.entries("./") @@ -229,6 +248,7 @@ end } PBENCHES[:extname] = { + min: 50, new: lambda do |x| x.times do FasterPath.extname('verylongfilename_verylongfilename.rb') @@ -245,6 +265,7 @@ end } PBENCHES[:"has_trailing_separator?"] = { + min: 75, new: lambda do |x| x.times do FasterPath.has_trailing_separator? '////a//aaa/a//a/aaa////' @@ -259,6 +280,7 @@ end } PBENCHES[:join] = { + min: 70, new: lambda do |x| x.times do FasterPath.join('a', 'b') @@ -275,6 +297,7 @@ end } PBENCHES[:plus] = { + min: 80, new: lambda do |x| x.times do FasterPath.plus('a', 'b') @@ -291,6 +314,7 @@ end } PBENCHES[:"relative?"] = { + min: 80, new: lambda do |x| x.times do FasterPath.relative?("/hello") @@ -307,6 +331,7 @@ PATHNAME_AB = Pathname("/a/b") PATHNAME_ABCD = Pathname("/a/b/c/d") PBENCHES[:relative_path_from] = { + min: 80, new: lambda do |x| x.times do FasterPath.relative_path_from "/a/b/c/d", "/a/b" @@ -320,4 +345,19 @@ end end } -Pbench.new(nil).run(PBENCHES) + +# Fails `rake pbench` when a method has lost its improvement over Ruby. +class PbenchRegressionTest < Minitest::Test + WINDOWS = !File::ALT_SEPARATOR.nil? + + def test_methods_keep_their_improvement_over_ruby + pbench = Pbench.new(nil) + regressions = pbench.regressions(pbench.run(PBENCHES), PBENCHES) + message = "Performance regressions:\n " + regressions.join("\n ") + # On Windows `join` is slower than Ruby's own implementation (see + # https://github.com/danielpclark/faster_path/issues/185), so the + # floors are reported there but not enforced until that is fixed. + skip message if WINDOWS && !regressions.empty? + assert_empty regressions, message + end +end