Skip to content

Commit

Permalink
Don't duplicate type check for union items
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Jul 13, 2024
1 parent 5dbb4e4 commit bb4c8de
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_union_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test("Parses when both schemas misses parser and have the same type", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(typeof i!=="string"){e[0](i)}else{e[5]([e[2],e[4],])}return i}`,
`i=>{if(typeof i!=="string"){e[0](i)}else{e[3]([e[1],e[2],])}return i}`,
)
})

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"];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}`,
`i=>{let v2=i;if(!i||i.constructor!==Object){e[0](i)}else{try{let v0=i["kind"],v1=i["radius"];if(v0!=="circle"){e[1](v0)}if(typeof v1!=="number"||Number.isNaN(v1)){e[2](v1)}v2={"TAG":e[3],"radius":v1,}}catch(e0){try{let v3=i["kind"],v4=i["x"];if(v3!=="square"){e[4](v3)}if(typeof v4!=="number"||Number.isNaN(v4)){e[5](v4)}v2={"TAG":e[6],"x":v4,}}catch(e1){try{let v5=i["kind"],v6=i["x"],v7=i["y"];if(v5!=="triangle"){e[7](v5)}if(typeof v6!=="number"||Number.isNaN(v6)){e[8](v6)}if(typeof v7!=="number"||Number.isNaN(v7)){e[9](v7)}v2={"TAG":e[10],"x":v6,"y":v7,}}catch(e2){e[11]([e0,e1,e2,])}}}}return v2}`,
)
})

Expand Down
4 changes: 3 additions & 1 deletion src/S_Core.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,9 @@ function parseSameType(b, schemas, input, output, path) {
var errorVar = "e" + idx;
try {
b.c = b.c + "try{";
var itemOutput = parseWithTypeCheck(b, schema, input, "");
var bb = scope(b);
var itemOutput = schema.p(bb, input, schema, "");
b.c = b.c + allocateScope(bb);
b.c = b.c + (set(b, output, itemOutput) + "}catch(" + errorVar + "){");
loopSchemas(idx + 1 | 0, errorCodes + errorVar + ",");
b.c = b.c + "}";
Expand Down
4 changes: 3 additions & 1 deletion src/S_Core.res
Original file line number Diff line number Diff line change
Expand Up @@ -2862,7 +2862,9 @@ module Union = {

try {
b.code = b.code ++ `try{`
let itemOutput = b->B.parseWithTypeCheck(~schema, ~input, ~path=Path.empty)
let bb = b->B.scope
let itemOutput = bb->B.parse(~schema, ~input, ~path=Path.empty)
b.code = b.code ++ bb->B.allocateScope
b.code = b.code ++ `${b->B.Val.set(output, itemOutput)}}catch(${errorVar}){`
loopSchemas(idx + 1, errorCodes ++ errorVar ++ ",")
b.code = b.code ++ "}"
Expand Down

2 comments on commit bb4c8de

@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: bb4c8de Previous: 06df40b Ratio
Parse string 820325883 ops/sec (±0.12%) 818372259 ops/sec (±0.13%) 1.00
Serialize string 818966758 ops/sec (±0.08%) 819110535 ops/sec (±0.05%) 1.00
Advanced object schema factory 456449 ops/sec (±0.42%) 449156 ops/sec (±0.56%) 0.98
Parse advanced object 57521727 ops/sec (±0.55%) 45100200 ops/sec (±0.16%) 0.78
Assert advanced object 173945527 ops/sec (±0.16%)
Create and parse advanced object 93485 ops/sec (±0.37%) 35041 ops/sec (±1.18%) 0.37
Parse advanced strict object 25565695 ops/sec (±0.31%) 22748126 ops/sec (±0.24%) 0.89
Assert advanced strict object 31012322 ops/sec (±0.21%)
Serialize advanced object 75503081 ops/sec (±1.23%) 806595235 ops/sec (±0.11%) 10.68

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: bb4c8de Previous: 06df40b Ratio
Serialize advanced object 75503081 ops/sec (±1.23%) 806595235 ops/sec (±0.11%) 10.68

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

Please sign in to comment.