From df229590b2a99822cbc4a7097404da13fbf48523 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 29 Sep 2019 20:02:24 -0700 Subject: [PATCH] Fabric: Adding missing `noexcept` operator to functions in raw props parsing infra Summary: This part of the codebase is very perf sensitive and designed to work without exceptions enabled. Most of the method was `noexcept` all the time, but some of those missing that by mistake. Reviewed By: sammy-SC Differential Revision: D17629426 fbshipit-source-id: b311e4b7eff8e2b7cf29518288480d3a812dda44 --- ReactCommon/fabric/core/primitives/RawProps.cpp | 2 +- ReactCommon/fabric/core/primitives/RawProps.h | 2 +- ReactCommon/fabric/core/primitives/RawPropsKey.cpp | 9 +++++---- ReactCommon/fabric/core/primitives/RawPropsKey.h | 8 ++++---- ReactCommon/fabric/core/primitives/RawPropsKeyMap.cpp | 10 ++++++---- ReactCommon/fabric/core/primitives/RawPropsKeyMap.h | 10 ++++++---- ReactCommon/fabric/core/primitives/RawPropsParser.cpp | 6 +++--- ReactCommon/fabric/core/primitives/RawPropsParser.h | 9 +++++---- ReactCommon/fabric/core/primitives/RawValue.h | 2 +- 9 files changed, 32 insertions(+), 26 deletions(-) diff --git a/ReactCommon/fabric/core/primitives/RawProps.cpp b/ReactCommon/fabric/core/primitives/RawProps.cpp index f655a2437d4..8ecab77d5e9 100644 --- a/ReactCommon/fabric/core/primitives/RawProps.cpp +++ b/ReactCommon/fabric/core/primitives/RawProps.cpp @@ -46,7 +46,7 @@ RawProps::RawProps(folly::dynamic const &dynamic) noexcept { dynamic_ = dynamic; } -void RawProps::parse(RawPropsParser const &parser) const { +void RawProps::parse(RawPropsParser const &parser) const noexcept { assert(parser_ == nullptr && "A parser was already assigned."); parser_ = &parser; parser.preparse(*this); diff --git a/ReactCommon/fabric/core/primitives/RawProps.h b/ReactCommon/fabric/core/primitives/RawProps.h index 29ac85bcbda..746f7ed72b9 100644 --- a/ReactCommon/fabric/core/primitives/RawProps.h +++ b/ReactCommon/fabric/core/primitives/RawProps.h @@ -74,7 +74,7 @@ class RawProps final { RawProps(RawProps const &other) noexcept = delete; RawProps &operator=(RawProps const &other) noexcept = delete; - void parse(RawPropsParser const &parser) const; + void parse(RawPropsParser const &parser) const noexcept; #ifdef ANDROID /* diff --git a/ReactCommon/fabric/core/primitives/RawPropsKey.cpp b/ReactCommon/fabric/core/primitives/RawPropsKey.cpp index 8deae017df8..e22ffe261b1 100644 --- a/ReactCommon/fabric/core/primitives/RawPropsKey.cpp +++ b/ReactCommon/fabric/core/primitives/RawPropsKey.cpp @@ -15,7 +15,8 @@ namespace facebook { namespace react { -void RawPropsKey::render(char *buffer, RawPropsPropNameLength *length) const { +void RawPropsKey::render(char *buffer, RawPropsPropNameLength *length) const + noexcept { *length = 0; // Prefix @@ -39,7 +40,7 @@ void RawPropsKey::render(char *buffer, RawPropsPropNameLength *length) const { assert(*length < kPropNameLengthHardCap); } -RawPropsKey::operator std::string() const { +RawPropsKey::operator std::string() const noexcept { char buffer[kPropNameLengthHardCap]; RawPropsPropNameLength length = 0; render(buffer, &length); @@ -47,13 +48,13 @@ RawPropsKey::operator std::string() const { return std::string{buffer, length}; } -bool operator==(RawPropsKey const &lhs, RawPropsKey const &rhs) { +bool operator==(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept { // Note: We check the name first. return lhs.name == rhs.name && lhs.prefix == rhs.prefix && lhs.suffix == rhs.suffix; } -bool operator!=(RawPropsKey const &lhs, RawPropsKey const &rhs) { +bool operator!=(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept { return !(lhs == rhs); } diff --git a/ReactCommon/fabric/core/primitives/RawPropsKey.h b/ReactCommon/fabric/core/primitives/RawPropsKey.h index d209230026c..76be2c7bcc3 100644 --- a/ReactCommon/fabric/core/primitives/RawPropsKey.h +++ b/ReactCommon/fabric/core/primitives/RawPropsKey.h @@ -26,17 +26,17 @@ class RawPropsKey final { /* * Converts to `std::string`. */ - explicit operator std::string() const; + explicit operator std::string() const noexcept; /* * Renders compound prop name to given buffer and put the resulting length * into `length`. */ - void render(char *buffer, RawPropsPropNameLength *length) const; + void render(char *buffer, RawPropsPropNameLength *length) const noexcept; }; -bool operator==(RawPropsKey const &lhs, RawPropsKey const &rhs); -bool operator!=(RawPropsKey const &lhs, RawPropsKey const &rhs); +bool operator==(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept; +bool operator!=(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/core/primitives/RawPropsKeyMap.cpp b/ReactCommon/fabric/core/primitives/RawPropsKeyMap.cpp index 913e6505070..2a7c2ea77fc 100644 --- a/ReactCommon/fabric/core/primitives/RawPropsKeyMap.cpp +++ b/ReactCommon/fabric/core/primitives/RawPropsKeyMap.cpp @@ -14,7 +14,7 @@ namespace facebook { namespace react { -int RawPropsKeyMap::comparator(void const *lhs, void const *rhs) { +int RawPropsKeyMap::comparator(void const *lhs, void const *rhs) noexcept { auto a = static_cast(lhs); auto b = static_cast(rhs); @@ -25,14 +25,16 @@ int RawPropsKeyMap::comparator(void const *lhs, void const *rhs) { return std::memcmp(a->name, b->name, a->length); } -void RawPropsKeyMap::insert(RawPropsKey const &key, RawPropsValueIndex value) { +void RawPropsKeyMap::insert( + RawPropsKey const &key, + RawPropsValueIndex value) noexcept { auto item = Item{}; item.value = value; key.render(item.name, &item.length); items_.push_back(item); } -void RawPropsKeyMap::reindex() { +void RawPropsKeyMap::reindex() noexcept { // Sorting `items_` by property names length and then lexicographically. std::qsort( items_.data(), @@ -60,7 +62,7 @@ void RawPropsKeyMap::reindex() { RawPropsValueIndex RawPropsKeyMap::at( char const *name, - RawPropsPropNameLength length) { + RawPropsPropNameLength length) noexcept { assert(length > 0); assert(length < kPropNameLengthHardCap); // 1. Find the bucket. diff --git a/ReactCommon/fabric/core/primitives/RawPropsKeyMap.h b/ReactCommon/fabric/core/primitives/RawPropsKeyMap.h index 3fa8347a4e6..0a82f9690e2 100644 --- a/ReactCommon/fabric/core/primitives/RawPropsKeyMap.h +++ b/ReactCommon/fabric/core/primitives/RawPropsKeyMap.h @@ -27,22 +27,24 @@ class RawPropsKeyMap final { /* * Stores `value` with by given `key`. */ - void insert(RawPropsKey const &key, RawPropsValueIndex value); + void insert(RawPropsKey const &key, RawPropsValueIndex value) noexcept; /* * Reindexes the stored data. * Must be called before `at` (after calling a bunch of `add`s). */ - void reindex(); + void reindex() noexcept; /* * Finds and returns the `value` (some index) by given `key`. * Returns `kRawPropsValueIndexEmpty` if the value wan't found. */ - RawPropsValueIndex at(char const *name, RawPropsPropNameLength length); + RawPropsValueIndex at( + char const *name, + RawPropsPropNameLength length) noexcept; private: - static int comparator(void const *lhs, void const *rhs); + static int comparator(void const *lhs, void const *rhs) noexcept; struct Item { RawPropsValueIndex value; diff --git a/ReactCommon/fabric/core/primitives/RawPropsParser.cpp b/ReactCommon/fabric/core/primitives/RawPropsParser.cpp index 0da2edf6202..619dffe95f6 100644 --- a/ReactCommon/fabric/core/primitives/RawPropsParser.cpp +++ b/ReactCommon/fabric/core/primitives/RawPropsParser.cpp @@ -15,7 +15,7 @@ namespace react { RawValue const *RawPropsParser::at( RawProps const &rawProps, - RawPropsKey const &key) const { + RawPropsKey const &key) const noexcept { if (UNLIKELY(!ready_)) { // This is not thread-safe part; this happens only during initialization of // a `ComponentDescriptor` where it is actually safe. @@ -38,12 +38,12 @@ RawValue const *RawPropsParser::at( : &rawProps.values_[valueIndex]; } -void RawPropsParser::postPrepare() { +void RawPropsParser::postPrepare() noexcept { ready_ = true; nameToIndex_.reindex(); } -void RawPropsParser::preparse(RawProps const &rawProps) const { +void RawPropsParser::preparse(RawProps const &rawProps) const noexcept { rawProps.keyIndexToValueIndex_.resize(size_, kRawPropsValueIndexEmpty); // Resetting the cursor, the next increment will give `0`. diff --git a/ReactCommon/fabric/core/primitives/RawPropsParser.h b/ReactCommon/fabric/core/primitives/RawPropsParser.h index e9b142fdd51..9e7562903aa 100644 --- a/ReactCommon/fabric/core/primitives/RawPropsParser.h +++ b/ReactCommon/fabric/core/primitives/RawPropsParser.h @@ -35,7 +35,7 @@ class RawPropsParser final { * To be used by `ConcreteComponentDescriptor` only. */ template - void prepare() { + void prepare() noexcept { static_assert( std::is_base_of::value, "PropsT must be a descendant of Props"); @@ -54,17 +54,18 @@ class RawPropsParser final { /* * To be used by `RawProps` only. */ - void preparse(RawProps const &rawProps) const; + void preparse(RawProps const &rawProps) const noexcept; /* * Non-generic part of `prepare`. */ - void postPrepare(); + void postPrepare() noexcept; /* * To be used by `RawProps` only. */ - RawValue const *at(RawProps const &rawProps, RawPropsKey const &key) const; + RawValue const *at(RawProps const &rawProps, RawPropsKey const &key) const + noexcept; mutable better::small_vector keys_{}; diff --git a/ReactCommon/fabric/core/primitives/RawValue.h b/ReactCommon/fabric/core/primitives/RawValue.h index e5366cc902f..2b7564f2779 100644 --- a/ReactCommon/fabric/core/primitives/RawValue.h +++ b/ReactCommon/fabric/core/primitives/RawValue.h @@ -95,7 +95,7 @@ class RawValue { return castValue(dynamic_, (T *)nullptr); } - inline explicit operator folly::dynamic() const { + inline explicit operator folly::dynamic() const noexcept { return dynamic_; }