Commit a9255c0
committed
Replace Rational() method calls with rational literals for performance
Replace Rational(1, 2) with 0.5r and Rational(0) with 0r across
core.rb, datetime.rb, and strftime.rb. Rational literals are resolved
at compile time, avoiding method dispatch overhead on every call.
Note: Rational(4800001, 2) in amjd is intentionally kept as-is because
4800001/2r is parsed as Integer#/(Rational) division at runtime, which
is 2x slower than the direct Rational() constructor.
Benchmark (Ruby 4.0.1 +YJIT, benchmark-ips):
Date.new!:
Before (Rational(1,2)): 2.812M i/s (355.58 ns/i)
After (0.5r): 3.596M i/s (278.11 ns/i)
Improvement: +27.9%
Date#day_fraction:
Before (Rational(0)): 11.230M i/s (89.05 ns/i)
After (0r): 29.947M i/s (33.39 ns/i)
Improvement: +166.7%
Micro-benchmark (literal vs method call):
0r vs Rational(0): 34.5M vs 11.3M i/s (+3.05x)
0.5r vs Rational(1,2): 34.2M vs 8.8M i/s (+3.90x)1 parent 7e80db9 commit a9255c0
3 files changed
+10
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
328 | 328 | | |
329 | 329 | | |
330 | 330 | | |
331 | | - | |
| 331 | + | |
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
| |||
875 | 875 | | |
876 | 876 | | |
877 | 877 | | |
878 | | - | |
| 878 | + | |
879 | 879 | | |
880 | 880 | | |
881 | 881 | | |
| |||
1585 | 1585 | | |
1586 | 1586 | | |
1587 | 1587 | | |
1588 | | - | |
| 1588 | + | |
1589 | 1589 | | |
1590 | 1590 | | |
1591 | 1591 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
| 143 | + | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| |||
440 | 440 | | |
441 | 441 | | |
442 | 442 | | |
443 | | - | |
| 443 | + | |
444 | 444 | | |
445 | 445 | | |
446 | 446 | | |
447 | 447 | | |
448 | | - | |
| 448 | + | |
449 | 449 | | |
450 | 450 | | |
451 | 451 | | |
| |||
857 | 857 | | |
858 | 858 | | |
859 | 859 | | |
860 | | - | |
| 860 | + | |
861 | 861 | | |
862 | 862 | | |
863 | 863 | | |
| |||
880 | 880 | | |
881 | 881 | | |
882 | 882 | | |
883 | | - | |
| 883 | + | |
884 | 884 | | |
885 | 885 | | |
886 | 886 | | |
| |||
932 | 932 | | |
933 | 933 | | |
934 | 934 | | |
935 | | - | |
| 935 | + | |
936 | 936 | | |
937 | 937 | | |
938 | 938 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
523 | 523 | | |
524 | 524 | | |
525 | 525 | | |
526 | | - | |
| 526 | + | |
527 | 527 | | |
528 | 528 | | |
529 | 529 | | |
| |||
0 commit comments