mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
src: use string_view for FastStringKey implementation
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
faf5c23084
commit
c2ed0ccb28
5 files changed
+21
-24
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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
|
||||
@@ -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, \
|
||||
@@ -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,
|
||||
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
+7
-13
@@ -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;
|
||||
}
|
||||
@@ -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_;
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user