Files
react-native/ReactCommon/react/renderer/mounting/ShadowView.h
T
Samuel Susla 81a97de546 Prevent type conversion in Differentiator
Summary:
changelog: [internal]

Prevents 2 type converions:
1. int <-> size_t
2. int <-> int32_t

# Why is using size_t better when working with indexes.

## 1. Type conversion isn't for free.

Take this example

```
size_t calculate(int number) {
  return number + 1;
}
```

It generates following assembly (generated with armv8-a clang 10.0.0):

```
calculate(int):                          // calculate(int)
sub     sp, sp, #16                     // =16
str     w0, [sp, #12]
ldr     w8, [sp, #12]
add     w9, w8, #1                      // =1
mov     w8, w9
sxtw    x0, w8
add     sp, sp, #16                     // =16
ret
```

That's 9 instructions.

If we get rid of type conversion:

```
size_t calculate(size_t number) {
  return number + 1;
}
```

Assembly (generated with armv8-a clang 10.0.0):

```
calculate(unsigned long):                          // calculate(unsigned long)
sub     sp, sp, #16             // =16
str     x0, [sp, #8]
ldr     x8, [sp, #8]
add     x0, x8, #1              // =1
add     sp, sp, #16             // =16
ret
```

Compiler now produces only 7 instructions.

## Semantics

When using int for indexing, the type doesn't say much. By using `size_t`, just by looking at the type, it gives the reader more information about where it is coming from.

Reviewed By: JoshuaGross

Differential Revision: D24332248

fbshipit-source-id: 87ef982829ec14906ed9e002ea2e875fda4a0cd8
2020-10-15 15:15:41 -07:00

103 lines
2.5 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.
*/
#pragma once
#include <better/small_vector.h>
#include <folly/Hash.h>
#include <react/renderer/core/EventEmitter.h>
#include <react/renderer/core/LayoutMetrics.h>
#include <react/renderer/core/Props.h>
#include <react/renderer/core/ReactPrimitives.h>
#include <react/renderer/core/ShadowNode.h>
namespace facebook {
namespace react {
/*
* Describes a view that can be mounted.
* This is exposed to the mounting layer.
*/
struct ShadowView final {
ShadowView() = default;
ShadowView(ShadowView const &shadowView) = default;
ShadowView(ShadowView &&shadowView) noexcept = default;
/*
* Constructs a `ShadowView` from given `ShadowNode`.
*/
explicit ShadowView(ShadowNode const &shadowNode);
ShadowView &operator=(ShadowView const &other) = default;
ShadowView &operator=(ShadowView &&other) = default;
bool operator==(ShadowView const &rhs) const;
bool operator!=(ShadowView const &rhs) const;
ComponentName componentName{};
ComponentHandle componentHandle{};
Tag tag{};
Props::Shared props{};
EventEmitter::Shared eventEmitter{};
LayoutMetrics layoutMetrics{EmptyLayoutMetrics};
State::Shared state{};
};
#if RN_DEBUG_STRING_CONVERTIBLE
std::string getDebugName(ShadowView const &object);
std::vector<DebugStringConvertibleObject> getDebugProps(
ShadowView const &object,
DebugStringConvertibleOptions options);
#endif
/*
* Describes pair of a `ShadowView` and a `ShadowNode`.
* This is not exposed to the mounting layer.
*
*/
struct ShadowViewNodePair final {
using List = better::
small_vector<ShadowViewNodePair, kShadowNodeChildrenSmallVectorSize>;
ShadowView shadowView;
ShadowNode const *shadowNode;
bool flattened{false};
bool isConcreteView{true};
size_t mountIndex{0};
bool inOtherTree{false};
/*
* The stored pointer to `ShadowNode` represents an identity of the pair.
*/
bool operator==(const ShadowViewNodePair &rhs) const;
bool operator!=(const ShadowViewNodePair &rhs) const;
};
} // namespace react
} // namespace facebook
namespace std {
template <>
struct hash<facebook::react::ShadowView> {
size_t operator()(const facebook::react::ShadowView &shadowView) const {
return folly::hash::hash_combine(
0,
shadowView.componentHandle,
shadowView.tag,
shadowView.props,
shadowView.eventEmitter,
shadowView.state);
}
};
} // namespace std