Skip to content

Commit

Permalink
src: use string_view for FastStringKey implementation
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax authored and RafaelGSS committed Jan 4, 2023
1 parent 25d29f9 commit 52eb054
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/debug_utils-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unsigned BASE_BITS,
typename T,
Expand Down
10 changes: 5 additions & 5 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1280,11 +1280,11 @@ SnapshotableObject::SnapshotableObject(Environment* env,
: BaseObject(env, wrap), type_(type) {
}

const char* SnapshotableObject::GetTypeNameChars() const {
std::string_view SnapshotableObject::GetTypeName() const {
switch (type_) {
#define V(PropertyName, NativeTypeName) \
case EmbedderObjectType::k_##PropertyName: { \
return NativeTypeName::type_name.c_str(); \
return NativeTypeName::type_name.as_string_view(); \
}
SERIALIZABLE_OBJECT_TYPES(V)
#undef V
Expand Down Expand Up @@ -1325,7 +1325,7 @@ void DeserializeNodeInternalFields(Local<Object> 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, \
Expand Down Expand Up @@ -1387,7 +1387,7 @@ StartupData SerializeNodeContextInternalFields(Local<Object> 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,
Expand All @@ -1412,7 +1412,7 @@ void SerializeSnapshotableObjects(Realm* realm,
}
SnapshotableObject* ptr = static_cast<SnapshotableObject*>(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",
Expand Down
2 changes: 1 addition & 1 deletion src/node_snapshotable.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class SnapshotableObject : public BaseObject {
SnapshotableObject(Environment* env,
v8::Local<v8::Object> 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<v8::Context> context,
Expand Down
20 changes: 7 additions & 13 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,11 @@ inline bool IsSafeJsInt(v8::Local<v8::Value> 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;
}
Expand All @@ -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_;
}

Expand Down
10 changes: 5 additions & 5 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 52eb054

Please sign in to comment.