Skip to content

Commit fc74d8c

Browse files
Nick Lefevermeta-codesync[bot]
authored andcommitted
Disable inlining of ColorStop and ValueUnit toDynamic conversion (#54125)
Summary: Pull Request resolved: #54125 With ColorStop and ValueUnit being used at multiple locations, inlining the `toDynamic` conversion used for RN Android and using `std::format` to convert floating point values to string increased the binary size of RN Android by ~180KiB. This diff declares the functions outside the header to avoid inlining the functions and removes the dependency on `std::format` for the percent value string formatting. Changelog: [Internal] Reviewed By: javache Differential Revision: D84349391 fbshipit-source-id: 2aab6f057f8b69d0af3779f6edabdd42705d0445
1 parent 39ede95 commit fc74d8c

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "ColorStop.h"
9+
10+
namespace facebook::react {
11+
12+
#ifdef RN_SERIALIZABLE_STATE
13+
folly::dynamic ColorStop::toDynamic() const {
14+
folly::dynamic result = folly::dynamic::object();
15+
result["color"] = *color;
16+
result["position"] = position.toDynamic();
17+
return result;
18+
}
19+
#endif
20+
21+
}; // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/graphics/ColorStop.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <react/renderer/graphics/Color.h>
1111
#include <react/renderer/graphics/Float.h>
1212
#include <react/renderer/graphics/ValueUnit.h>
13+
#include <optional>
1314

1415
namespace facebook::react {
1516

@@ -19,12 +20,7 @@ struct ColorStop {
1920
ValueUnit position;
2021

2122
#ifdef RN_SERIALIZABLE_STATE
22-
folly::dynamic toDynamic() const {
23-
folly::dynamic result = folly::dynamic::object();
24-
result["color"] = *color;
25-
result["position"] = position.toDynamic();
26-
return result;
27-
}
23+
folly::dynamic toDynamic() const;
2824
#endif
2925
};
3026

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "ValueUnit.h"
9+
10+
#ifdef RN_SERIALIZABLE_STATE
11+
#include <double-conversion/double-conversion.h>
12+
#include <array>
13+
#include <string>
14+
#endif
15+
16+
namespace facebook::react {
17+
18+
#ifdef RN_SERIALIZABLE_STATE
19+
20+
std::string toString(double doubleValue, char suffix) {
21+
// Format taken from folly's toString
22+
static double_conversion::DoubleToStringConverter conv(
23+
0,
24+
NULL,
25+
NULL,
26+
'E',
27+
-6, // detail::kConvMaxDecimalInShortestLow,
28+
21, // detail::kConvMaxDecimalInShortestHigh,
29+
6, // max leading padding zeros
30+
1); // max trailing padding zeros
31+
std::array<char, 256> buffer{};
32+
double_conversion::StringBuilder builder(buffer.data(), buffer.size());
33+
if (!conv.ToShortest(doubleValue, &builder)) {
34+
// Serialize infinite and NaN as 0%
35+
builder.AddCharacter('0');
36+
builder.AddCharacter('%');
37+
}
38+
builder.AddCharacter(suffix);
39+
return builder.Finalize();
40+
}
41+
42+
folly::dynamic ValueUnit::toDynamic() const {
43+
switch (unit) {
44+
case UnitType::Undefined:
45+
return nullptr;
46+
case UnitType::Point:
47+
return value;
48+
case UnitType::Percent:
49+
return toString(value, '%');
50+
default:
51+
return nullptr;
52+
}
53+
}
54+
#endif
55+
56+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,7 @@ struct ValueUnit {
5050
}
5151

5252
#ifdef RN_SERIALIZABLE_STATE
53-
folly::dynamic toDynamic() const {
54-
switch (unit) {
55-
case UnitType::Undefined:
56-
return nullptr;
57-
case UnitType::Point:
58-
return value;
59-
case UnitType::Percent:
60-
return std::format("{}%", value);
61-
}
62-
}
53+
folly::dynamic toDynamic() const;
6354
#endif
6455
};
6556
} // namespace facebook::react

0 commit comments

Comments
 (0)