[rubygems/rubygems] Remove array allocation from Candidate#<=>

In a large application I profiled allocations while running `bundle
update` and found that this method was ~60% of allocations while
resolving (and Candidate#<=> is almost half of the total runtime).

This commit removes the array allocation in Candidate#<=> (and similar
methods since the implementations are so simple). The array is always
the same two elements so they can just be compared directly.

https://github.com/rubygems/rubygems/commit/6a7c411ba7
This commit is contained in:
Hartley McGuire 2025-03-11 17:46:35 -04:00 committed by Hiroshi SHIBATA
parent af76b7f4d9
commit 8cc85dc00f

View File

@ -48,35 +48,38 @@ module Bundler
@version.segments @version.segments
end end
def sort_obj
[@version, @priority]
end
def <=>(other) def <=>(other)
return unless other.is_a?(self.class) return unless other.is_a?(self.class)
sort_obj <=> other.sort_obj version_comparison = version <=> other.version
return version_comparison unless version_comparison.zero?
priority <=> other.priority
end end
def ==(other) def ==(other)
return unless other.is_a?(self.class) return unless other.is_a?(self.class)
sort_obj == other.sort_obj version == other.version && priority == other.priority
end end
def eql?(other) def eql?(other)
return unless other.is_a?(self.class) return unless other.is_a?(self.class)
sort_obj.eql?(other.sort_obj) version.eql?(other.version) && priority.eql?(other.priority)
end end
def hash def hash
sort_obj.hash [@version, @priority].hash
end end
def to_s def to_s
@version.to_s @version.to_s
end end
protected
attr_reader :priority
end end
end end
end end