public
Created — forked from tmm1/bench.rb

performance improvements for Bundler and Gem::Version#<=>

  • Download Gist
bench.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
require 'rubygems'
 
# diff --git a/lib/rubygems/version.rb b/lib/rubygems/version.rb
# index 50d8204..4962ce1 100644
# --- a/lib/rubygems/version.rb
# +++ b/lib/rubygems/version.rb
# @@ -295,12 +295,16 @@ class Gem::Version
# rhsize = other.segments.size
# limit = (lhsize > rhsize ? lhsize : rhsize) - 1
#
# + lhsegments = segments
# + rhsegments = other.segments
# +
# 0.upto(limit) do |i|
# - lhs, rhs = segments[i] || 0, other.segments[i] || 0
# + lhs, rhs = lhsegments[i] || 0, rhsegments[i] || 0
#
# + next if lhs == rhs
# return -1 if String === lhs && Numeric === rhs
# return 1 if Numeric === lhs && String === rhs
# - return lhs <=> rhs if lhs != rhs
# + return lhs <=> rhs
# end
#
# return 0
class Gem::FastVersion < Gem::Version
def <=> other
return 1 unless other # HACK: comparable with nil? why?
return nil unless self.class === other
 
lhsize = segments.size
rhsize = other.segments.size
limit = (lhsize > rhsize ? lhsize : rhsize) - 1
 
lhsegments = segments
rhsegments = other.segments
 
0.upto(limit) do |i|
lhs, rhs = lhsegments[i] || 0, rhsegments[i] || 0
 
next if lhs == rhs
return -1 if String === lhs && Numeric === rhs
return 1 if Numeric === lhs && String === rhs
return lhs <=> rhs
end
 
return 0
end
end
 
class Gem::FasterVersion < Gem::FastVersion
def <=> other
return 0 if version == other.version
super
end
end
 
require 'version_sorter' # http://github.com/defunkt/version_sorter
class Gem::CExtVersion < Gem::Version
def <=> other
return 1 unless other # HACK: comparable with nil? why?
return nil unless self.class === other
return 0 if version == other.version
 
# XXX: this does not follow rubygems' prelease versioning rules
VersionSorter.sort([version, other.version]).first == version ?
-1 :
1
end
end
 
require 'benchmark'
Benchmark.bmbm do |b|
[ Gem::Version, Gem::FastVersion, Gem::FasterVersion, Gem::CExtVersion ].each do |klass|
b.report klass.to_s do
a = klass.new('3.0.0.0.0')
b = klass.new('3.0.0.0.0')
c = klass.new('3.0.0.0.3')
 
250_000.times do
a <=> b
b <=> c
end
end
end
end
 
__END__
 
 
Rehearsal ------------------------------------------------------
Gem::Version 10.630000 0.020000 10.650000 ( 10.731254)
Gem::FastVersion 7.270000 0.010000 7.280000 ( 7.359935)
Gem::FasterVersion 4.370000 0.010000 4.380000 ( 4.408528)
Gem::CExtVersion 3.520000 0.010000 3.530000 ( 3.540201)
-------------------------------------------- total: 25.840000sec
 
user system total real
Gem::Version 11.430000 0.020000 11.450000 ( 11.513403)
Gem::FastVersion 7.230000 0.010000 7.240000 ( 7.313092)
Gem::FasterVersion 4.450000 0.010000 4.460000 ( 4.478300)
Gem::CExtVersion 3.500000 0.010000 3.510000 ( 3.539400)
bundler_rubygems_profile.png

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.