Skip to content

Commit

Permalink
Invert literals check
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Jul 13, 2024
1 parent ec6fe04 commit 7f1bc64
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 92 deletions.
4 changes: 2 additions & 2 deletions packages/tests/src/core/Example_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ test("Compiled parse code snapshot", t => {
t->U.assertCompiledCode(
~schema=filmSchema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[11](i)}let v0=i["Id"],v1=i["Title"],v2=i["Tags"],v6=i["Rating"],v7=i["Age"];if(typeof v0!=="number"||Number.isNaN(v0)){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(v2!==void 0&&(!Array.isArray(v2))){e[2](v2)}if(v2!==void 0){for(let v3=0;v3<v2.length;++v3){let v5=v2[v3];try{if(typeof v5!=="string"){e[3](v5)}}catch(v4){if(v4&&v4.s===s){v4.path="[\\"Tags\\"]"+\'["\'+v3+\'"]\'+v4.path}throw v4}}}if(!(v6==="G")){if(!(v6==="PG")){if(!(v6==="PG13")){if(!(v6==="R")){e[5](v6)}else{v6==="R"||e[6](v6);}}else{v6==="PG13"||e[7](v6);}}else{v6==="PG"||e[8](v6);}}else{v6==="G"||e[9](v6);}if(v7!==void 0&&(typeof v7!=="number"||v7>2147483647||v7<-2147483648||v7%1!==0)){e[10](v7)}return {"id":v0,"title":v1,"tags":v2===void 0?e[4]:v2,"rating":v6,"deprecatedAgeRestriction":v7,}}`,
`i=>{if(!i||i.constructor!==Object){e[11](i)}let v0=i["Id"],v1=i["Title"],v2=i["Tags"],v6=i["Rating"],v7=i["Age"];if(typeof v0!=="number"||Number.isNaN(v0)){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(v2!==void 0&&(!Array.isArray(v2))){e[2](v2)}if(v2!==void 0){for(let v3=0;v3<v2.length;++v3){let v5=v2[v3];try{if(typeof v5!=="string"){e[3](v5)}}catch(v4){if(v4&&v4.s===s){v4.path="[\\"Tags\\"]"+\'["\'+v3+\'"]\'+v4.path}throw v4}}}if(v6!=="G"){if(v6!=="PG"){if(v6!=="PG13"){if(v6!=="R"){e[5](v6)}else{if(v6!=="R"){e[6](v6)}}}else{if(v6!=="PG13"){e[7](v6)}}}else{if(v6!=="PG"){e[8](v6)}}}else{if(v6!=="G"){e[9](v6)}}if(v7!==void 0&&(typeof v7!=="number"||v7>2147483647||v7<-2147483648||v7%1!==0)){e[10](v7)}return {"id":v0,"title":v1,"tags":v2===void 0?e[4]:v2,"rating":v6,"deprecatedAgeRestriction":v7,}}`,
)
})

test("Compiled serialize code snapshot", t => {
t->U.assertCompiledCode(
~schema=filmSchema,
~op=#Serialize,
`i=>{let v0=i["tags"],v1,v2=i["rating"],v3,v4=i["deprecatedAgeRestriction"],v5;if(v0!==void 0){v1=e[0](v0)}try{v2==="G"||e[1](v2);v3=v2}catch(e0){try{v2==="PG"||e[2](v2);v3=v2}catch(e1){try{v2==="PG13"||e[3](v2);v3=v2}catch(e2){try{v2==="R"||e[4](v2);v3=v2}catch(e3){e[5]([e0,e1,e2,e3,])}}}}if(v4!==void 0){v5=e[6](v4)}return {"Id":i["id"],"Title":i["title"],"Tags":v1,"Rating":v3,"Age":v5,}}`,
`i=>{let v0=i["tags"],v1,v2=i["rating"],v3,v4=i["deprecatedAgeRestriction"],v5;if(v0!==void 0){v1=e[0](v0)}try{if(v2!=="G"){e[1](v2)}v3=v2}catch(e0){try{if(v2!=="PG"){e[2](v2)}v3=v2}catch(e1){try{if(v2!=="PG13"){e[3](v2)}v3=v2}catch(e2){try{if(v2!=="R"){e[4](v2)}v3=v2}catch(e3){e[5]([e0,e1,e2,e3,])}}}}if(v4!==void 0){v5=e[6](v4)}return {"Id":i["id"],"Title":i["title"],"Tags":v1,"Rating":v3,"Age":v5,}}`,
)
})
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Array_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module Common = {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]==="bar"&&i[1]===true)||e[1](i);return i}`,
`i=>{if(i!==e[0]&&(!Array.isArray(i)||i.length!==2||i[0]!=="bar"||i[1]!==true)){e[1](i)}return i}`,
)
})

Expand All @@ -97,7 +97,7 @@ module Common = {
t->U.assertCompiledCode(
~schema,
~op=#Serialize,
`i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]==="bar"&&i[1]===true)||e[1](i);return i}`,
`i=>{if(i!==e[0]&&(!Array.isArray(i)||i.length!==2||i[0]!=="bar"||i[1]!==true)){e[1](i)}return i}`,
)
})
}
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Boolean_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ module Common = {
test("Compiled parse code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===false||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==false){e[0](i)}return i}`)
})

test("Compiled serialize code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===false||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==false){e[0](i)}return i}`)
})
}
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_NaN_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ module Common = {
test("Compiled parse code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{Number.isNaN(i)||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(!Number.isNaN(i)){e[0](i)}return i}`)
})

test("Compiled serialize code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{Number.isNaN(i)||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(!Number.isNaN(i)){e[0](i)}return i}`)
})
}
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Null_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ module Common = {
test("Compiled parse code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===null||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==null){e[0](i)}return i}`)
})

test("Compiled serialize code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===null||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==null){e[0](i)}return i}`)
})
}
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Number_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ module Common = {
test("Compiled parse code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===123||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==123){e[0](i)}return i}`)
})

test("Compiled serialize code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===123||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==123){e[0](i)}return i}`)
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Object_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module Common = {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{(i===e[0]||i&&i.constructor===Object&&Object.keys(i).length===1&&i["foo"]==="bar")||e[1](i);return i}`,
`i=>{if(i!==e[0]&&(!i||i.constructor!==Object||Object.keys(i).length!==1||i["foo"]!=="bar")){e[1](i)}return i}`,
)
})

Expand All @@ -126,7 +126,7 @@ module Common = {
t->U.assertCompiledCode(
~schema,
~op=#Serialize,
`i=>{(i===e[0]||i&&i.constructor===Object&&Object.keys(i).length===1&&i["foo"]==="bar")||e[1](i);return i}`,
`i=>{if(i!==e[0]&&(!i||i.constructor!==Object||Object.keys(i).length!==1||i["foo"]!=="bar")){e[1](i)}return i}`,
)
})
}
8 changes: 6 additions & 2 deletions packages/tests/src/core/S_literal_String_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ module Common = {
test("Compiled parse code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i==="ReScript is Great!"||e[0](i);return i}`)
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(i!=="ReScript is Great!"){e[0](i)}return i}`,
)
})

test("Compiled serialize code snapshot", t => {
Expand All @@ -80,7 +84,7 @@ module Common = {
t->U.assertCompiledCode(
~schema,
~op=#Serialize,
`i=>{i==="ReScript is Great!"||e[0](i);return i}`,
`i=>{if(i!=="ReScript is Great!"){e[0](i)}return i}`,
)
})
}
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Undefined_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ module Common = {
test("Compiled parse code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{i===undefined||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Parse, `i=>{if(i!==undefined){e[0](i)}return i}`)
})

test("Compiled serialize code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{i===undefined||e[0](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#Serialize, `i=>{if(i!==undefined){e[0](i)}return i}`)
})
}
2 changes: 1 addition & 1 deletion packages/tests/src/core/S_object_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[5](i)}let v0=i["tag"],v1=i["FOO"],v2=i["BAR"],v3;v0===0||e[0](v0);if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}for(v3 in i){if(v3!=="tag"&&v3!=="FOO"&&v3!=="BAR"){e[3](v3)}}return {"foo":v1,"bar":v2,"zoo":e[4],}}`,
`i=>{if(!i||i.constructor!==Object){e[5](i)}let v0=i["tag"],v1=i["FOO"],v2=i["BAR"],v3;if(v0!==0){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}for(v3 in i){if(v3!=="tag"&&v3!=="FOO"&&v3!=="BAR"){e[3](v3)}}return {"foo":v1,"bar":v2,"zoo":e[4],}}`,
)
},
)
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/src/core/S_tuple_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ module Compiled = {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!Array.isArray(i)||i.length!==3){e[4](i)}let v0=i["0"],v1=i["1"],v2=i["2"];v0===0||e[0](v0);if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}return {"foo":v1,"bar":v2,"zoo":e[3],}}`,
`i=>{if(!Array.isArray(i)||i.length!==3){e[4](i)}let v0=i["0"],v1=i["1"],v2=i["2"];if(v0!==0){e[0](v0)}if(typeof v1!=="string"){e[1](v1)}if(typeof v2!=="boolean"){e[2](v2)}return {"foo":v1,"bar":v2,"zoo":e[3],}}`,
)
},
)
Expand Down
14 changes: 7 additions & 7 deletions packages/tests/src/core/S_union_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test("Parses when both schemas misses parser and have different types", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!(i==="apple")){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{throw e[3]}return i}`,
`i=>{if(i!=="apple"){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{throw e[3]}return i}`,
)
})

Expand Down Expand Up @@ -141,7 +141,7 @@ test("Parses when second struct misses parser", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!(i==="apple")){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{i==="apple"||e[2](i);}return i}`,
`i=>{if(i!=="apple"){if(typeof i!=="string"){e[0](i)}else{throw e[1]}}else{if(i!=="apple"){e[2](i)}}return i}`,
)
})

Expand All @@ -153,7 +153,7 @@ test("Serializes when second struct misses serializer", t => {
t->U.assertCompiledCode(
~schema,
~op=#Serialize,
`i=>{let v0;try{i==="apple"||e[0](i);v0=i}catch(e0){e[2]([e0,e[1],])}return v0}`,
`i=>{let v0;try{if(i!=="apple"){e[0](i)}v0=i}catch(e0){e[2]([e0,e[1],])}return v0}`,
)
})

Expand Down Expand Up @@ -405,7 +405,7 @@ module Advanced = {
t->U.assertCompiledCode(
~schema=shapeSchema,
~op=#Parse,
`i=>{let v2=i;if(!i||i.constructor!==Object){e[0](i)}else{try{if(!i||i.constructor!==Object){e[1](i)}let v0=i["kind"],v1=i["radius"];v0==="circle"||e[2](v0);if(typeof v1!=="number"||Number.isNaN(v1)){e[3](v1)}v2={"TAG":e[4],"radius":v1,}}catch(e0){try{if(!i||i.constructor!==Object){e[5](i)}let v3=i["kind"],v4=i["x"];v3==="square"||e[6](v3);if(typeof v4!=="number"||Number.isNaN(v4)){e[7](v4)}v2={"TAG":e[8],"x":v4,}}catch(e1){try{if(!i||i.constructor!==Object){e[9](i)}let v5=i["kind"],v6=i["x"],v7=i["y"];v5==="triangle"||e[10](v5);if(typeof v6!=="number"||Number.isNaN(v6)){e[11](v6)}if(typeof v7!=="number"||Number.isNaN(v7)){e[12](v7)}v2={"TAG":e[13],"x":v6,"y":v7,}}catch(e2){e[14]([e0,e1,e2,])}}}}return v2}`,
`i=>{let v2=i;if(!i||i.constructor!==Object){e[0](i)}else{try{if(!i||i.constructor!==Object){e[1](i)}let v0=i["kind"],v1=i["radius"];if(v0!=="circle"){e[2](v0)}if(typeof v1!=="number"||Number.isNaN(v1)){e[3](v1)}v2={"TAG":e[4],"radius":v1,}}catch(e0){try{if(!i||i.constructor!==Object){e[5](i)}let v3=i["kind"],v4=i["x"];if(v3!=="square"){e[6](v3)}if(typeof v4!=="number"||Number.isNaN(v4)){e[7](v4)}v2={"TAG":e[8],"x":v4,}}catch(e1){try{if(!i||i.constructor!==Object){e[9](i)}let v5=i["kind"],v6=i["x"],v7=i["y"];if(v5!=="triangle"){e[10](v5)}if(typeof v6!=="number"||Number.isNaN(v6)){e[11](v6)}if(typeof v7!=="number"||Number.isNaN(v7)){e[12](v7)}v2={"TAG":e[13],"x":v6,"y":v7,}}catch(e2){e[14]([e0,e1,e2,])}}}}return v2}`,
)
})

Expand Down Expand Up @@ -441,7 +441,7 @@ test("Compiled parse code snapshot", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!(i===0)){if(!(i===1)){e[0](i)}else{i===1||e[1](i);}}else{i===0||e[2](i);}return i}`,
`i=>{if(i!==0){if(i!==1){e[0](i)}else{if(i!==1){e[1](i)}}}else{if(i!==0){e[2](i)}}return i}`,
)
})

Expand All @@ -454,7 +454,7 @@ test("Compiled async parse code snapshot", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{let v0=i;if(!(i===0)){if(!(i===1)){e[0](i)}else{i===1||e[1](i);}}else{i===0||e[2](i);v0=e[3](i)}return Promise.resolve(v0)}`,
`i=>{let v0=i;if(i!==0){if(i!==1){e[0](i)}else{if(i!==1){e[1](i)}}}else{if(i!==0){e[2](i)}v0=e[3](i)}return Promise.resolve(v0)}`,
)
})

Expand All @@ -465,7 +465,7 @@ test("Compiled serialize code snapshot", t => {
t->U.assertCompiledCode(
~schema,
~op=#Serialize,
`i=>{let v0;try{i===0||e[0](i);v0=i}catch(e0){try{i===1||e[1](i);v0=i}catch(e1){e[2]([e0,e1,])}}return v0}`,
`i=>{let v0;try{if(i!==0){e[0](i)}v0=i}catch(e0){try{if(i!==1){e[1](i)}v0=i}catch(e1){e[2]([e0,e1,])}}return v0}`,
)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/tests/src/core/S_variant_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ test(
t->U.assertCompiledCode(
~schema,
~op=#Serialize,
`i=>{let v0=e[2];if(i!==e[0]){e[1](i)}(v0===e[3]||Array.isArray(v0)&&v0.length===2&&v0[0]===true&&v0[1]===12)||e[4](v0);return v0}`,
`i=>{let v0=e[2];if(i!==e[0]){e[1](i)}if(v0!==e[3]&&(!Array.isArray(v0)||v0.length!==2||v0[0]!==true||v0[1]!==12)){e[4](v0)}return v0}`,
)
},
)
Expand Down
Loading

2 comments on commit 7f1bc64

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7f1bc64 Previous: 06df40b Ratio
Parse string 817722630 ops/sec (±0.30%) 818372259 ops/sec (±0.13%) 1.00
Serialize string 819694781 ops/sec (±0.11%) 819110535 ops/sec (±0.05%) 1.00
Advanced object schema factory 457513 ops/sec (±0.30%) 449156 ops/sec (±0.56%) 0.98
Parse advanced object 57788793 ops/sec (±0.55%) 45100200 ops/sec (±0.16%) 0.78
Assert advanced object 173765767 ops/sec (±0.28%)
Create and parse advanced object 93852 ops/sec (±0.37%) 35041 ops/sec (±1.18%) 0.37
Parse advanced strict object 25531219 ops/sec (±0.24%) 22748126 ops/sec (±0.24%) 0.89
Assert advanced strict object 30887418 ops/sec (±0.54%)
Serialize advanced object 53424747 ops/sec (±1.28%) 806595235 ops/sec (±0.11%) 15.10

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 7f1bc64 Previous: 06df40b Ratio
Serialize advanced object 53424747 ops/sec (±1.28%) 806595235 ops/sec (±0.11%) 15.10

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.