summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHartley McGuire <[email protected]>2025-03-11 17:46:35 -0400
committerHiroshi SHIBATA <[email protected]>2025-03-13 10:24:21 +0900
commit8cc85dc00feef7fc7846ad15df2778f58716c169 (patch)
tree05463a0e38ba43011b78a32965bb2a3e118f3091
parentaf76b7f4d9f791b8d20f567e0fa8a45488fa00b8 (diff)
[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
-rw-r--r--lib/bundler/resolver/candidate.rb19
1 files 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