ruby icon indicating copy to clipboard operation
ruby copied to clipboard

Optimize `Range#initialize` for `Bignum`, `Float`, `String`, and `Symbol`

Open kyanagi opened this issue 2 years ago • 0 comments

In range_init, a check is performed to determine if beg and end are comparable. This check is skipped when both of them are Fixnum. By treating more classes in this manner, we can speed up Range#new.

benchmark

Iteration per second (i/s)

master PR
Range.new(0, 5) 11.964M 11.976M
- 1.00x
Range.new(0, bignum) 7.451M 11.627M
- 1.56x
Range.new(0, 5.5) 9.176M 11.870M
- 1.29x
Range.new('a', 'z') 6.959M 8.088M
- 1.16x
Range.new(:a, :z) 8.583M 11.816M
- 1.38x
Range.new(0, nil) 11.956M 11.933M
1.00x -
Range.new(date1, date2) 8.296M 8.244M
1.01x -
Range.new(time1, time2) 9.140M 9.008M
1.01x -

kyanagi avatar Oct 08 '23 23:10 kyanagi