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

Feature: TypeBoxValidatorCompiler #24

Merged
merged 9 commits into from
Jul 8, 2022
Merged

Feature: TypeBoxValidatorCompiler #24

merged 9 commits into from
Jul 8, 2022

Conversation

sinclairzx81
Copy link
Contributor

Checklist

This PR implements the TypeBox TypeCompiler mentioned on issue #23. This compiler can used to attain faster runtime validation performance (roughly 1.5x the performance of AJV), as well as significantly increase schema compilation time. Note this compiler only accepts TypeBox types for compilation, and thus should be considered optional from a users standpoint.

Documentation on the Compiler and associated Benchmarks can be located on the TypeBox repository located here. Current benchmarks listed below.

Validate

This benchmark measures overall validate performance. You can review this benchmark here.

┌──────────────────┬────────────┬────────────┬────────────┬────────────────────────┐
     (index)       Iterations     Ajv       TypeBox           Measured        
├──────────────────┼────────────┼────────────┼────────────┼────────────────────────┤
           Number   16000000   '74ms    '  '66ms    '  '1.12 x faster       ' 
           String   16000000   '277ms   '  '154ms   '  '1.80 x faster       ' 
          Boolean   16000000   '263ms   '  '152ms   '  '1.73 x faster       ' 
             Null   16000000   '275ms   '  '153ms   '  '1.80 x faster       ' 
            RegEx   16000000   '658ms   '  '548ms   '  '1.20 x faster       ' 
          ObjectA   16000000   '453ms   '  '319ms   '  '1.42 x faster       ' 
          ObjectB   16000000   '676ms   '  '514ms   '  '1.32 x faster       ' 
            Tuple   16000000   '320ms   '  '193ms   '  '1.66 x faster       ' 
            Union   16000000   '331ms   '  '211ms   '  '1.57 x faster       ' 
        Recursive   16000000   '6010ms  '  '2368ms  '  '2.54 x faster       ' 
          Vector4   16000000   '313ms   '  '168ms   '  '1.86 x faster       ' 
          Matrix4   16000000   '604ms   '  '419ms   '  '1.44 x faster       ' 
   Literal_String   16000000   '281ms   '  '154ms   '  '1.82 x faster       ' 
   Literal_Number   16000000   '268ms   '  '150ms   '  '1.79 x faster       ' 
  Literal_Boolean   16000000   '273ms   '  '149ms   '  '1.83 x faster       ' 
     Array_Number   16000000   '470ms   '  '235ms   '  '2.00 x faster       ' 
     Array_String   16000000   '462ms   '  '303ms   '  '1.52 x faster       ' 
    Array_Boolean   16000000   '527ms   '  '352ms   '  '1.50 x faster       ' 
    Array_ObjectA   16000000   '43852ms '  '28042ms '  '1.56 x faster       ' 
    Array_ObjectB   16000000   '48025ms '  '33205ms '  '1.45 x faster       ' 
      Array_Tuple   16000000   '1424ms  '  '1095ms  '  '1.30 x faster       ' 
    Array_Vector4   16000000   '1495ms  '  '808ms   '  '1.85 x faster       ' 
    Array_Matrix4   16000000   '6129ms  '  '4969ms  '  '1.23 x faster       ' 
└──────────────────┴────────────┴────────────┴────────────┴────────────────────────┘

Compile

This benchmark measures schema compilation time. You can review this benchmark here.

┌──────────────────┬────────────┬────────────┬────────────┬────────────────────────┐
     (index)       Iterations     Ajv       TypeBox           Measured        
├──────────────────┼────────────┼────────────┼────────────┼────────────────────────┤
           Number     2000     '400ms   '  '8ms     '  '50.00 x faster      ' 
           String     2000     '324ms   '  '7ms     '  '46.29 x faster      ' 
          Boolean     2000     '325ms   '  '10ms    '  '32.50 x faster      ' 
             Null     2000     '271ms   '  '4ms     '  '67.75 x faster      ' 
            RegEx     2000     '493ms   '  '11ms    '  '44.82 x faster      ' 
          ObjectA     2000     '2998ms  '  '27ms    '  '111.04 x faster     ' 
          ObjectB     2000     '3058ms  '  '28ms    '  '109.21 x faster     ' 
            Tuple     2000     '1306ms  '  '19ms    '  '68.74 x faster      ' 
            Union     2000     '1450ms  '  '18ms    '  '80.56 x faster      ' 
          Vector4     2000     '1633ms  '  '12ms    '  '136.08 x faster     ' 
          Matrix4     2000     '984ms   '  '10ms    '  '98.40 x faster      ' 
   Literal_String     2000     '376ms   '  '5ms     '  '75.20 x faster      ' 
   Literal_Number     2000     '396ms   '  '7ms     '  '56.57 x faster      ' 
  Literal_Boolean     2000     '383ms   '  '3ms     '  '127.67 x faster     ' 
     Array_Number     2000     '748ms   '  '6ms     '  '124.67 x faster     ' 
     Array_String     2000     '774ms   '  '5ms     '  '154.80 x faster     ' 
    Array_Boolean     2000     '814ms   '  '8ms     '  '101.75 x faster     ' 
    Array_ObjectA     2000     '3675ms  '  '24ms    '  '153.13 x faster     ' 
    Array_ObjectB     2000     '4364ms  '  '30ms    '  '145.47 x faster     ' 
      Array_Tuple     2000     '2236ms  '  '13ms    '  '172.00 x faster     ' 
    Array_Vector4     2000     '1772ms  '  '15ms    '  '118.13 x faster     ' 
    Array_Matrix4     2000     '1612ms  '  '10ms    '  '161.20 x faster     ' 
└──────────────────┴────────────┴────────────┴────────────┴────────────────────────┘

Additional Updates

  • Updated type documentation and include JSDoc @example for TypeBoxTypeProvider and TypeBoxValidatorCompiler respectively. (Useful for vscode intellisense)
  • Implemented tests

Not implemented

  • Re-export TypeBox Type and and associated types on this provider (note: re-exporting TypeBox currently causes coverage tests to fall below threshold)

Submitting for review

tests/index.js Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
tests/index.js Outdated Show resolved Hide resolved
tests/index.js Outdated Show resolved Hide resolved
tests/index.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Member

mcollina commented Jul 8, 2022

@sinclairzx81 are you fixing the other tests with ready() or should I land this?

@sinclairzx81
Copy link
Contributor Author

are you fixing the other tests with ready() or should I land this?

@mcollina It's ok, all fixed in this PR :)

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

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

LGTM.

@mcollina mcollina merged commit 59fa040 into fastify:main Jul 8, 2022
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.

4 participants