From c2ed0ccb28f7e3ed173a45e91ed55d4ca2943449 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 21 Dec 2022 23:48:46 +0100 Subject: [PATCH] src: use string_view for FastStringKey implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/45914 Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu Reviewed-By: Tobias Nießen --- src/debug_utils-inl.h | 3 +++ src/node_snapshotable.cc | 10 +++++----- src/node_snapshotable.h | 2 +- src/util-inl.h | 20 +++++++------------- src/util.h | 10 +++++----- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h index 0fb760834d5792..ab00589269d4db 100644 --- a/src/debug_utils-inl.h +++ b/src/debug_utils-inl.h @@ -26,6 +26,9 @@ struct ToStringHelper { return value != nullptr ? value : "(null)"; } static std::string Convert(const std::string& value) { return value; } + static std::string Convert(std::string_view value) { + return std::string(value); + } static std::string Convert(bool value) { return value ? "true" : "false"; } template holder, per_process::Debug(DebugCategory::MKSNAPSHOT, \ "Object %p is %s\n", \ (*holder), \ - NativeTypeName::type_name.c_str()); \ + NativeTypeName::type_name.as_string_view()); \ env_ptr->EnqueueDeserializeRequest( \ NativeTypeName::Deserialize, \ holder, \ @@ -1387,7 +1387,7 @@ StartupData SerializeNodeContextInternalFields(Local holder, per_process::Debug(DebugCategory::MKSNAPSHOT, "Object %p is %s, ", *holder, - obj->GetTypeNameChars()); + obj->GetTypeName()); InternalFieldInfoBase* info = obj->Serialize(index); per_process::Debug(DebugCategory::MKSNAPSHOT, @@ -1412,7 +1412,7 @@ void SerializeSnapshotableObjects(Realm* realm, } SnapshotableObject* ptr = static_cast(obj); - const char* type_name = ptr->GetTypeNameChars(); + std::string type_name{ptr->GetTypeName()}; per_process::Debug(DebugCategory::MKSNAPSHOT, "Serialize snapshotable object %i (%p), " "object=%p, type=%s\n", diff --git a/src/node_snapshotable.h b/src/node_snapshotable.h index 39a2a71f45bd49..a825350806bfb0 100644 --- a/src/node_snapshotable.h +++ b/src/node_snapshotable.h @@ -101,7 +101,7 @@ class SnapshotableObject : public BaseObject { SnapshotableObject(Environment* env, v8::Local wrap, EmbedderObjectType type); - const char* GetTypeNameChars() const; + std::string_view GetTypeName() const; // If returns false, the object will not be serialized. virtual bool PrepareForSerialization(v8::Local context, diff --git a/src/util-inl.h b/src/util-inl.h index 18f829d2775f0d..f98bb16aa7687a 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -574,11 +574,11 @@ inline bool IsSafeJsInt(v8::Local v) { return false; } -constexpr size_t FastStringKey::HashImpl(const char* str) { +constexpr size_t FastStringKey::HashImpl(std::string_view str) { // Low-quality hash (djb2), but just fine for current use cases. size_t h = 5381; - while (*str != '\0') { - h = h * 33 + *(str++); // NOLINT(readability/pointer_notation) + for (const char c : str) { + h = h * 33 + c; } return h; } @@ -589,19 +589,13 @@ constexpr size_t FastStringKey::Hash::operator()( } constexpr bool FastStringKey::operator==(const FastStringKey& other) const { - const char* p1 = name_; - const char* p2 = other.name_; - if (p1 == p2) return true; - do { - if (*(p1++) != *(p2++)) return false; - } while (*p1 != '\0'); - return *p2 == '\0'; + return name_ == other.name_; } -constexpr FastStringKey::FastStringKey(const char* name) - : name_(name), cached_hash_(HashImpl(name)) {} +constexpr FastStringKey::FastStringKey(std::string_view name) + : name_(name), cached_hash_(HashImpl(name)) {} -constexpr const char* FastStringKey::c_str() const { +constexpr std::string_view FastStringKey::as_string_view() const { return name_; } diff --git a/src/util.h b/src/util.h index 0bb6d2648ef6aa..399018655ec38f 100644 --- a/src/util.h +++ b/src/util.h @@ -838,20 +838,20 @@ class PersistentToLocal { // computations. class FastStringKey { public: - constexpr explicit FastStringKey(const char* name); + constexpr explicit FastStringKey(std::string_view name); struct Hash { constexpr size_t operator()(const FastStringKey& key) const; }; constexpr bool operator==(const FastStringKey& other) const; - constexpr const char* c_str() const; + constexpr std::string_view as_string_view() const; private: - static constexpr size_t HashImpl(const char* str); + static constexpr size_t HashImpl(std::string_view str); - const char* name_; - size_t cached_hash_; + const std::string_view name_; + const size_t cached_hash_; }; // Like std::static_pointer_cast but for unique_ptr with the default deleter.