Skip to content

Commit 4ab2bf8

Browse files
committed
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
1 parent f98ce05 commit 4ab2bf8

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

dom/base/nsContentUtils.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10852,12 +10852,8 @@ bool nsContentUtils::StringifyJSON(JSContext* aCx, JS::Handle<JS::Value> aValue,
1085210852
case UndefinedIsNullStringLiteral: {
1085310853
aOutStr.Truncate();
1085410854
JS::Rooted<JS::Value> value(aCx, aValue);
10855-
nsAutoString serializedValue;
10856-
NS_ENSURE_TRUE(JS_Stringify(aCx, &value, nullptr, JS::NullHandleValue,
10857-
JSONCreator, &serializedValue),
10858-
false);
10859-
aOutStr = serializedValue;
10860-
return true;
10855+
return JS_Stringify(aCx, &value, nullptr, JS::NullHandleValue,
10856+
JSONCreator, &aOutStr);
1086110857
}
1086210858
case UndefinedIsVoidString: {
1086310859
aOutStr.SetIsVoid(true);

dom/system/IOUtils.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,11 +574,6 @@ already_AddRefed<Promise> IOUtils::WriteUTF8(GlobalObject& aGlobal,
574574
});
575575
}
576576

577-
static bool AppendJsonAsUtf8(const char16_t* aData, uint32_t aLen, void* aStr) {
578-
nsCString* str = static_cast<nsCString*>(aStr);
579-
return AppendUTF16toUTF8(Span<const char16_t>(aData, aLen), *str, fallible);
580-
}
581-
582577
/* static */
583578
already_AddRefed<Promise> IOUtils::WriteJSON(GlobalObject& aGlobal,
584579
const nsAString& aPath,
@@ -605,10 +600,9 @@ already_AddRefed<Promise> IOUtils::WriteJSON(GlobalObject& aGlobal,
605600

606601
JSContext* cx = aGlobal.Context();
607602
JS::Rooted<JS::Value> rootedValue(cx, aValue);
608-
nsCString utf8Str;
609-
610-
if (!JS_Stringify(cx, &rootedValue, nullptr, JS::NullHandleValue,
611-
AppendJsonAsUtf8, &utf8Str)) {
603+
nsString string;
604+
if (!nsContentUtils::StringifyJSON(cx, aValue, string,
605+
UndefinedIsNullStringLiteral)) {
612606
JS::Rooted<JS::Value> exn(cx, JS::UndefinedValue());
613607
if (JS_GetPendingException(cx, &exn)) {
614608
JS_ClearPendingException(cx);
@@ -624,8 +618,9 @@ already_AddRefed<Promise> IOUtils::WriteJSON(GlobalObject& aGlobal,
624618

625619
DispatchAndResolve<uint32_t>(
626620
state->mEventQueue, promise,
627-
[file = std::move(file), utf8Str = std::move(utf8Str),
621+
[file = std::move(file), string = std::move(string),
628622
opts = opts.unwrap()]() {
623+
NS_ConvertUTF16toUTF8 utf8Str(string);
629624
return WriteSync(file, AsBytes(Span(utf8Str)), opts);
630625
});
631626
});

0 commit comments

Comments
 (0)