Skip to content

Check happy path first when comparing gem version#9417

Merged
Edouard-chin merged 1 commit into
ruby:masterfrom
Shopify:ec-happy-path
Mar 25, 2026
Merged

Check happy path first when comparing gem version#9417
Edouard-chin merged 1 commit into
ruby:masterfrom
Shopify:ec-happy-path

Conversation

@Edouard-chin

@Edouard-chin Edouard-chin commented Mar 21, 2026

Copy link
Copy Markdown
Member

Note

Reviewing with ?w=1 is recommended.

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

Checking Gem::Version can be micro optimized by checking happy path first.

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

During resolution, Gem::Version are compared against each other. Since comparing versions is a very hot path we can micro optimize it to check the happy path first.

The speed gain on the overall resolution isn't significant but the ips gain is quite substantial. The diff chunk is small so I figure it's worth it anyway.

a = Gem::Version.new("5.3.1")
b = Gem::Version.new("5.3.1")

Benchmark.ips do |x|
  x.report("equal regular:") { a <=> c }
  x.report("equal optimized:") { a <=> c }
  x.hold!("equal_temp_results")
  x.compare!(order: :baseline)
end
Warming up --------------------------------------
      equal optimized:     1.268M i/100ms
Calculating -------------------------------------
      equal optimized:     12.738M (± 1.5%) i/s   (78.50 ns/i) -     64.680M in   5.078754s

Comparison: equal regular::  9866605.0 i/s equal optimized:: 12738310.3 i/s - 1.29x  faster 

Make sure the following tasks are checked

cc/ @skipkayhil

- During resolution, Gem::Version are compared against each other.
  Since comparing versions is a very hot path we can micro optimize
  it to check the happy path first.

  The speed gain on the overall resolution isn't significant but the
  ips gain is quite substantial. The diff chunk is small so I figure
  it's worth it anyway.

  ```ruby
  a = Gem::Version.new("5.3.1")
  b = Gem::Version.new("5.3.1")

  Benchmark.ips do |x|
    x.report("equal regular:") { a <=> c }
    x.report("equal optimized:") { a <=> c }
    x.hold!("equal_temp_results")
    x.compare!(order: :baseline)
  end
  ```

  ```
  Warming up --------------------------------------
      equal optimized:     1.268M i/100ms
  Calculating -------------------------------------
      equal optimized:     12.738M (± 1.5%) i/s   (78.50 ns/i) -     64.680M in   5.078754s

  Comparison:
        equal regular::  9866605.0 i/s
      equal optimized:: 12738310.3 i/s - 1.29x  faster
  ```

ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [arm64-darwin25]
@Edouard-chin Edouard-chin merged commit edf0012 into ruby:master Mar 25, 2026
92 checks passed
@Edouard-chin Edouard-chin deleted the ec-happy-path branch March 25, 2026 18:53
hsbt pushed a commit that referenced this pull request Apr 7, 2026
Check happy path first when comparing gem version

(cherry picked from commit edf0012)
hsbt pushed a commit that referenced this pull request Apr 7, 2026
Check happy path first when comparing gem version

(cherry picked from commit edf0012)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant