Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use string_view for FastStringKey implementation #45914

Merged
merged 3 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/debug_utils-inl.h
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
Expand Up @@ -1275,11 +1275,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 @@ -1320,7 +1320,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 @@ -1382,7 +1382,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 @@ -1407,7 +1407,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
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
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
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