From 5411b143ef229d8ca804cb885fcc0c599ae14335 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 4 Mar 2025 07:25:59 -0800 Subject: [PATCH] Use fast_float in CSS parser (#49751) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49751 fast_float promises significant improvements in float parsing performance and is already what's being used in folly internally. Unifying all percentage parsing on the new css parser logic, which can use fast_float internally. Changelog: [Internal] Reviewed By: rshest Differential Revision: D70392268 fbshipit-source-id: 56147e5478d008bc2c34579df035ec7acf8744bb --- .../renderer/components/view/conversions.h | 63 ++++++------------- .../react/renderer/css/CMakeLists.txt | 2 + .../react/renderer/css/CSSTokenizer.h | 61 ++++-------------- .../renderer/css/tests/CSSTokenizerTest.cpp | 10 +++ 4 files changed, 42 insertions(+), 94 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h index 25d43836295..5bdf3a5b82b 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -62,25 +63,6 @@ inline float yogaFloatFromFloat(Float value) { return (float)value; } -/* - * Converts string to float only if the entire string is valid float. - */ -inline std::optional stringToFloat(const std::string& string) { - try { - size_t pos = 0; - auto result = std::stof(string, &pos); - // Check if entire string was valid - if (pos == string.length()) { - return result; - } - } catch (...) { - // Ignore, caller falls back to default value. - return std::nullopt; - } - - return std::nullopt; -} - /* * `yoga::FloatOptional` <-> React Native's `Float` * @@ -483,19 +465,15 @@ inline void fromRawValue( result = yoga::StyleSizeLength::ofFitContent(); return; } else { - if (stringValue.back() == '%') { - auto tryValue = - stringToFloat(stringValue.substr(0, stringValue.length() - 1)); - if (tryValue.has_value()) { - result = yoga::StyleSizeLength::percent(tryValue.value()); - return; - } - } else { - auto tryValue = stringToFloat(stringValue); - if (tryValue.has_value()) { - result = yoga::StyleSizeLength::points(tryValue.value()); - return; - } + auto parsed = parseCSSProperty(stringValue); + if (std::holds_alternative(parsed)) { + result = yoga::StyleSizeLength::percent( + std::get(parsed).value); + return; + } else if (std::holds_alternative(parsed)) { + result = + yoga::StyleSizeLength::points(std::get(parsed).value); + return; } } } @@ -515,19 +493,14 @@ inline void fromRawValue( result = yoga::StyleLength::ofAuto(); return; } else { - if (stringValue.back() == '%') { - auto tryValue = - stringToFloat(stringValue.substr(0, stringValue.length() - 1)); - if (tryValue.has_value()) { - result = yoga::StyleLength::percent(tryValue.value()); - return; - } - } else { - auto tryValue = stringToFloat(stringValue); - if (tryValue.has_value()) { - result = yoga::StyleLength::points(tryValue.value()); - return; - } + auto parsed = parseCSSProperty(stringValue); + if (std::holds_alternative(parsed)) { + result = + yoga::StyleLength::percent(std::get(parsed).value); + return; + } else if (std::holds_alternative(parsed)) { + result = yoga::StyleLength::points(std::get(parsed).value); + return; } } } diff --git a/packages/react-native/ReactCommon/react/renderer/css/CMakeLists.txt b/packages/react-native/ReactCommon/react/renderer/css/CMakeLists.txt index 95a5c5a51f9..040e5ce23c3 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/CMakeLists.txt +++ b/packages/react-native/ReactCommon/react/renderer/css/CMakeLists.txt @@ -16,6 +16,7 @@ if("${react_renderer_css_SRC}" STREQUAL "") target_include_directories(react_renderer_css INTERFACE ${REACT_COMMON_DIR}) target_link_libraries(react_renderer_css INTERFACE + fast_float glog react_debug react_utils) @@ -26,6 +27,7 @@ else() target_include_directories(react_renderer_css PUBLIC ${REACT_COMMON_DIR}) target_link_libraries(react_renderer_css + fast_float glog react_debug react_utils) diff --git a/packages/react-native/ReactCommon/react/renderer/css/CSSTokenizer.h b/packages/react-native/ReactCommon/react/renderer/css/CSSTokenizer.h index 7ff498f5d5d..75b3a3a4970 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/CSSTokenizer.h +++ b/packages/react-native/ReactCommon/react/renderer/css/CSSTokenizer.h @@ -10,6 +10,7 @@ #include #include +#include #include namespace facebook::react { @@ -136,60 +137,22 @@ class CSSTokenizer { constexpr CSSToken consumeNumber() { // https://www.w3.org/TR/css-syntax-3/#consume-number // https://www.w3.org/TR/css-syntax-3/#convert-a-string-to-a-number - int32_t signPart = 1.0; - if (peek() == '+' || peek() == '-') { - if (peek() == '-') { - signPart = -1.0; - } - advance(); - } - double intPart = 0; - while (isDigit(peek())) { - intPart = intPart * 10 + (peek() - '0'); - advance(); - } + auto* b = remainingCharacters_.data(); + auto* e = b + remainingCharacters_.size(); - double fractionalPart = 0; - int32_t fractionDigits = 0; - if (peek() == '.') { - advance(); - while (isDigit(peek())) { - fractionalPart = fractionalPart * 10 + (peek() - '0'); - fractionDigits++; - advance(); - } - } - - int32_t exponentSign = 1.0; - double exponentPart = 0; - if ((peek() == 'e' || peek() == 'E') && - (isDigit(peek(1)) || - ((peek(1) == '+' || peek(1) == '-') && isDigit(peek(2))))) { - advance(); - if (peek() == '+' || peek() == '-') { - if (peek() == '-') { - exponentSign = -1.0; - } - advance(); - } - - while (isDigit(peek())) { - exponentPart = exponentPart * 10 + (peek() - '0'); - advance(); - } - } float value; - if (exponentPart == 0 && fractionalPart == 0) { - value = static_cast(signPart * intPart); - } else { - value = static_cast( - signPart * - (intPart + (fractionalPart * std::pow(10, -fractionDigits))) * - std::pow(10, exponentSign * exponentPart)); - } + fast_float::parse_options options{ + fast_float::chars_format::general | + fast_float::chars_format::allow_leading_plus}; + auto [ptr, ec] = fast_float::from_chars_advanced(b, e, value, options); + // Do we need to handle any other errors? + // bool isOk = ec == std::errc(); + + position_ += ptr - b; consumeRunningValue(); + return {CSSTokenType::Number, value}; } diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTokenizerTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTokenizerTest.cpp index c0b9f3c3f74..33b67e14b32 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTokenizerTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTokenizerTest.cpp @@ -96,6 +96,16 @@ TEST(CSSTokenizer, number_values) { CSSToken{CSSTokenType::Number, +.123e+0}, CSSToken{CSSTokenType::EndOfFile}); + EXPECT_TOKENS( + "3e-1000", + CSSToken{CSSTokenType::Number, 0}, + CSSToken{CSSTokenType::EndOfFile}); + + EXPECT_TOKENS( + "3e1000", + CSSToken{CSSTokenType::Number, std::numeric_limits::infinity()}, + CSSToken{CSSTokenType::EndOfFile}); + EXPECT_TOKENS( "+.9999999999", CSSToken{CSSTokenType::Number, +.9999999999},