Skip to content

Commit 9ca31cd

Browse files
addaleaxjuanarbol
authored andcommitted
src: use string_view for FastStringKey implementation
PR-URL: #45914 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 53684e4 commit 9ca31cd

5 files changed

+21
-24
lines changed

src/debug_utils-inl.h

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ struct ToStringHelper {
2626
return value != nullptr ? value : "(null)";
2727
}
2828
static std::string Convert(const std::string& value) { return value; }
29+
static std::string Convert(std::string_view value) {
30+
return std::string(value);
31+
}
2932
static std::string Convert(bool value) { return value ? "true" : "false"; }
3033
template <unsigned BASE_BITS,
3134
typename T,

src/node_snapshotable.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -1280,11 +1280,11 @@ SnapshotableObject::SnapshotableObject(Environment* env,
12801280
: BaseObject(env, wrap), type_(type) {
12811281
}
12821282

1283-
const char* SnapshotableObject::GetTypeNameChars() const {
1283+
std::string_view SnapshotableObject::GetTypeName() const {
12841284
switch (type_) {
12851285
#define V(PropertyName, NativeTypeName) \
12861286
case EmbedderObjectType::k_##PropertyName: { \
1287-
return NativeTypeName::type_name.c_str(); \
1287+
return NativeTypeName::type_name.as_string_view(); \
12881288
}
12891289
SERIALIZABLE_OBJECT_TYPES(V)
12901290
#undef V
@@ -1325,7 +1325,7 @@ void DeserializeNodeInternalFields(Local<Object> holder,
13251325
per_process::Debug(DebugCategory::MKSNAPSHOT, \
13261326
"Object %p is %s\n", \
13271327
(*holder), \
1328-
NativeTypeName::type_name.c_str()); \
1328+
NativeTypeName::type_name.as_string_view()); \
13291329
env_ptr->EnqueueDeserializeRequest( \
13301330
NativeTypeName::Deserialize, \
13311331
holder, \
@@ -1387,7 +1387,7 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
13871387
per_process::Debug(DebugCategory::MKSNAPSHOT,
13881388
"Object %p is %s, ",
13891389
*holder,
1390-
obj->GetTypeNameChars());
1390+
obj->GetTypeName());
13911391
InternalFieldInfoBase* info = obj->Serialize(index);
13921392

13931393
per_process::Debug(DebugCategory::MKSNAPSHOT,
@@ -1412,7 +1412,7 @@ void SerializeSnapshotableObjects(Realm* realm,
14121412
}
14131413
SnapshotableObject* ptr = static_cast<SnapshotableObject*>(obj);
14141414

1415-
const char* type_name = ptr->GetTypeNameChars();
1415+
std::string type_name{ptr->GetTypeName()};
14161416
per_process::Debug(DebugCategory::MKSNAPSHOT,
14171417
"Serialize snapshotable object %i (%p), "
14181418
"object=%p, type=%s\n",

src/node_snapshotable.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class SnapshotableObject : public BaseObject {
101101
SnapshotableObject(Environment* env,
102102
v8::Local<v8::Object> wrap,
103103
EmbedderObjectType type);
104-
const char* GetTypeNameChars() const;
104+
std::string_view GetTypeName() const;
105105

106106
// If returns false, the object will not be serialized.
107107
virtual bool PrepareForSerialization(v8::Local<v8::Context> context,

src/util-inl.h

+7-13
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,11 @@ inline bool IsSafeJsInt(v8::Local<v8::Value> v) {
574574
return false;
575575
}
576576

577-
constexpr size_t FastStringKey::HashImpl(const char* str) {
577+
constexpr size_t FastStringKey::HashImpl(std::string_view str) {
578578
// Low-quality hash (djb2), but just fine for current use cases.
579579
size_t h = 5381;
580-
while (*str != '\0') {
581-
h = h * 33 + *(str++); // NOLINT(readability/pointer_notation)
580+
for (const char c : str) {
581+
h = h * 33 + c;
582582
}
583583
return h;
584584
}
@@ -589,19 +589,13 @@ constexpr size_t FastStringKey::Hash::operator()(
589589
}
590590

591591
constexpr bool FastStringKey::operator==(const FastStringKey& other) const {
592-
const char* p1 = name_;
593-
const char* p2 = other.name_;
594-
if (p1 == p2) return true;
595-
do {
596-
if (*(p1++) != *(p2++)) return false;
597-
} while (*p1 != '\0');
598-
return *p2 == '\0';
592+
return name_ == other.name_;
599593
}
600594

601-
constexpr FastStringKey::FastStringKey(const char* name)
602-
: name_(name), cached_hash_(HashImpl(name)) {}
595+
constexpr FastStringKey::FastStringKey(std::string_view name)
596+
: name_(name), cached_hash_(HashImpl(name)) {}
603597

604-
constexpr const char* FastStringKey::c_str() const {
598+
constexpr std::string_view FastStringKey::as_string_view() const {
605599
return name_;
606600
}
607601

src/util.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -837,20 +837,20 @@ class PersistentToLocal {
837837
// computations.
838838
class FastStringKey {
839839
public:
840-
constexpr explicit FastStringKey(const char* name);
840+
constexpr explicit FastStringKey(std::string_view name);
841841

842842
struct Hash {
843843
constexpr size_t operator()(const FastStringKey& key) const;
844844
};
845845
constexpr bool operator==(const FastStringKey& other) const;
846846

847-
constexpr const char* c_str() const;
847+
constexpr std::string_view as_string_view() const;
848848

849849
private:
850-
static constexpr size_t HashImpl(const char* str);
850+
static constexpr size_t HashImpl(std::string_view str);
851851

852-
const char* name_;
853-
size_t cached_hash_;
852+
const std::string_view name_;
853+
const size_t cached_hash_;
854854
};
855855

856856
// Like std::static_pointer_cast but for unique_ptr with the default deleter.

0 commit comments

Comments
 (0)