zefchain / serde-reflection

Rust libraries and tools to help with interoperability and testing of serialization formats based on Serde.
Apache License 2.0
139 stars 26 forks source link

feat: rework typescript generator & add suite #46

Open MrFoxPro opened 2 months ago

MrFoxPro commented 2 months ago

Closes https://github.com/zefchain/serde-reflection/issues/43 Probably needs more tests Checkout suite/typescript/ts/test.ts and suite/typescript/readme.md for example usage and additional information You can fix and adjust as you want if you have time

Benchamrks:

Encode

┌─────────┬───────────────────────────────┬─────────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name                     │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples │
├─────────┼───────────────────────────────┼─────────────┼────────────────────┼──────────┼─────────┤
│ 0       │ 'serdegen-bincode:encode'     │ '717,309'   │ 1394.0992784144942 │ '±0.31%' │ 1075964 │
│ 1       │ 'JSON:encode'                 │ '384,683'   │ 2599.5422867295706 │ '±0.05%' │ 577025  │
│ 2       │ 'protobuf-js-ts-proto:encode' │ '1,049,439' │ 952.8891828025206  │ '±0.50%' │ 1574160 │
└─────────┴───────────────────────────────┴─────────────┴────────────────────┴──────────┴─────────┘

Decode

┌─────────┬───────────────────────────────┬───────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name                     │ ops/sec   │ Average Time (ns)  │ Margin   │ Samples │
├─────────┼───────────────────────────────┼───────────┼────────────────────┼──────────┼─────────┤
│ 0       │ 'serdegen-bincode:decode'     │ '894,259' │ 1118.2439247680838 │ '±0.24%' │ 1341389 │
│ 1       │ 'JSON:decode'                 │ '460,448' │ 2171.7932150326965 │ '±0.14%' │ 690674  │
│ 2       │ 'protobuf-js-ts-proto:decode' │ '837,149' │ 1194.5301794026077 │ '±0.18%' │ 1255724 │
└─────────┴───────────────────────────────┴───────────┴────────────────────┴──────────┴─────────┘

Need investigation how to improve encoding. It's worth to look into protobuf-js implementation

ma2bd commented 2 months ago

Thanks @MrFoxPro for your work. It looks like the lint error is on main but the test compilation error is yours.

MrFoxPro commented 2 months ago

Thanks @MrFoxPro for your work. It looks like the lint error is on main but the test compilation error is yours.

Yep, PR requires some polishing

MrFoxPro commented 2 months ago

I thought one way to speedup encoding is to preallocate chunk for fixed-size values of type by recursive lookup of it in code generator and adding flag to "noAlloc" in writeX functions

MrFoxPro commented 2 months ago

I decided to change TypeScript implementation. Now it looks like this:

enum MultiEnum {
    VariantA(i32),
    VariantB(String),
    VariantC { x: u8, y: f64 },
    UnitVariant,
}
struct ComplexStruct {
    inner: SimpleStruct,
    flag: bool,
    items: Vec<MultiEnum>,
    unit: UnitStruct,
    newtype: NewtypeStruct,
    tuple: TupleStruct,
    tupple_inline: (String, i32),
    map: HashMap<i32, i64>
}

->

export const ComplexStruct_obj: Registry.ComplexStruct = {
    inner: { a: 42, b: "Hello" },
    flag: true,
    items: [
        { $: "variant_a", $0: 10 },
        { $: "variant_b", $0: "World" }
    ],
    unit: null,
    newtype: 99,
    tuple: { $0: 123, $1: 45.67, $2: "Test" },
    tupple_inline: { $0: "SomeString", $1: 777 },
    map: new Map().set(3, 7n)
}