feat(wasm): JSON text export of deep values - #1087
Open
zxch3n wants to merge 1 commit into
Open
Conversation
zxch3n
force-pushed
the
feat/wasm-json-text-export
branch
from
September 4, 2026 04:17
c2a4656 to
a22454b
Compare
This was referenced Sep 4, 2026
Merged
zxch3n
force-pushed
the
feat/wasm-json-text-export
branch
from
September 4, 2026 11:22
a22454b to
1430e55
Compare
Contributor
WASM Size Report
|
zxch3n
force-pushed
the
feat/wasm-json-text-export
branch
from
September 4, 2026 12:17
1430e55 to
80ac32f
Compare
Add getDeepValueJson(): string on LoroDoc and every container class —
serde_json serialization of the deep value in one WASM call, same content
as JSON.stringify(x.toJSON()) — and getDeepValueJsonWithIds():
{ json, cids } where cids lists container ids in pre-order DFS of the
serialized tree so a consumer can re-attach ids in a single JS walk.
The (json, cids) pair is produced by converting the with-id deep value to
a serde_json::Value and stripping { cid, value } nodes in one pass, so the
cids order always matches the key/item order a JS consumer sees after
JSON.parse, regardless of serde_json's preserve_order feature.
Benchmark on a ~70k-container doc (Map 15,632 / List 9,956 / Text 44,463,
4.1 MB JSON), release build: getDeepValueWithID 137.0 ms vs
getDeepValueJson()+JSON.parse 56.3 ms (2.4x). The 5x target is not
reachable from the JS side: profiling shows the Rust-side deep-value walk
and serialization dominate (getDeepValueJson alone is 46 ms; JSON.parse
of 4.1 MB is ~5 ms), not the boundary crossing.
Wasm size (dev build, with debug info): +524 KB (+0.52%).
zxch3n
force-pushed
the
feat/wasm-json-text-export
branch
from
September 4, 2026 16:38
80ac32f to
55093ca
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack: 4/6 — merge order: #1093 → #1085 → #1086 → #1087 → #1090 → #1091
Summary
Stacked on #1086 (which stacks on #1085); merge in order, GitHub retargets automatically.
Adds two bulk-read APIs on
LoroDocand every container class (LoroMap/LoroList/LoroMovableList/LoroTree/LoroText/LoroCounter):getDeepValueJson(): string— serde_json serialization of the deep value in ONE wasm call; same content asJSON.stringify(x.toJSON()).getDeepValueJsonWithIds(): { json: string, cids: ContainerID[] }—jsonis the deep value without ids;cidslists container ids in pre-order DFS of the serialized tree, so a consumer (e.g. loro-mirror) can re-attach ids in a single JS walk to reconstruct thegetDeepValueWithID()shape.The
(json, cids)pair is produced by converting the with-id deep value to aserde_json::Valueand stripping{ cid, value }nodes in one pass, socidsorder always matches the key/item order afterJSON.parse, regardless of serde_json'spreserve_orderfeature.Benchmark (generated doc: 70,051 containers — Map 15,632 / List 9,956 / Text 44,463; 4.1 MB JSON;
pnpm bench-deep-value-json)Release build, median of 5 rounds:
toJSON()getDeepValueWithID()getDeepValueJson()getDeepValueJson()+JSON.parsegetDeepValueJsonWithIds()+ parse + re-attach walkThe ≥5x target is not reachable: measured 2.4x end-to-end (
getDeepValueJson()+JSON.parsevsgetDeepValueWithID()). CPU profiling shows the bottleneck is the Rust-side deep-value walk + serialization (getDeepValueJsonalone is 46 ms), not the boundary crossing —JSON.parseof 4.1 MB costs only ~5 ms. The JsValue structured-clone conversion accounts for ~75–80 ms ofgetDeepValueWithID()'s 137 ms. Dev (unoptimized wasm) build numbers: 370 ms → 280 ms (1.3x), dominated by dlmalloc/debug checks. Further wins would require reducing Rust-side materialization cost, not JS-side changes.Caveats (documented in the changeset, doc comments, and
context/wasm-bulk-read.md):metamaps serialize as plain deep values (matchinggetDeepValueWithID()), so meta container ids do not appear incids.cid+valuewherecidis a valid container id string is indistinguishable from a container node (inherent to the format).Tests
crates/loro-internal/tests/deep_value_json.rs(5 tests: content equality with plain deep value, exact pre-order cids, per-container variants, empty doc, detached errors).tests/deep_value.test.tsextended (17 tests):JSON.parse(getDeepValueJson())deep-equalstoJSON()for doc + all 6 container types; re-attach walk reconstructsgetDeepValueWithID()exactly.tsc --noEmitclean;cargo test -p loro-internal/-p loropass.Wasm binary size
Dev build with debug info: +524 KB (+0.52%) vs the parent branch.