Bug 1866239 - Do the UTF16->UTF8 conversion in IOUtils in the background thread. r=smaug

See the profile in the blocked bug, this is about half of the time spent
there. It doesn't help with the json serialization being slow (maybe
huge object graph?), but should be an easy win.

Differential Revision: https://phabricator.services.mozilla.com/D194476
This commit is contained in:
Emilio Cobos Álvarez 2023-11-23 13:03:57 +00:00
Родитель f98ce05e32
Коммит 4ab2bf820c
2 изменённых файлов: 7 добавлений и 16 удалений

Просмотреть файл

@ -10852,12 +10852,8 @@ bool nsContentUtils::StringifyJSON(JSContext* aCx, JS::Handle<JS::Value> aValue,
case UndefinedIsNullStringLiteral: {
aOutStr.Truncate();
JS::Rooted<JS::Value> value(aCx, aValue);
nsAutoString serializedValue;
NS_ENSURE_TRUE(JS_Stringify(aCx, &value, nullptr, JS::NullHandleValue,
JSONCreator, &serializedValue),
false);
aOutStr = serializedValue;
return true;
return JS_Stringify(aCx, &value, nullptr, JS::NullHandleValue,
JSONCreator, &aOutStr);
}
case UndefinedIsVoidString: {
aOutStr.SetIsVoid(true);

Просмотреть файл

@ -574,11 +574,6 @@ already_AddRefed<Promise> IOUtils::WriteUTF8(GlobalObject& aGlobal,
});
}
static bool AppendJsonAsUtf8(const char16_t* aData, uint32_t aLen, void* aStr) {
nsCString* str = static_cast<nsCString*>(aStr);
return AppendUTF16toUTF8(Span<const char16_t>(aData, aLen), *str, fallible);
}
/* static */
already_AddRefed<Promise> IOUtils::WriteJSON(GlobalObject& aGlobal,
const nsAString& aPath,
@ -605,10 +600,9 @@ already_AddRefed<Promise> IOUtils::WriteJSON(GlobalObject& aGlobal,
JSContext* cx = aGlobal.Context();
JS::Rooted<JS::Value> rootedValue(cx, aValue);
nsCString utf8Str;
if (!JS_Stringify(cx, &rootedValue, nullptr, JS::NullHandleValue,
AppendJsonAsUtf8, &utf8Str)) {
nsString string;
if (!nsContentUtils::StringifyJSON(cx, aValue, string,
UndefinedIsNullStringLiteral)) {
JS::Rooted<JS::Value> exn(cx, JS::UndefinedValue());
if (JS_GetPendingException(cx, &exn)) {
JS_ClearPendingException(cx);
@ -624,8 +618,9 @@ already_AddRefed<Promise> IOUtils::WriteJSON(GlobalObject& aGlobal,
DispatchAndResolve<uint32_t>(
state->mEventQueue, promise,
[file = std::move(file), utf8Str = std::move(utf8Str),
[file = std::move(file), string = std::move(string),
opts = opts.unwrap()]() {
NS_ConvertUTF16toUTF8 utf8Str(string);
return WriteSync(file, AsBytes(Span(utf8Str)), opts);
});
});