Files
react-native/ReactCommon/react/renderer/core/RawPropsKey.cpp
T
Vincent Lee 25c5d194ad Compare strings by value instead of reference
Summary:
LLD, our new iOS linker, is an ongoing effort to migrate our old outdated ld64 linker. As part of our effort to rollout LLD to all apps, we are making sure LLD reaches parity with ld64.

Due to Identical Code Folding (ICF), LLD and ld64 handles strings differently. LLD treats each string as a separate object in memory even if the values of the strings are the same. ld64 happens to aggregate these values across files. This behavior creates a subtle difference on our codebase when we start comparing by value or by reference.

`char * ` fields from `RawPropsKey.h` are using `==` which compares by its address. Here, we cast the buffer to a string to make the comparison, while avoiding the cast if one happens to be null.

Changelog: [Internal]

Reviewed By: int3, JoshuaGross

Differential Revision: D30444176

fbshipit-source-id: 74216926803adbece05206ddd8478cc3c8e6812e
2021-08-25 13:25:42 -07:00

74 lines
1.9 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "RawPropsKey.h"
#include <cassert>
#include <cstring>
#include <react/debug/react_native_assert.h>
#include <react/renderer/core/RawPropsPrimitives.h>
namespace facebook {
namespace react {
void RawPropsKey::render(char *buffer, RawPropsPropNameLength *length)
const noexcept {
*length = 0;
// Prefix
if (prefix) {
auto prefixLength =
static_cast<RawPropsPropNameLength>(std::strlen(prefix));
std::memcpy(buffer, prefix, prefixLength);
*length = prefixLength;
}
// Name
auto nameLength = static_cast<RawPropsPropNameLength>(std::strlen(name));
std::memcpy(buffer + *length, name, nameLength);
*length += nameLength;
// Suffix
if (suffix) {
auto suffixLength =
static_cast<RawPropsPropNameLength>(std::strlen(suffix));
std::memcpy(buffer + *length, suffix, suffixLength);
*length += suffixLength;
}
react_native_assert(*length < kPropNameLengthHardCap);
}
RawPropsKey::operator std::string() const noexcept {
char buffer[kPropNameLengthHardCap];
RawPropsPropNameLength length = 0;
render(buffer, &length);
react_native_assert(length < kPropNameLengthHardCap);
return std::string{buffer, length};
}
static bool areFieldsEqual(char const *lhs, char const *rhs) {
if (lhs == nullptr || rhs == nullptr) {
return lhs == rhs;
}
return std::string(lhs) == std::string(rhs);
}
bool operator==(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept {
// Note: We check the name first.
return areFieldsEqual(lhs.name, rhs.name) &&
areFieldsEqual(lhs.prefix, rhs.prefix) &&
areFieldsEqual(lhs.suffix, rhs.suffix);
}
bool operator!=(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept {
return !(lhs == rhs);
}
} // namespace react
} // namespace facebook