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
This commit is contained in:
Pieter De Baets authored and Facebook GitHub Bot committed 2025-03-04 07:25:59 -08:00
1 parent 3dc78f804a
commit 5411b143ef
4 files changed
+42 -94

No files matched your search

@@ -15,6 +15,7 @@
#include <react/renderer/core/RawProps.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/css/CSSAngle.h>
#include <react/renderer/css/CSSNumber.h>
#include <react/renderer/css/CSSPercentage.h>
#include <react/renderer/css/CSSValueParser.h>
#include <react/renderer/graphics/BackgroundImage.h>
@@ -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<float> 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<CSSNumber, CSSPercentage>(stringValue);
if (std::holds_alternative<CSSPercentage>(parsed)) {
result = yoga::StyleSizeLength::percent(
std::get<CSSPercentage>(parsed).value);
return;
} else if (std::holds_alternative<CSSNumber>(parsed)) {
result =
yoga::StyleSizeLength::points(std::get<CSSNumber>(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<CSSNumber, CSSPercentage>(stringValue);
if (std::holds_alternative<CSSPercentage>(parsed)) {
result =
yoga::StyleLength::percent(std::get<CSSPercentage>(parsed).value);
return;
} else if (std::holds_alternative<CSSNumber>(parsed)) {
result = yoga::StyleLength::points(std::get<CSSNumber>(parsed).value);
return;
}
}
}
@@ -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)
@@ -10,6 +10,7 @@
#include <cmath>
#include <string_view>
#include <fast_float/fast_float.h>
#include <react/renderer/css/CSSToken.h>
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<float>(signPart * intPart);
} else {
value = static_cast<float>(
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};
}
@@ -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<float>::infinity()},
CSSToken{CSSTokenType::EndOfFile});
EXPECT_TOKENS(
"+.9999999999",
CSSToken{CSSTokenType::Number, +.9999999999},