Skip to content

Commit 8cc85dc

Browse files
skipkayhilhsbt
authored andcommitted
[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. ruby/rubygems@6a7c411ba7
1 parent af76b7f commit 8cc85dc

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

lib/bundler/resolver/candidate.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,38 @@ def segments
4848
@version.segments
4949
end
5050

51-
def sort_obj
52-
[@version, @priority]
53-
end
54-
5551
def <=>(other)
5652
return unless other.is_a?(self.class)
5753

58-
sort_obj <=> other.sort_obj
54+
version_comparison = version <=> other.version
55+
return version_comparison unless version_comparison.zero?
56+
57+
priority <=> other.priority
5958
end
6059

6160
def ==(other)
6261
return unless other.is_a?(self.class)
6362

64-
sort_obj == other.sort_obj
63+
version == other.version && priority == other.priority
6564
end
6665

6766
def eql?(other)
6867
return unless other.is_a?(self.class)
6968

70-
sort_obj.eql?(other.sort_obj)
69+
version.eql?(other.version) && priority.eql?(other.priority)
7170
end
7271

7372
def hash
74-
sort_obj.hash
73+
[@version, @priority].hash
7574
end
7675

7776
def to_s
7877
@version.to_s
7978
end
79+
80+
protected
81+
82+
attr_reader :priority
8083
end
8184
end
8285
end

0 commit comments

Comments
 (0)