perf: avoid duplicate calculations in gin_helper::Dictionary getters (#43073)

* perf: cache the dictionary handle

* refactor: prefer result.IsJust() over !result.IsNothing()

for consistency

* refactor: prefer maybe.FromMaybe() over maybe.IsJust() && maybe.FromJust()

the inlined code is simpler

* refactor: simplify Get() impl

* refactor: add private helper Dictionary::MakeKey()

refactor: add private helper Dictionary::MakeHiddenKey()
This commit is contained in:
Charles Kerr 2024-07-29 12:43:28 -05:00 committed by GitHub
parent e70ce89235
commit 7e9eb9e3f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 41 deletions

View file

@ -42,16 +42,16 @@ class Dictionary : public gin::Dictionary {
// 3. It accepts arbitrary type of key. // 3. It accepts arbitrary type of key.
template <typename K, typename V> template <typename K, typename V>
bool Get(const K& key, V* out) const { bool Get(const K& key, V* out) const {
v8::Isolate* const iso = isolate();
v8::Local<v8::Object> handle = GetHandle();
v8::Local<v8::Context> context = iso->GetCurrentContext();
v8::Local<v8::Value> v8_key = gin::ConvertToV8(iso, key);
v8::Local<v8::Value> value;
// Check for existence before getting, otherwise this method will always // Check for existence before getting, otherwise this method will always
// returns true when T == v8::Local<v8::Value>. // returns true when T == v8::Local<v8::Value>.
v8::Local<v8::Context> context = isolate()->GetCurrentContext(); return handle->Has(context, v8_key).FromMaybe(false) &&
v8::Local<v8::Value> v8_key = gin::ConvertToV8(isolate(), key); handle->Get(context, v8_key).ToLocal(&value) &&
v8::Local<v8::Value> value; gin::ConvertFromV8(iso, value, out);
v8::Maybe<bool> result = GetHandle()->Has(context, v8_key);
if (result.IsJust() && result.FromJust() &&
GetHandle()->Get(context, v8_key).ToLocal(&value))
return gin::ConvertFromV8(isolate(), value, out);
return false;
} }
// Differences from the Set method in gin::Dictionary: // Differences from the Set method in gin::Dictionary:
@ -64,7 +64,7 @@ class Dictionary : public gin::Dictionary {
v8::Maybe<bool> result = v8::Maybe<bool> result =
GetHandle()->Set(isolate()->GetCurrentContext(), GetHandle()->Set(isolate()->GetCurrentContext(),
gin::ConvertToV8(isolate(), key), v8_value); gin::ConvertToV8(isolate(), key), v8_value);
return !result.IsNothing() && result.FromJust(); return result.FromMaybe(false);
} }
// Like normal Get but put result in an std::optional. // Like normal Get but put result in an std::optional.
@ -81,28 +81,27 @@ class Dictionary : public gin::Dictionary {
template <typename T> template <typename T>
bool GetHidden(std::string_view key, T* out) const { bool GetHidden(std::string_view key, T* out) const {
v8::Local<v8::Context> context = isolate()->GetCurrentContext(); v8::Isolate* const iso = isolate();
v8::Local<v8::Private> privateKey = v8::Local<v8::Object> handle = GetHandle();
v8::Private::ForApi(isolate(), gin::StringToV8(isolate(), key)); v8::Local<v8::Context> context = iso->GetCurrentContext();
v8::Local<v8::Private> privateKey = MakeHiddenKey(key);
v8::Local<v8::Value> value; v8::Local<v8::Value> value;
v8::Maybe<bool> result = GetHandle()->HasPrivate(context, privateKey); return handle->HasPrivate(context, privateKey).FromMaybe(false) &&
if (result.IsJust() && result.FromJust() && handle->GetPrivate(context, privateKey).ToLocal(&value) &&
GetHandle()->GetPrivate(context, privateKey).ToLocal(&value)) gin::ConvertFromV8(iso, value, out);
return gin::ConvertFromV8(isolate(), value, out);
return false;
} }
template <typename T> template <typename T>
bool SetHidden(std::string_view key, T val) { bool SetHidden(std::string_view key, T val) {
v8::Isolate* const iso = isolate();
v8::Local<v8::Value> v8_value; v8::Local<v8::Value> v8_value;
if (!gin::TryConvertToV8(isolate(), val, &v8_value)) if (!gin::TryConvertToV8(iso, val, &v8_value))
return false; return false;
v8::Local<v8::Context> context = isolate()->GetCurrentContext(); v8::Local<v8::Context> context = iso->GetCurrentContext();
v8::Local<v8::Private> privateKey = v8::Local<v8::Private> privateKey = MakeHiddenKey(key);
v8::Private::ForApi(isolate(), gin::StringToV8(isolate(), key));
v8::Maybe<bool> result = v8::Maybe<bool> result =
GetHandle()->SetPrivate(context, privateKey, v8_value); GetHandle()->SetPrivate(context, privateKey, v8_value);
return !result.IsNothing() && result.FromJust(); return result.FromMaybe(false);
} }
template <typename T> template <typename T>
@ -110,7 +109,7 @@ class Dictionary : public gin::Dictionary {
auto context = isolate()->GetCurrentContext(); auto context = isolate()->GetCurrentContext();
auto templ = CallbackTraits<T>::CreateTemplate(isolate(), callback); auto templ = CallbackTraits<T>::CreateTemplate(isolate(), callback);
return GetHandle() return GetHandle()
->Set(context, gin::StringToV8(isolate(), key), ->Set(context, MakeKey(key),
templ->GetFunction(context).ToLocalChecked()) templ->GetFunction(context).ToLocalChecked())
.ToChecked(); .ToChecked();
} }
@ -130,7 +129,7 @@ class Dictionary : public gin::Dictionary {
return GetHandle() return GetHandle()
->SetNativeDataProperty( ->SetNativeDataProperty(
context, gin::StringToV8(isolate(), key), context, MakeKey(key),
[](v8::Local<v8::Name> property_name, [](v8::Local<v8::Name> property_name,
const v8::PropertyCallbackInfo<v8::Value>& info) { const v8::PropertyCallbackInfo<v8::Value>& info) {
AccessorValue<V> acc_value; AccessorValue<V> acc_value;
@ -153,9 +152,8 @@ class Dictionary : public gin::Dictionary {
if (!gin::TryConvertToV8(isolate(), val, &v8_value)) if (!gin::TryConvertToV8(isolate(), val, &v8_value))
return false; return false;
v8::Maybe<bool> result = GetHandle()->DefineOwnProperty( v8::Maybe<bool> result = GetHandle()->DefineOwnProperty(
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key), isolate()->GetCurrentContext(), MakeKey(key), v8_value, v8::ReadOnly);
v8_value, v8::ReadOnly); return result.FromMaybe(false);
return !result.IsNothing() && result.FromJust();
} }
// Note: If we plan to add more Set methods, consider adding an option instead // Note: If we plan to add more Set methods, consider adding an option instead
@ -166,22 +164,21 @@ class Dictionary : public gin::Dictionary {
if (!gin::TryConvertToV8(isolate(), val, &v8_value)) if (!gin::TryConvertToV8(isolate(), val, &v8_value))
return false; return false;
v8::Maybe<bool> result = GetHandle()->DefineOwnProperty( v8::Maybe<bool> result = GetHandle()->DefineOwnProperty(
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key), isolate()->GetCurrentContext(), MakeKey(key), v8_value,
v8_value,
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
return !result.IsNothing() && result.FromJust(); return result.FromMaybe(false);
} }
bool Has(std::string_view key) const { bool Has(std::string_view key) const {
v8::Maybe<bool> result = GetHandle()->Has(isolate()->GetCurrentContext(), v8::Maybe<bool> result =
gin::StringToV8(isolate(), key)); GetHandle()->Has(isolate()->GetCurrentContext(), MakeKey(key));
return !result.IsNothing() && result.FromJust(); return result.FromMaybe(false);
} }
bool Delete(std::string_view key) { bool Delete(std::string_view key) {
v8::Maybe<bool> result = GetHandle()->Delete( v8::Maybe<bool> result =
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key)); GetHandle()->Delete(isolate()->GetCurrentContext(), MakeKey(key));
return !result.IsNothing() && result.FromJust(); return result.FromMaybe(false);
} }
bool IsEmpty() const { return isolate() == nullptr || GetHandle().IsEmpty(); } bool IsEmpty() const { return isolate() == nullptr || GetHandle().IsEmpty(); }
@ -194,6 +191,16 @@ class Dictionary : public gin::Dictionary {
private: private:
// DO NOT ADD ANY DATA MEMBER. // DO NOT ADD ANY DATA MEMBER.
[[nodiscard]] v8::Local<v8::String> MakeKey(
const std::string_view key) const {
return gin::StringToV8(isolate(), key);
}
[[nodiscard]] v8::Local<v8::Private> MakeHiddenKey(
const std::string_view key) const {
return v8::Private::ForApi(isolate(), MakeKey(key));
}
}; };
} // namespace gin_helper } // namespace gin_helper

View file

@ -32,14 +32,13 @@ class PersistentDictionary {
template <typename K, typename V> template <typename K, typename V>
bool Get(const K& key, V* out) const { bool Get(const K& key, V* out) const {
const auto handle = GetHandle();
v8::Local<v8::Context> context = isolate_->GetCurrentContext(); v8::Local<v8::Context> context = isolate_->GetCurrentContext();
v8::Local<v8::Value> v8_key = gin::ConvertToV8(isolate_, key); v8::Local<v8::Value> v8_key = gin::ConvertToV8(isolate_, key);
v8::Local<v8::Value> value; v8::Local<v8::Value> value;
v8::Maybe<bool> result = GetHandle()->Has(context, v8_key); return handle->Has(context, v8_key).FromMaybe(false) &&
if (result.IsJust() && result.FromJust() && handle->Get(context, v8_key).ToLocal(&value) &&
GetHandle()->Get(context, v8_key).ToLocal(&value)) gin::ConvertFromV8(isolate_, value, out);
return gin::ConvertFromV8(isolate_, value, out);
return false;
} }
private: private: