From 8cc85dc00feef7fc7846ad15df2778f58716c169 Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Tue, 11 Mar 2025 17:46:35 -0400 Subject: [PATCH] [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 --- lib/bundler/resolver/candidate.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/bundler/resolver/candidate.rb b/lib/bundler/resolver/candidate.rb index 30fd6fe2fd..ad280fe82d 100644 --- a/lib/bundler/resolver/candidate.rb +++ b/lib/bundler/resolver/candidate.rb @@ -48,35 +48,38 @@ module Bundler @version.segments end - def sort_obj - [@version, @priority] - end - def <=>(other) 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 def ==(other) return unless other.is_a?(self.class) - sort_obj == other.sort_obj + version == other.version && priority == other.priority end def eql?(other) return unless other.is_a?(self.class) - sort_obj.eql?(other.sort_obj) + version.eql?(other.version) && priority.eql?(other.priority) end def hash - sort_obj.hash + [@version, @priority].hash end def to_s @version.to_s end + + protected + + attr_reader :priority end end end