From e90bfcebbeb50218cda0644dc13fb6ab6fa37ab3 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Fri, 11 Oct 2024 14:43:40 +0900 Subject: [PATCH] Fix reverseConvert performance --- src/S_Core.bs.mjs | 15 +++++++-------- src/S_Core.res | 22 ++++++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/S_Core.bs.mjs b/src/S_Core.bs.mjs index 52c47cb..5396fe9 100644 --- a/src/S_Core.bs.mjs +++ b/src/S_Core.bs.mjs @@ -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; } @@ -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){ @@ -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){ @@ -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) { diff --git a/src/S_Core.res b/src/S_Core.res index 500cae0..e5d7c58 100644 --- a/src/S_Core.res +++ b/src/S_Core.res @@ -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 @@ -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 } @@ -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 => { @@ -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) }