Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revision 0.32.29 #862

Merged
merged 2 commits into from
May 4, 2024
Merged

Revision 0.32.29 #862

merged 2 commits into from
May 4, 2024

Conversation

sinclairzx81
Copy link
Owner

@sinclairzx81 sinclairzx81 commented May 4, 2024

This PR provides an optimization on Intersect Encode and Decode by using a different strategy for resolving key and key schematics. It was noted that Key + Index resolve operations were slow when Index was called per iteration, this PR resolves all schematics in a single call resulting in roughly 2x better performance.

Test Case

import { Type } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

const N = Type.Transform(Type.Number())
  .Decode(value => value)
  .Encode(value => value)

const I = Type.Intersect([
  Type.Object({ a: N, b: N, c: N }),
  Type.Object({ d: N, e: N, f: N }),
  Type.Object({ g: N, h: N, i: N })
])

const V = Value.Create(I)
const D = Value.Decode(I, V)
const L = 100_000

console.time('intersect-decode')
for(let i = 0; i < L; i++) Value.Decode(I, V)
console.timeEnd('intersect-decode')

console.time('intersect-encode')
for(let i = 0; i < L; i++) Value.Encode(I, D)
console.timeEnd('intersect-encode')

// before:
//
// intersect-decode: 3.541s
// intersect-encode: 3.483s
//
// after:
//
// intersect-decode: 1.509s
// intersect-encode: 1.369s

This PR also updates the Value.Decode and Value.Encode functions to check for HasTransform prior to invoking the codecs. Schematics without Transforms will yield the checked value only leading to significantly better performance. This check was missed on the revision 0.32.0.

@sinclairzx81 sinclairzx81 merged commit 7a42aee into master May 4, 2024
18 checks passed
@sinclairzx81 sinclairzx81 deleted the fast branch May 4, 2024 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant