diff --git a/packages/tests/src/benchmark/Benchmark.bs.mjs b/packages/tests/src/benchmark/Benchmark.bs.mjs index d391e5b3..c31d4441 100644 --- a/packages/tests/src/benchmark/Benchmark.bs.mjs +++ b/packages/tests/src/benchmark/Benchmark.bs.mjs @@ -1,6 +1,7 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as Benchmark from "benchmark"; +import * as Core__Array from "@rescript/core/src/Core__Array.bs.mjs"; import * as S$RescriptSchema from "rescript-schema/src/S.bs.mjs"; function addWithPrepare(suite, name, fn) { @@ -74,47 +75,121 @@ S$RescriptSchema.setGlobalConfig({ disableNanNumberCheck: true }); +var schema = S$RescriptSchema.recursive(function (schema) { + return S$RescriptSchema.union([ + S$RescriptSchema.object(function (s) { + s.tag("type", "A"); + return { + TAG: "A", + _0: s.f("nested", S$RescriptSchema.array(schema)) + }; + }), + S$RescriptSchema.literal("B"), + S$RescriptSchema.literal("C"), + S$RescriptSchema.literal("D"), + S$RescriptSchema.literal("E"), + S$RescriptSchema.literal("F"), + S$RescriptSchema.literal("G"), + S$RescriptSchema.literal("H"), + S$RescriptSchema.literal("I"), + S$RescriptSchema.literal("J"), + S$RescriptSchema.literal("K"), + S$RescriptSchema.literal("L"), + S$RescriptSchema.literal("M"), + S$RescriptSchema.literal("N"), + S$RescriptSchema.literal("O"), + S$RescriptSchema.literal("P"), + S$RescriptSchema.literal("Q"), + S$RescriptSchema.literal("R"), + S$RescriptSchema.literal("S"), + S$RescriptSchema.literal("T"), + S$RescriptSchema.literal("U"), + S$RescriptSchema.literal("V"), + S$RescriptSchema.literal("W"), + S$RescriptSchema.literal("X"), + S$RescriptSchema.literal("Y"), + S$RescriptSchema.object(function (s) { + s.tag("type", "Z"); + return { + TAG: "Z", + _0: s.f("nested", S$RescriptSchema.array(schema)) + }; + }) + ]); + }); + +var testData1 = { + TAG: "Z", + _0: Core__Array.make(2, { + TAG: "Z", + _0: Core__Array.make(2, { + TAG: "Z", + _0: Core__Array.make(2, "Y") + }) + }) +}; + +Core__Array.make(2, { + TAG: "A", + _0: Core__Array.make(2, { + TAG: "A", + _0: Core__Array.make(2, "B") + }) + }); + +function test() { + console.time("testData1 serialize"); + var json = S$RescriptSchema.serializeOrRaiseWith(testData1, schema); + console.timeEnd("testData1 serialize"); + console.time("testData1 parse"); + S$RescriptSchema.parseOrRaiseWith(json, schema); + console.timeEnd("testData1 parse"); + console.log(schema.parseOrThrow.toString()); +} + +test(); + var data = makeTestObject(); console.time("makeAdvancedObjectSchema"); -var schema = makeAdvancedObjectSchema(); +var schema$1 = makeAdvancedObjectSchema(); console.timeEnd("makeAdvancedObjectSchema"); console.time("parseAnyWith: 1"); -S$RescriptSchema.parseAnyWith(data, schema); +S$RescriptSchema.parseAnyWith(data, schema$1); console.timeEnd("parseAnyWith: 1"); console.time("parseAnyWith: 2"); -S$RescriptSchema.parseAnyWith(data, schema); +S$RescriptSchema.parseAnyWith(data, schema$1); console.timeEnd("parseAnyWith: 2"); console.time("parseAnyWith: 3"); -S$RescriptSchema.parseAnyWith(data, schema); +S$RescriptSchema.parseAnyWith(data, schema$1); console.timeEnd("parseAnyWith: 3"); console.time("serializeWith: 1"); -S$RescriptSchema.serializeWith(data, schema); +S$RescriptSchema.serializeWith(data, schema$1); console.timeEnd("serializeWith: 1"); console.time("serializeWith: 2"); -S$RescriptSchema.serializeWith(data, schema); +S$RescriptSchema.serializeWith(data, schema$1); console.timeEnd("serializeWith: 2"); console.time("serializeWith: 3"); -S$RescriptSchema.serializeWith(data, schema); +S$RescriptSchema.serializeWith(data, schema$1); console.timeEnd("serializeWith: 3"); diff --git a/packages/tests/src/benchmark/Benchmark.res b/packages/tests/src/benchmark/Benchmark.res index 4a8fe6f2..f9530e6b 100644 --- a/packages/tests/src/benchmark/Benchmark.res +++ b/packages/tests/src/benchmark/Benchmark.res @@ -103,6 +103,104 @@ S.setGlobalConfig({ disableNanNumberCheck: true, }) +// Reported in https://gist.github.com/cknitt/4ac6813a3f3bc907187105e01a4324ca +module CrazyUnion = { + type rec test = + | A(array) + | B + | C + | D + | E + | F + | G + | H + | I + | J + | K + | L + | M + | N + | O + | P + | Q + | R + | S + | T + | U + | V + | W + | X + | Y + | Z(array) + + let schema = S.recursive(schema => + S.union([ + S.object(s => { + s.tag("type", "A") + A(s.field("nested", S.array(schema))) + }), + S.literal(B), + S.literal(C), + S.literal(D), + S.literal(E), + S.literal(F), + S.literal(G), + S.literal(H), + S.literal(I), + S.literal(J), + S.literal(K), + S.literal(L), + S.literal(M), + S.literal(N), + S.literal(O), + S.literal(P), + S.literal(Q), + S.literal(R), + S.literal(S), + S.literal(T), + S.literal(U), + S.literal(V), + S.literal(W), + S.literal(X), + S.literal(Y), + S.object(s => { + s.tag("type", "Z") + Z(s.field("nested", S.array(schema))) + }), + ]) + ) + + let testData1 = Z(Array.make(~length=2, Z(Array.make(~length=2, Z(Array.make(~length=2, Y)))))) + + let _testData2 = A(Array.make(~length=2, A(Array.make(~length=2, A(Array.make(~length=2, B)))))) + + let test = () => { + Console.time("testData1 serialize") + let json = S.serializeOrRaiseWith(testData1, schema) + Console.timeEnd("testData1 serialize") + + Console.time("testData1 parse") + let _ = S.parseOrRaiseWith(json, schema) + Console.timeEnd("testData1 parse") + + // Console.time("testData2 serialize") + // let json = S.serializeOrRaiseWith(testData2, schema) + // Console.timeEnd("testData2 serialize") + + // Console.time("testData2 parse") + // let _ = S.parseOrRaiseWith(json, schema) + // Console.timeEnd("testData2 parse") + + Js.log((schema->Obj.magic)["parseOrThrow"]["toString"]()) + } +} + +// testData1 serialize: 5.414s +// testData1 parse: 5.519s +// testData2 serialize: 70.864ms +// testData2 parse: 70.967ms +CrazyUnion.test() + let data = makeTestObject() Console.time("makeAdvancedObjectSchema") let schema = makeAdvancedObjectSchema() diff --git a/packages/tests/src/core/S_parseAnyAsyncInStepsWith_deprecatedTest.bs.mjs b/packages/tests/src/core/S_parseAnyAsyncInStepsWith_deprecatedTest.bs.mjs new file mode 100644 index 00000000..d856702b --- /dev/null +++ b/packages/tests/src/core/S_parseAnyAsyncInStepsWith_deprecatedTest.bs.mjs @@ -0,0 +1,2 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ diff --git a/packages/tests/src/core/S_union_test.res b/packages/tests/src/core/S_union_test.res index 3150d808..c1a3d86c 100644 --- a/packages/tests/src/core/S_union_test.res +++ b/packages/tests/src/core/S_union_test.res @@ -500,3 +500,86 @@ module CknittelBugReport = { ) }) } + +// Reported in https://gist.github.com/cknitt/4ac6813a3f3bc907187105e01a4324ca +module CrazyUnion = { + type rec test = + | A(array) + | B + | C + | D + | E + | F + | G + | H + | I + | J + | K + | L + | M + | N + | O + | P + | Q + | R + | S + | T + | U + | V + | W + | X + | Y + | Z(array) + + let schema = S.recursive(schema => + S.union([ + S.object(s => { + s.tag("type", "A") + A(s.field("nested", S.array(schema))) + }), + S.literal(B), + S.literal(C), + S.literal(D), + S.literal(E), + S.literal(F), + S.literal(G), + S.literal(H), + S.literal(I), + S.literal(J), + S.literal(K), + S.literal(L), + S.literal(M), + S.literal(N), + S.literal(O), + S.literal(P), + S.literal(Q), + S.literal(R), + S.literal(S), + S.literal(T), + S.literal(U), + S.literal(V), + S.literal(W), + S.literal(X), + S.literal(Y), + S.object(s => { + s.tag("type", "Z") + Z(s.field("nested", S.array(schema))) + }), + ]) + ) + + test("Compiled code snapshot of crazy union", t => { + S.setGlobalConfig({}) + t->U.assertCompiledCode( + ~schema, + ~op=#Parse, + `i=>{let r0=i=>{let v6;try{if(!i||i.constructor!==Object){e[0](i)}let v0=i["type"],v1=i["nested"],v5=[];v0==="A"||e[1](v0);if(!Array.isArray(v1)){e[2](v1)}for(let v2=0;v2U.assertCompiledCode( + ~schema, + ~op=#Serialize, + `i=>{let r0=i=>{let v5,v6,v12;try{let v0=i["_0"],v4=[];if(i["TAG"]!==e[0]){e[1](i["TAG"])}for(let v1=0;v1): jsResult<'value> => { module InternalError = { %%raw(` + // let index = 0; class RescriptSchemaError extends Error { constructor(code, operation, path) { + // console.log(index) + // index = index + 1; super(); this.operation = operation; this.code = code; diff --git a/test.js b/test.js index 952a7610..1a2f24ef 100644 --- a/test.js +++ b/test.js @@ -1,14 +1,226 @@ (i) => { - return e[0](i).then((v0) => { - return e[1](v0) - .then((v1) => { - if (typeof v1 !== "string") { - e[2](v1); + let r0 = (i) => { + let v5, v6, v12; + try { + let v0 = i["_0"], + v4 = []; + if (i["TAG"] !== e[0]) { + e[1](i["TAG"]); + } + for (let v1 = 0; v1 < v0.length; ++v1) { + let v3; + try { + v3 = r0(v0[v1]); + } catch (v2) { + if (v2 && v2.s === s) { + v2.path = '["_0"]' + '["' + v1 + '"]' + v2.path; + } + throw v2; } - return v1; - }) - .then((v2) => { - return e[3](v2); - }); - }); + v4.push(v3); + } + v5 = { type: e[2], nested: v4 }; + if (!v5 || v5.constructor !== Object) { + e[3](v5); + } + v6 = v5; + } catch (e0) { + try { + i === "B" || e[4](i); + v6 = i; + } catch (e1) { + try { + i === "C" || e[5](i); + v6 = i; + } catch (e2) { + try { + i === "D" || e[6](i); + v6 = i; + } catch (e3) { + try { + i === "E" || e[7](i); + v6 = i; + } catch (e4) { + try { + i === "F" || e[8](i); + v6 = i; + } catch (e5) { + try { + i === "G" || e[9](i); + v6 = i; + } catch (e6) { + try { + i === "H" || e[10](i); + v6 = i; + } catch (e7) { + try { + i === "I" || e[11](i); + v6 = i; + } catch (e8) { + try { + i === "J" || e[12](i); + v6 = i; + } catch (e9) { + try { + i === "K" || e[13](i); + v6 = i; + } catch (e10) { + try { + i === "L" || e[14](i); + v6 = i; + } catch (e11) { + try { + i === "M" || e[15](i); + v6 = i; + } catch (e12) { + try { + i === "N" || e[16](i); + v6 = i; + } catch (e13) { + try { + i === "O" || e[17](i); + v6 = i; + } catch (e14) { + try { + i === "P" || e[18](i); + v6 = i; + } catch (e15) { + try { + i === "Q" || e[19](i); + v6 = i; + } catch (e16) { + try { + i === "R" || e[20](i); + v6 = i; + } catch (e17) { + try { + i === "S" || e[21](i); + v6 = i; + } catch (e18) { + try { + i === "T" || e[22](i); + v6 = i; + } catch (e19) { + try { + i === "U" || e[23](i); + v6 = i; + } catch (e20) { + try { + i === "V" || e[24](i); + v6 = i; + } catch (e21) { + try { + i === "W" || e[25](i); + v6 = i; + } catch (e22) { + try { + i === "X" || e[26](i); + v6 = i; + } catch (e23) { + try { + i === "Y" || e[27](i); + v6 = i; + } catch (e24) { + try { + let v7 = i["_0"], + v11 = []; + if ( + i["TAG"] !== e[28] + ) { + e[29](i["TAG"]); + } + for ( + let v8 = 0; + v8 < v7.length; + ++v8 + ) { + let v10; + try { + v10 = r0(v7[v8]); + } catch (v9) { + if ( + v9 && + v9.s === s + ) { + v9.path = + '["_0"]' + + '["' + + v8 + + '"]' + + v9.path; + } + throw v9; + } + v11.push(v10); + } + v12 = { + type: e[30], + nested: v11, + }; + if ( + !v12 || + v12.constructor !== + Object + ) { + e[31](v12); + } + v6 = v12; + } catch (e25) { + e[32]([ + e0, + e1, + e2, + e3, + e4, + e5, + e6, + e7, + e8, + e9, + e10, + e11, + e12, + e13, + e14, + e15, + e16, + e17, + e18, + e19, + e20, + e21, + e22, + e23, + e24, + e25, + ]); + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + return v6; + }; + return r0(i); };