Skip to content

Better algorithm for sorting gem version#9421

Open
Edouard-chin wants to merge 1 commit intoruby:masterfrom
Shopify:ec-version-optimized
Open

Better algorithm for sorting gem version#9421
Edouard-chin wants to merge 1 commit intoruby:masterfrom
Shopify:ec-version-optimized

Conversation

@Edouard-chin
Copy link
Collaborator

What was the end-user or developer problem that led to this PR?

In #9414 I introduced a fast path to compare gem version. The implementation has a couple of caveats such as not computing a sort key for gems having more than 4 segments (major, minor, patch, security), as well as skipping gems that uses a "timestamped" version (such as 0.0.1.20223008103022).

The implementation also had to iterate over the segments couple of times. There were some concerns from some users that some gems such as sorbet (currently at version 0.6.13055) would at some point reach the threshold where we don't compute a sort key (any segment > 65_000).

This change was tested on every version returned by Rubygems.org compact index API:

Method Valid Invalid Improvement
compute_sort_key 1,624,734 6,613 baseline
mixedradix 1,625,985 5,362 -1,115 (17.2%)

What is your fix for the problem, implemented in this PR?

We can instead use a Mixed-radix solution which is more elegant and solve some of those issues (we still don't compute a sort key for timestamped version gems as the integer value may be larger than what we compute for a gem that has a higher version, such as 0.1.0).

I re-ran the benchmark from #9414, and the results are similar.

Make sure the following tasks are checked

cc/ @paracycle

- In ruby#9414 I introduced a fast path to compare gem version. The implementation has a couple of caveats such as not computing a sort key for gems having more than 4 segments (major, minor, patch, security), as well as skipping gems that uses a "timestamped" version (such as 0.0.1.20223008103022). The implementation also had to iterate over the segments couple of times. There were some concerns from some users that some gems such as sorbet (currentlt at version 0.6.13055) would at somepoint reach the thresold where we don't compute a sort key (any segment > 65_000). We can instead use a Mixed-radix solution which is more elegant and solve some of those issues (we still don't compute a sort key for timestamped version gems as the integer value may be larger than what we compute for a gem that has a higher version, such as 0.1.0). Co-authored-by: Ufuk Kayserilioglu <ufuk@paralaus.com>
@Edouard-chin Edouard-chin changed the title Better algorithm for sorting gem version: Better algorithm for sorting gem version Mar 24, 2026
Copy link
Contributor

@paracycle paracycle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants