Commit 3e172d6
committed
Check happy path first when comparing gem version:
- 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]1 parent 30e2aa9 commit 3e172d6
1 file changed
+41
-43
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
345 | 345 | | |
346 | 346 | | |
347 | 347 | | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
361 | 353 | | |
362 | | - | |
363 | | - | |
| 354 | + | |
364 | 355 | | |
365 | | - | |
366 | | - | |
367 | | - | |
| 356 | + | |
| 357 | + | |
368 | 358 | | |
369 | | - | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
370 | 362 | | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
| 363 | + | |
375 | 364 | | |
376 | | - | |
377 | | - | |
378 | | - | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
379 | 369 | | |
380 | | - | |
381 | | - | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
382 | 373 | | |
383 | | - | |
| 374 | + | |
| 375 | + | |
384 | 376 | | |
385 | | - | |
386 | | - | |
| 377 | + | |
387 | 378 | | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
398 | 393 | | |
399 | | - | |
400 | 394 | | |
401 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
402 | 400 | | |
403 | 401 | | |
404 | 402 | | |
| |||
0 commit comments