Skip to content

Commit 5e4f275

Browse files
authored
refactor(javascript): move serialization runtime state into contexts (#3549)
## Why? - Align the JavaScript runtime ownership model with context-based operation state instead of ambient `Fory` state. - Remove internal `Fory` retention from serializers and codegen while preserving the existing wire format and early-bound closure performance model. ## What does this PR do? - Introduces reusable `WriteContext` and `ReadContext` objects and moves binary reader/writer, ref tracking, meta string handling, and type-meta state under those contexts. - Keeps `Fory` as the top-level facade, renames `registerSerializer(...)` to `register(...)`, removes `serializeVolatile(...)`, and exports `WriteContext` / `ReadContext` for custom serializers. - Refactors `TypeResolver`, `TypeInfo`, codegen, and generated serializers to stop retaining `Fory` and instead bind hot reader/writer/resolver locals from resolver-rooted reusable contexts. - Splits legacy `ReferenceResolver` / `MetaStringResolver` into directional helpers and folds `TypeMetaResolver` logic into the new contexts. - Updates JavaScript docs, benchmark scripts, and tests to the new context-first custom serializer API without changing serialization behavior or wire format. - Normalizes the new JavaScript context/codegen files to satisfy the repository code style checks. ## Related issues - None. ## AI Contribution Checklist - [ ] Substantial AI assistance was used in this PR: `yes` / `no` - [ ] If `yes`, I included a completed [AI Contribution Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs) in this PR description and the required `AI Usage Disclosure`. - [ ] If `yes`, my PR description includes the required `ai_review` summary and screenshot evidence of the final clean AI review results from both fresh reviewers on the current PR diff or current HEAD after the latest code changes. ## Does this PR introduce any user-facing change? - [x] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? JavaScript public API changes include `fory.register(...)`, removal of `serializeVolatile(...)`, and context-first custom serializer signatures. ## Benchmark Preserves the existing early-bound closure strategy for generated serializers; no wire-format change is intended.
1 parent a3095c4 commit 5e4f275

38 files changed

Lines changed: 1342 additions & 1122 deletions

javascript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const typeInfo = Type.struct('example.foo', {
2121
foo: Type.string(),
2222
});
2323
const fory = new Fory({ hps });
24-
const { serialize, deserialize } = fory.registerSerializer(typeInfo);
24+
const { serialize, deserialize } = fory.register(typeInfo);
2525
const input = serialize({ foo: 'hello fory' });
2626
const result = deserialize(input);
2727
console.log(result);

javascript/benchmark/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const sample = {
171171

172172

173173
const typeinfo = utils.data2TypeInfo(sample, "fory.test.foo");
174-
const { serialize, deserialize, serializeVolatile } = fory.registerSerializer(typeinfo);
174+
const { serialize, deserialize } = fory.register(typeinfo);
175175

176176
const foryAb = serialize(sample);
177177
const sampleJson = JSON.stringify(sample);
@@ -228,7 +228,7 @@ async function start() {
228228
var suite = new Benchmark.Suite();
229229
suite
230230
.add("fory", function () {
231-
serializeVolatile(sample).dispose();
231+
serialize(sample);
232232
})
233233
.add("json", function () {
234234
JSON.stringify(sample);

javascript/benchmark/map.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ const Type = Fory.Type;
3232

3333

3434

35-
const { serialize: serialize1, deserialize: deserialize1, serializeVolatile: serializeVolatile1 } = fory.registerSerializer(Type.struct("any", {
35+
const { serialize: serialize1, deserialize: deserialize1 } = fory.register(Type.struct("any", {
3636
f1: Type.map(Type.any(), Type.any()),
3737
f2: Type.map(Type.any(), Type.any())
3838
}));
3939

40-
const { serialize: serialize2, deserialize: deserialize2, serializeVolatile: serializeVolatile2 } = fory.registerSerializer(Type.struct("specific", {
40+
const { serialize: serialize2, deserialize: deserialize2 } = fory.register(Type.struct("specific", {
4141
f1: Type.map(Type.string(), Type.string()),
4242
f2: Type.map(Type.int32(), Type.string())
4343
}));
@@ -58,13 +58,13 @@ async function start() {
5858
var suite = new Benchmark.Suite();
5959
suite
6060
.add("any serialize", function () {
61-
serializeVolatile1(sample).dispose()
61+
serialize1(sample)
6262
})
6363
.add("any deserialize", function () {
6464
deserialize1(foryAb1)
6565
})
6666
.add("jit serialize", function () {
67-
serializeVolatile2(sample).dispose()
67+
serialize2(sample)
6868
})
6969
.add("jit deserialize", function () {
7070
deserialize2(foryAb2)

javascript/packages/core/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Fory from "./lib/fory";
2727
import { BinaryReader } from "./lib/reader";
2828
import { BinaryWriter } from "./lib/writer";
2929
import { BFloat16, BFloat16Array } from "./lib/bfloat16";
30+
import { ReadContext, WriteContext } from "./lib/context";
3031

3132
export {
3233
Serializer,
@@ -38,6 +39,8 @@ export {
3839
BinaryReader,
3940
BFloat16,
4041
BFloat16Array,
42+
ReadContext,
43+
WriteContext,
4144
};
4245

4346
export default Fory;

0 commit comments

Comments
 (0)