Repository navigation
Introduce a fast path for comparing Gem::Version #9414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -222,6 +222,7 @@ def initialize(version) | |||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
| @version = -@version | ||||||||||||||||||||||||||||||||||||||||||||||
| @segments = nil | ||||||||||||||||||||||||||||||||||||||||||||||
| @sort_key = compute_sort_key | ||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ## | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -350,6 +351,12 @@ def <=>(other) | |||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return unless Gem::Version === other | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # Fast path for comparison when available. | ||||||||||||||||||||||||||||||||||||||||||||||
| if @sort_key && other.sort_key | ||||||||||||||||||||||||||||||||||||||||||||||
| return @sort_key <=> other.sort_key | ||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return 0 if @version == other.version || canonical_segments == other.canonical_segments | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| lhsegments = canonical_segments | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -415,6 +422,21 @@ def freeze | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| protected | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| attr_reader :sort_key # :nodoc: | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def compute_sort_key | ||||||||||||||||||||||||||||||||||||||||||||||
| segments = canonical_segments | ||||||||||||||||||||||||||||||||||||||||||||||
| return if segments.size > 4 || prerelease? || segments.any? {|segment| segment > 65_000 } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| base = 1_000_000_000_000 | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| segments.sum do |segment| | ||||||||||||||||||||||||||||||||||||||||||||||
| result = segment * base | ||||||||||||||||||||||||||||||||||||||||||||||
| base /= 10_000 | ||||||||||||||||||||||||||||||||||||||||||||||
| result | ||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+428
to
+437
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I'd much rather prefer this as:
Suggested change
since I find it to be more readable, we only iterate over the segment list once, and the multiplier bounds check is handled in the same place where the multiplication happens.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if your suggestion works. |
||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def _segments | ||||||||||||||||||||||||||||||||||||||||||||||
| # segments is lazy so it can pick up version values that come from | ||||||||||||||||||||||||||||||||||||||||||||||
| # old marshaled versions, which don't go through marshal_load. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not be the same number (or larger) as used above in
segment > 65_000There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I orginally went with
65_000(by looking at our own Gemfile.lock) as I wanted to exclude computing a sort key for gems that get released with a timestamp version such as20180730171000).We could compute a sort key for those type of gems though. However I need to dig, because I tried now and it seems that the resolver is taking forever to solve the versions (been waiting 3 minutes on our monolith and it still hasn't finished resolving). Let me get back to you with an explanation, as I don't know the reason yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, some private gems changed their version scheme, it used to be
0.0.1.<timestamp>(Where timestamp is 14 digits) , but changed to follow semver later on.So we end up with a older version that has a larger computed sort key integer.