Closed Bug 1860460 Opened 1 year ago Closed 1 year ago

Add non-GC JSON stringify API ?

Categories

(Core :: JavaScript Engine, task, P3)

task

Tracking

()

RESOLVED WONTFIX

People

(Reporter: arai, Unassigned)

References

Details

(derived from bug 1858692 comment #19 and some)

There are some consumers of JsonCpp in tree, which is for stringify-ing some data, such as principal.
one of the requirement there is "not using JSContext" (same as bug 1858803 for parsing API)

JsonCpp's stringify API requires creating a temporary object and call Json::writeString with it,
and it's hitting perf issue (bug 1795312)

https://searchfox.org/mozilla-central/rev/3389de86c4daf28fe402fcfe7d99e8646d41f302/caps/BasePrincipal.cpp#382,386-387,397

nsresult BasePrincipal::ToJSON(nsACString& aJSON) {
...
  Json::Value root = Json::objectValue;
  nsresult rv = ToJSON(root);
...
  std::string result = Json::writeString(*sJSONBuilderForPrincipals, root);

For principal case, the input data is very simple, and it's not necessary to create temporary data if the JSON.stringify operation can be performed with the following style:

JSONWriter writer(...);

writer.startObject();
writer.propertyName("foo");
writer.stringValue("bar");
...
writer.endObject();

auto resultJSONString = writer.finish();

This can be a set of reverse operations of the API added by bug 1858803 patch.
the new API just need to handle buffer allocation, concatenation, some state for object/array, and serialization of number and strings.

Blocks: sm-meta

actually, we have https://searchfox.org/mozilla-central/source/mfbt/JSONWriter.h , which sounds like already implements the above.
maybe we could just utilize it, instead of implementing the same thing in JSAPI or wrapping it with JSAPI.

if the performance is more important, we could tweak it not to put indentation.

Summary: Add non-GC JSON stringify API → Add non-GC JSON stringify API ?

apparently mfbt/JSONWriter.h is sufficient for the purpose, e.g. in bug 1861787

Status: NEW → RESOLVED
Closed: 1 year ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.