Add Eisel-Lemire algorithm for faster String#to_f #15655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the Eisel-Lemire algorithm for string-to-float conversion, providing significant performance improvements for
String#to_f, especially for numbers with many significant digits.Performance Results
Benchmark: 3,000,000 iterations per category
"1.5","3.14")"9.99","19.95")"5","42")"3.141592653589793")"0.123456789012345")"1e5","2e10")Key Insights
Implementation Details
Algorithm Overview
The implementation adds three optimization levels to
rb_cstr_to_dbl_raise:Ultra-fast path for small integers (
try_small_integer_fast_path)"5","42","-123"(up to 3 digits)Ultra-fast path for simple decimals (
try_simple_decimal_fast_path)"1.5","9.99","199.95"(up to 3+3 digits)Eisel-Lemire algorithm (
rb_eisel_lemire64)strtodfor ambiguous rounding casesTechnical Details
__uint128_twhen available, falls back to portable 64-bit emulationstrtodfor edge cases (hex floats, >19 digits, ambiguous rounding)References
Benchmark Script