perf: avoid unnecessary base value clone (#38537)

This commit is contained in:
Charles Kerr 2023-06-02 08:38:29 -05:00 committed by GitHub
parent d818f35ad4
commit f247ca3f62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 28 deletions

View file

@ -329,10 +329,8 @@ void ElectronBrowserContext::InitPrefs() {
#endif #endif
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
base::Value::List current_dictionaries =
prefs()->GetList(spellcheck::prefs::kSpellCheckDictionaries).Clone();
// No configured dictionaries, the default will be en-US // No configured dictionaries, the default will be en-US
if (current_dictionaries.empty()) { if (prefs()->GetList(spellcheck::prefs::kSpellCheckDictionaries).empty()) {
std::string default_code = spellcheck::GetCorrespondingSpellCheckLanguage( std::string default_code = spellcheck::GetCorrespondingSpellCheckLanguage(
base::i18n::GetConfiguredLocale()); base::i18n::GetConfiguredLocale());
if (!default_code.empty()) { if (!default_code.empty()) {

View file

@ -14,7 +14,7 @@ namespace electron {
NSArray* ListValueToNSArray(const base::Value::List& value) { NSArray* ListValueToNSArray(const base::Value::List& value) {
std::string json; std::string json;
if (!base::JSONWriter::Write(base::Value(value.Clone()), &json)) if (!base::JSONWriter::Write(base::ValueView{value}, &json))
return nil; return nil;
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()]; NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
id obj = [NSJSONSerialization JSONObjectWithData:jsonData id obj = [NSJSONSerialization JSONObjectWithData:jsonData
@ -57,7 +57,7 @@ base::Value::List NSArrayToValue(NSArray* arr) {
NSDictionary* DictionaryValueToNSDictionary(const base::Value::Dict& value) { NSDictionary* DictionaryValueToNSDictionary(const base::Value::Dict& value) {
std::string json; std::string json;
if (!base::JSONWriter::Write(base::Value(value.Clone()), &json)) if (!base::JSONWriter::Write(base::ValueView{value}, &json))
return nil; return nil;
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()]; NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
id obj = [NSJSONSerialization JSONObjectWithData:jsonData id obj = [NSJSONSerialization JSONObjectWithData:jsonData

View file

@ -206,7 +206,7 @@ void UsbChooserContext::RevokeObjectPermissionInternal(
v8::HandleScope scope(isolate); v8::HandleScope scope(isolate);
gin_helper::Dictionary details = gin_helper::Dictionary details =
gin_helper::Dictionary::CreateEmpty(isolate); gin_helper::Dictionary::CreateEmpty(isolate);
details.Set("device", object.Clone()); details.Set("device", object);
details.Set("origin", origin.Serialize()); details.Set("origin", origin.Serialize());
session->Emit("usb-device-revoked", details); session->Emit("usb-device-revoked", details);
} }

View file

@ -22,7 +22,7 @@ v8::Local<v8::Value> Converter<const extensions::Extension*>::ToV8(
dict.Set("path", extension->path()); dict.Set("path", extension->path());
dict.Set("url", extension->url()); dict.Set("url", extension->url());
dict.Set("version", extension->VersionString()); dict.Set("version", extension->VersionString());
dict.Set("manifest", extension->manifest()->value()->Clone()); dict.Set("manifest", *extension->manifest()->value());
return gin::ConvertToV8(isolate, dict); return gin::ConvertToV8(isolate, dict);
} }

View file

@ -25,14 +25,6 @@ bool Converter<base::Value::Dict>::FromV8(v8::Isolate* isolate,
} }
} }
v8::Local<v8::Value> Converter<base::Value::Dict>::ToV8(
v8::Isolate* isolate,
const base::Value::Dict& val) {
base::Value value(val.Clone());
return content::V8ValueConverter::Create()->ToV8Value(
value, isolate->GetCurrentContext());
}
bool Converter<base::Value>::FromV8(v8::Isolate* isolate, bool Converter<base::Value>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val, v8::Local<v8::Value> val,
base::Value* out) { base::Value* out) {
@ -47,8 +39,9 @@ bool Converter<base::Value>::FromV8(v8::Isolate* isolate,
} }
} }
v8::Local<v8::Value> Converter<base::Value>::ToV8(v8::Isolate* isolate, v8::Local<v8::Value> Converter<base::ValueView>::ToV8(
const base::Value& val) { v8::Isolate* isolate,
const base::ValueView val) {
return content::V8ValueConverter::Create()->ToV8Value( return content::V8ValueConverter::Create()->ToV8Value(
val, isolate->GetCurrentContext()); val, isolate->GetCurrentContext());
} }
@ -67,12 +60,4 @@ bool Converter<base::Value::List>::FromV8(v8::Isolate* isolate,
} }
} }
v8::Local<v8::Value> Converter<base::Value::List>::ToV8(
v8::Isolate* isolate,
const base::Value::List& val) {
base::Value value(val.Clone());
return content::V8ValueConverter::Create()->ToV8Value(
value, isolate->GetCurrentContext());
}
} // namespace gin } // namespace gin

View file

@ -10,13 +10,21 @@
namespace gin { namespace gin {
template <>
struct Converter<base::ValueView> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::ValueView val);
};
template <> template <>
struct Converter<base::Value::Dict> { struct Converter<base::Value::Dict> {
static bool FromV8(v8::Isolate* isolate, static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val, v8::Local<v8::Value> val,
base::Value::Dict* out); base::Value::Dict* out);
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Value::Dict& val); const base::Value::Dict& val) {
return gin::ConvertToV8(isolate, base::ValueView{val});
}
}; };
template <> template <>
@ -25,7 +33,9 @@ struct Converter<base::Value> {
v8::Local<v8::Value> val, v8::Local<v8::Value> val,
base::Value* out); base::Value* out);
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Value& val); const base::Value& val) {
return gin::ConvertToV8(isolate, base::ValueView{val});
}
}; };
template <> template <>
@ -34,7 +44,9 @@ struct Converter<base::Value::List> {
v8::Local<v8::Value> val, v8::Local<v8::Value> val,
base::Value::List* out); base::Value::List* out);
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Value::List& val); const base::Value::List& val) {
return gin::ConvertToV8(isolate, base::ValueView{val});
}
}; };
} // namespace gin } // namespace gin