Skip to content

Commit

Permalink
Fix reverseConvert performance
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Oct 11, 2024
1 parent 870b742 commit e90bfce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
15 changes: 7 additions & 8 deletions src/S_Core.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ function operationFn(s, o) {
if ((o in s)) {
return (s[o]);
}
var f = compile(s.b, s, o);
var ss = o & 32 ? s.r() : s;
var f = compile(ss.b, ss, o);
((s[o] = f));
return f;
}
Expand Down Expand Up @@ -853,11 +854,10 @@ function serializeOrRaiseWith(value, schema) {
}

function serializeWith(value, schema) {
var schema$1 = schema.r();
try {
return {
TAG: "Ok",
_0: operationFn(schema$1, 8)(value)
_0: operationFn(schema, 40)(value)
};
}
catch (exn){
Expand All @@ -866,15 +866,14 @@ function serializeWith(value, schema) {
}

function serializeToUnknownOrRaiseWith(value, schema) {
return operationFn(schema.r(), 0)(value);
return operationFn(schema, 32)(value);
}

function serializeToUnknownWith(value, schema) {
var schema$1 = schema.r();
try {
return {
TAG: "Ok",
_0: operationFn(schema$1, 0)(value)
_0: operationFn(schema, 32)(value)
};
}
catch (exn){
Expand Down Expand Up @@ -938,11 +937,11 @@ function initialAssertOrRaise(unknown) {
}

function initialSerializeToUnknownOrRaise(unknown) {
return operationFn(this.r(), 0)(unknown);
return operationFn(this, 32)(unknown);
}

function initialSerializeOrRaise(unknown) {
return operationFn(this.r(), 8)(unknown);
return operationFn(this, 40)(unknown);
}

function jsParse(unknown) {
Expand Down
22 changes: 14 additions & 8 deletions src/S_Core.res
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ module Operation = {
@inline let assertOutput = 4
@inline let jsonableOutput = 8
@inline let jsonStringOutput = 16
@inline let reverse = 32
}

type t = int
Expand Down Expand Up @@ -865,7 +866,8 @@ let operationFn = (s, o) => {
if %raw(`o in s`) {
%raw(`s[o]`)
} else {
let f = s.builder->Builder.compile(~schema=s, ~operation=o)
let ss = o->Operation.unsafeHasFlag(Operation.Flag.reverse) ? s.reverse() : s
let f = ss.builder->Builder.compile(~schema=ss, ~operation=o)
let _ = %raw(`s[o] = f`)
f
}
Expand Down Expand Up @@ -1393,19 +1395,21 @@ let serializeOrRaiseWith = (value, schema) => {
}

let serializeWith = (value, schema) => {
schema.reverse()->useSyncOperation(
Operation.make()->Operation.addFlag(Operation.Flag.jsonableOutput),
schema->useSyncOperation(
Operation.make()
->Operation.addFlag(Operation.Flag.reverse)
->Operation.addFlag(Operation.Flag.jsonableOutput),
value,
)
}

@inline
let serializeToUnknownOrRaiseWith = (value, schema) => {
(schema.reverse()->operationFn(Operation.make()))(value)
(schema->operationFn(Operation.make()->Operation.addFlag(Operation.Flag.reverse)))(value)
}

let serializeToUnknownWith = (value, schema) => {
schema.reverse()->useSyncOperation(Operation.make(), value)
schema->useSyncOperation(Operation.make()->Operation.addFlag(Operation.Flag.reverse), value)
}

let serializeToJsonStringOrRaiseWith = (value: 'value, schema: t<'value>, ~space=0): string => {
Expand Down Expand Up @@ -1462,13 +1466,15 @@ let initialAssertOrRaise = unknown => {
}

let initialSerializeToUnknownOrRaise = unknown => {
((%raw(`this`)).reverse()->operationFn(Operation.make()))(unknown)
(%raw(`this`)->operationFn(Operation.make()->Operation.addFlag(Operation.Flag.reverse)))(unknown)
}

let initialSerializeOrRaise = unknown => {
(
(%raw(`this`)).reverse()->operationFn(
Operation.make()->Operation.addFlag(Operation.Flag.jsonableOutput),
%raw(`this`)->operationFn(
Operation.make()
->Operation.addFlag(Operation.Flag.jsonableOutput)
->Operation.addFlag(Operation.Flag.reverse),
)
)(unknown)
}
Expand Down

2 comments on commit e90bfce

@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: e90bfce Previous: 90ab99c Ratio
Parse string 168709139 ops/sec (±0.31%) 811431552 ops/sec (±0.15%) 4.81
Reverse convert string 289731040 ops/sec (±0.33%)
Advanced object schema factory 476543 ops/sec (±0.80%) 472293 ops/sec (±0.54%) 0.99
Parse advanced object 51995665 ops/sec (±0.33%) 56799894 ops/sec (±0.57%) 1.09
Assert advanced object - compile 163884751 ops/sec (±0.33%)
Assert advanced object 166954882 ops/sec (±0.15%) 171920805 ops/sec (±0.20%) 1.03
Create and parse advanced object 92812 ops/sec (±0.28%) 95083 ops/sec (±0.21%) 1.02
Create and parse advanced object - with S.schema 102477 ops/sec (±0.21%)
Parse advanced strict object 24259680 ops/sec (±0.20%) 25471962 ops/sec (±0.17%) 1.05
Assert advanced strict object 28808151 ops/sec (±0.44%) 30460141 ops/sec (±0.20%) 1.06
Reverse convert advanced object 59779091 ops/sec (±0.37%)
Reverse convert advanced object - with S.schema 58050103 ops/sec (±0.20%)
Reverse convert advanced object - compile 805022952 ops/sec (±0.11%)

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: e90bfce Previous: 90ab99c Ratio
Parse string 168709139 ops/sec (±0.31%) 811431552 ops/sec (±0.15%) 4.81

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

Please sign in to comment.