From 881a504789fe1d099da349514ccf65bb322daac6 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 18 Nov 2026 16:25:35 -0500 Subject: [PATCH 1/2] short_numbers: reject inputs that cannot start a number at once With short_numbers, an input that failed the short parser fell back to the usual parser. Words such as "auto" or "undefined" thus paid for both, which made them about six times slower than without the hint. In a JavaScript engine, most strings converted to numbers can be such words (see #435). We now test the first character before anything else: unless it is a digit, a sign or the decimal point, the input is rejected as the usual parser would reject it (leading spaces, infinity and NaN are still left to the usual parser). --- include/fast_float/float_common.h | 3 ++- include/fast_float/parse_number.h | 24 +++++++++++++++++++++++- tests/performance_hints_test.cpp | 14 ++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 4753de89..bc6ccc26 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -99,7 +99,8 @@ enum class performance_hints : uint64_t { // parsed with a few bitwise operations, without branches that depend on // their shape. Do not use it when the numbers have predictable shapes (e.g., // mostly 0, 1.0 and 0.75 in a regular pattern): the usual parser is then - // faster. Other inputs are parsed as usual, only a bit more slowly. The + // faster. Inputs that cannot start a number (e.g., "auto") are rejected at + // once; other inputs are parsed as usual, only a bit more slowly. The // results do not change. Only for char inputs and the float and double // types; the JSON and JavaScript formats ignore it. short_numbers = 1 << 2, diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 17febfae..fd84a704 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -388,7 +388,29 @@ from_chars_short(UC const *first, UC const *last, T &value, chars_format fmt, // JSON and JavaScript have stricter rules (e.g., no "01" or "5."). if (!uint64_t(fmt & chars_format::fixed) || uint64_t(fmt & (basic_json_fmt | basic_javascript_fmt)) || - (last - first) > 8 || first == last) { + first == last) { + return false; + } + // A number starts with a digit, a sign or the decimal point. Other inputs, + // such as "auto", are rejected here as the usual parser would reject them: + // falling back would cost them a full parse, and such inputs can be common + // (e.g., in a JavaScript engine, most strings converted to numbers are not + // numbers). We test before the length so that long words (e.g., "undefined") + // take the same branch. + UC const c = *first; + if (!is_integer(c) && c != UC('-') && c != decimal_point && + !(uint64_t(fmt & chars_format::allow_leading_plus) && c == UC('+'))) { + // Leading spaces, infinity and NaN are left to the usual parser. + bool const maybe_infnan = !uint64_t(fmt & chars_format::no_infnan) && + ((c | 0x20) == 'i' || (c | 0x20) == 'n'); + if (uint64_t(fmt & chars_format::skip_white_space) || maybe_infnan) { + return false; + } + answer.ec = std::errc::invalid_argument; + answer.ptr = first; + return true; + } + if ((last - first) > 8) { return false; } uint64_t mantissa; diff --git a/tests/performance_hints_test.cpp b/tests/performance_hints_test.cpp index 1800281d..1b40314d 100644 --- a/tests/performance_hints_test.cpp +++ b/tests/performance_hints_test.cpp @@ -30,6 +30,12 @@ chars_format const formats[] = { chars_format::general, chars_format::general | chars_format::allow_leading_plus, chars_format::general | chars_format::skip_white_space, + chars_format::general | chars_format::no_infnan, + chars_format::general | chars_format::no_infnan | + chars_format::allow_leading_plus, + chars_format::general | chars_format::no_infnan | + chars_format::skip_white_space, + chars_format::fixed | chars_format::no_infnan, chars_format::scientific, chars_format::fixed, chars_format::json, @@ -313,14 +319,18 @@ int main() { // around 8 characters "12345678", "123456789", "-1234567", "-12345678", "+1234567", "1234.567", "1234.5678", "-123.456", ".1234567", "1234567.", "0000000.", "-0000000", - "99999999", "9.999999", "1.2.3", "12..3", "1,234", "12 34"}; + "99999999", "9.999999", "1.2.3", "12..3", "1,234", "12 34", + // not numbers + "auto", "none", "px", "x", "a1", "e5", "E5", "true", "undefined", + "inherit", "Infinity", "infinity", "INF", "inf", "NaN", "nan", "nan(1)", + "i", "n", "Nope", "infx", " 1", "\t1", "_1", "(1)", "#1"}; for (std::string const &s : cases) { if (!check_all(s)) { return EXIT_FAILURE; } } // Short random strings over an alphabet rich in signs, digits and dots. - char const alphabet[] = "0123456789000.-+e ,xn"; + char const alphabet[] = "0123456789000.-+e ,xniaf"; std::mt19937_64 gen(1234); for (size_t i = 0; i < 100000; i++) { size_t const length = 1 + size_t(gen() % 10); From abc71cbac5175895eb763ec1f32960ad1c5d45d6 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 18 Nov 2026 16:36:56 -0500 Subject: [PATCH 2/2] short_numbers: do not take the short path when the decimal point is a digit The short parser cannot tell such a decimal point from the digit itself: with '0' as decimal point, "30" gave 3. V8 passes '0' so that parseInt stops after the integer part. The test now also uses '0' and '5' as decimal points. --- include/fast_float/parse_number.h | 6 ++++-- tests/performance_hints_test.cpp | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index fd84a704..acb099bf 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -385,10 +385,12 @@ from_chars_short(UC const *first, UC const *last, T &value, chars_format fmt, return false; } fmt = adjust_for_feature_macros(fmt); - // JSON and JavaScript have stricter rules (e.g., no "01" or "5."). + // JSON and JavaScript have stricter rules (e.g., no "01" or "5."). The + // short parser cannot tell a decimal point that is a digit (e.g., V8 passes + // '0' so that parseInt stops after the integer part) from the digit itself. if (!uint64_t(fmt & chars_format::fixed) || uint64_t(fmt & (basic_json_fmt | basic_javascript_fmt)) || - first == last) { + is_integer(decimal_point) || first == last) { return false; } // A number starts with a digit, a sign or the decimal point. Other inputs, diff --git a/tests/performance_hints_test.cpp b/tests/performance_hints_test.cpp index 1b40314d..d127f198 100644 --- a/tests/performance_hints_test.cpp +++ b/tests/performance_hints_test.cpp @@ -79,12 +79,19 @@ bool same_result(std::basic_string const &s, chars_format fmt) { if (!same(s, r1, expected, r2, actual, "from_chars", fmt)) { return false; } - fast_float::parse_options_t const options(fmt, UC(',')); - expected = T(-12345); - actual = T(-12345); - r1 = fast_float::from_chars_advanced(first, last, expected, options); - r2 = fast_float::from_chars_advanced(first, last, actual, options); - return same(s, r1, expected, r2, actual, "from_chars_advanced", fmt); + // A comma as decimal point, and a digit: V8 passes '0' so that parseInt + // stops after the integer part. + for (UC const decimal_point : {UC(','), UC('0'), UC('5')}) { + fast_float::parse_options_t const options(fmt, decimal_point); + expected = T(-12345); + actual = T(-12345); + r1 = fast_float::from_chars_advanced(first, last, expected, options); + r2 = fast_float::from_chars_advanced(first, last, actual, options); + if (!same(s, r1, expected, r2, actual, "from_chars_advanced", fmt)) { + return false; + } + } + return true; } // The parser's own result also tells which error it found, and where.