qri-io / jsonschema

golang implementation of https://json-schema.org drafts 7 & 2019-09
MIT License
461 stars 54 forks source link

feat(*): implement json type system #55

Closed asido closed 4 years ago

asido commented 5 years ago

As a continuation to PR #54, I attempted to see what would it take to implement a type system. It turned out not bad. The patch implements json type system, that maps any possible Go type to json type: null, boolean, integer, number, string, object, array. It allows validators not to care about specific Go type and their differences. For example, a validator can type assert object and read data without needing to differentiate between map and struct.

The most important part of the patch is a new file types.go. The rest is primarily patching existing validators. The new code needs testing, which I'll do if you consider merging.

I benchmarked the current master and this branch using benchmarks from PR #56. It has a significant performance degradation running type validator. I haven't checked exactly where most of the time is spent, but is worth to see if we can optimize it.

// master
BenchmarkAdditionalItems/sample_size_1-8             3000000           411 ns/op          52 B/op          5 allocs/op
BenchmarkAdditionalItems/sample_size_10-8            1000000          2261 ns/op         232 B/op         32 allocs/op
BenchmarkAdditionalItems/sample_size_100-8            100000         21225 ns/op        2248 B/op        302 allocs/op
BenchmarkAdditionalItems/sample_size_1000-8            10000        241823 ns/op       27448 B/op       3902 allocs/op
BenchmarkAdditionalProperties/sample_size_1-8        3000000           571 ns/op          54 B/op          5 allocs/op
BenchmarkAdditionalProperties/sample_size_10-8        300000          4278 ns/op         256 B/op         32 allocs/op
BenchmarkAdditionalProperties/sample_size_100-8        10000        105712 ns/op        2428 B/op        302 allocs/op
BenchmarkAdditionalProperties/sample_size_1000-8         200       8885443 ns/op       26613 B/op       3002 allocs/op
BenchmarkConst/sample_size_1-8                       1000000          1141 ns/op         720 B/op         13 allocs/op
BenchmarkConst/sample_size_10-8                       200000          6168 ns/op        2134 B/op         68 allocs/op
BenchmarkConst/sample_size_100-8                       30000         58875 ns/op       20591 B/op        616 allocs/op
BenchmarkConst/sample_size_1000-8                       2000        643018 ns/op      267220 B/op       6037 allocs/op
BenchmarkContains/sample_size_1-8                    3000000           496 ns/op         208 B/op          3 allocs/op
BenchmarkContains/sample_size_10-8                    200000          9214 ns/op        3170 B/op         85 allocs/op
BenchmarkContains/sample_size_100-8                    20000         98430 ns/op       33511 B/op       1094 allocs/op
BenchmarkContains/sample_size_1000-8                    2000       1010793 ns/op      336124 B/op      10994 allocs/op
BenchmarkDependencies/sample_size_1-8                5000000           404 ns/op          38 B/op          4 allocs/op
BenchmarkDependencies/sample_size_10-8               2000000           602 ns/op          38 B/op          4 allocs/op
BenchmarkDependencies/sample_size_100-8              1000000          2200 ns/op          38 B/op          4 allocs/op
BenchmarkDependencies/sample_size_1000-8               50000         26662 ns/op          38 B/op          4 allocs/op
BenchmarkEnum/sample_size_1-8                        3000000           415 ns/op         176 B/op          2 allocs/op
BenchmarkEnum/sample_size_10-8                        300000          5131 ns/op        1657 B/op         42 allocs/op
BenchmarkEnum/sample_size_100-8                        30000         46125 ns/op       15306 B/op        483 allocs/op
BenchmarkEnum/sample_size_1000-8                        3000        455323 ns/op      152206 B/op       4983 allocs/op
BenchmarkMaximum/sample_size_1-8                    30000000            46.2 ns/op         0 B/op          0 allocs/op
BenchmarkMaximum/sample_size_10-8                   30000000            46.2 ns/op         0 B/op          0 allocs/op
BenchmarkMaximum/sample_size_100-8                  30000000            46.7 ns/op         0 B/op          0 allocs/op
BenchmarkMaximum/sample_size_1000-8                 30000000            46.5 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_1-8                    30000000            46.7 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_10-8                   30000000            46.7 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_100-8                  30000000            47.0 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_1000-8                 30000000            46.9 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_1-8           30000000            46.6 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_10-8          30000000            46.7 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_100-8         30000000            45.7 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_1000-8        30000000            46.1 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_1-8           30000000            45.1 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_10-8          30000000            46.6 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_100-8         30000000            44.9 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_1000-8        30000000            46.0 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_1-8                   30000000            46.0 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_10-8                  30000000            47.1 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_100-8                 30000000            46.3 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_1000-8                30000000            46.2 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_1-8                   30000000            45.7 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_10-8                  30000000            46.1 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_100-8                 30000000            45.5 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_1000-8                30000000            45.7 ns/op         0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_1-8                  30000000            47.9 ns/op         0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_10-8                 30000000            52.2 ns/op         0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_100-8                20000000           115 ns/op           0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_1000-8                2000000           614 ns/op           0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_1-8                  30000000            48.5 ns/op         0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_10-8                 30000000            52.5 ns/op         0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_100-8                20000000           117 ns/op           0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_1000-8                2000000           608 ns/op           0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_1-8              30000000            45.8 ns/op         0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_10-8             30000000            46.1 ns/op         0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_100-8            30000000            45.1 ns/op         0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_1000-8           30000000            45.3 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_1-8              30000000            45.9 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_10-8             30000000            45.1 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_100-8            30000000            44.8 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_1000-8           30000000            45.0 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_1-8                 30000000            44.8 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_10-8                30000000            46.7 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_100-8               30000000            46.3 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_1000-8              30000000            46.1 ns/op         0 B/op          0 allocs/op
BenchmarkPattern/sample_size_1-8                     5000000           245 ns/op         152 B/op          2 allocs/op
BenchmarkPattern/sample_size_10-8                    3000000           433 ns/op         160 B/op          2 allocs/op
BenchmarkPattern/sample_size_100-8                   1000000          2184 ns/op         256 B/op          2 allocs/op
BenchmarkPattern/sample_size_1000-8                   100000         19968 ns/op        1168 B/op          2 allocs/op
BenchmarkType/sample_size_1-8                        1000000          2233 ns/op         150 B/op         19 allocs/op
BenchmarkType/sample_size_10-8                        100000         23057 ns/op        1360 B/op        181 allocs/op
BenchmarkType/sample_size_100-8                         5000        261737 ns/op       14396 B/op       1801 allocs/op
BenchmarkType/sample_size_1000-8                         500       2859282 ns/op      159515 B/op      18001 allocs/op

// json type system
BenchmarkAdditionalItems/sample_size_1-8             3000000           400 ns/op          52 B/op          5 allocs/op
BenchmarkAdditionalItems/sample_size_10-8            1000000          2413 ns/op         336 B/op         41 allocs/op
BenchmarkAdditionalItems/sample_size_100-8            100000         22550 ns/op        3216 B/op        401 allocs/op
BenchmarkAdditionalItems/sample_size_1000-8             5000        259886 ns/op       39216 B/op       4901 allocs/op
BenchmarkAdditionalProperties/sample_size_1-8         300000          4163 ns/op        4102 B/op         16 allocs/op
BenchmarkAdditionalProperties/sample_size_10-8        100000         14283 ns/op        5024 B/op         88 allocs/op
BenchmarkAdditionalProperties/sample_size_100-8        10000        157243 ns/op       14403 B/op        808 allocs/op
BenchmarkAdditionalProperties/sample_size_1000-8         100      10425760 ns/op      110580 B/op       8008 allocs/op
BenchmarkConst/sample_size_1-8                        500000          2403 ns/op        1520 B/op         28 allocs/op
BenchmarkConst/sample_size_10-8                       100000         12383 ns/op        4766 B/op        131 allocs/op
BenchmarkConst/sample_size_100-8                       10000        123338 ns/op       45159 B/op       1137 allocs/op
BenchmarkConst/sample_size_1000-8                       1000       1390451 ns/op      580925 B/op      11082 allocs/op
BenchmarkContains/sample_size_1-8                    1000000          1009 ns/op         392 B/op          6 allocs/op
BenchmarkContains/sample_size_10-8                    100000         14293 ns/op        5291 B/op        150 allocs/op
BenchmarkContains/sample_size_100-8                    10000        155046 ns/op       55881 B/op       1879 allocs/op
BenchmarkContains/sample_size_1000-8                    1000       1635063 ns/op      568191 B/op      18980 allocs/op
BenchmarkDependencies/sample_size_1-8                3000000           532 ns/op          86 B/op          7 allocs/op
BenchmarkDependencies/sample_size_10-8               1000000          1645 ns/op         374 B/op         25 allocs/op
BenchmarkDependencies/sample_size_100-8               100000         13244 ns/op        3254 B/op        205 allocs/op
BenchmarkDependencies/sample_size_1000-8               10000        119900 ns/op       32054 B/op       2005 allocs/op
BenchmarkEnum/sample_size_1-8                        2000000           933 ns/op         360 B/op          5 allocs/op
BenchmarkEnum/sample_size_10-8                        200000          7833 ns/op        2898 B/op         77 allocs/op
BenchmarkEnum/sample_size_100-8                        20000         69402 ns/op       26644 B/op        839 allocs/op
BenchmarkEnum/sample_size_1000-8                        2000        681553 ns/op      264422 B/op       8489 allocs/op
BenchmarkMaximum/sample_size_1-8                    30000000            40.6 ns/op         0 B/op          0 allocs/op
BenchmarkMaximum/sample_size_10-8                   30000000            41.0 ns/op         0 B/op          0 allocs/op
BenchmarkMaximum/sample_size_100-8                  30000000            40.4 ns/op         0 B/op          0 allocs/op
BenchmarkMaximum/sample_size_1000-8                 30000000            41.6 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_1-8                    30000000            41.3 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_10-8                   30000000            40.8 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_100-8                  30000000            41.1 ns/op         0 B/op          0 allocs/op
BenchmarkMinimum/sample_size_1000-8                 30000000            41.6 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_1-8           30000000            40.5 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_10-8          30000000            40.9 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_100-8         30000000            41.1 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMaximum/sample_size_1000-8        30000000            41.6 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_1-8           30000000            40.1 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_10-8          30000000            40.2 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_100-8         30000000            41.3 ns/op         0 B/op          0 allocs/op
BenchmarkExclusiveMinimum/sample_size_1000-8        30000000            41.3 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_1-8                   30000000            40.6 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_10-8                  30000000            40.2 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_100-8                 30000000            39.8 ns/op         0 B/op          0 allocs/op
BenchmarkMaxItems/sample_size_1000-8                30000000            40.1 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_1-8                   30000000            41.7 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_10-8                  30000000            40.5 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_100-8                 30000000            40.7 ns/op         0 B/op          0 allocs/op
BenchmarkMinItems/sample_size_1000-8                30000000            39.5 ns/op         0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_1-8                  30000000            43.1 ns/op         0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_10-8                 30000000            47.1 ns/op         0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_100-8                20000000           106 ns/op           0 B/op          0 allocs/op
BenchmarkMaxLength/sample_size_1000-8                3000000           565 ns/op           0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_1-8                  30000000            42.6 ns/op         0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_10-8                 30000000            47.1 ns/op         0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_100-8                20000000           104 ns/op           0 B/op          0 allocs/op
BenchmarkMinLength/sample_size_1000-8                3000000           580 ns/op           0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_1-8              30000000            49.4 ns/op         0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_10-8             30000000            48.8 ns/op         0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_100-8            30000000            52.4 ns/op         0 B/op          0 allocs/op
BenchmarkMaxProperties/sample_size_1000-8           20000000            51.6 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_1-8              30000000            49.2 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_10-8             30000000            49.7 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_100-8            30000000            48.9 ns/op         0 B/op          0 allocs/op
BenchmarkMinProperties/sample_size_1000-8           30000000            51.1 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_1-8                 30000000            43.1 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_10-8                30000000            42.1 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_100-8               30000000            42.4 ns/op         0 B/op          0 allocs/op
BenchmarkMultipleOf/sample_size_1000-8              30000000            42.4 ns/op         0 B/op          0 allocs/op
BenchmarkPattern/sample_size_1-8                    10000000           229 ns/op         152 B/op          2 allocs/op
BenchmarkPattern/sample_size_10-8                    3000000           390 ns/op         160 B/op          2 allocs/op
BenchmarkPattern/sample_size_100-8                   1000000          1840 ns/op         256 B/op          2 allocs/op
BenchmarkPattern/sample_size_1000-8                   100000         19207 ns/op        1168 B/op          2 allocs/op
BenchmarkType/sample_size_1-8                         200000          6459 ns/op        2460 B/op         40 allocs/op
BenchmarkType/sample_size_10-8                         30000         43821 ns/op        6612 B/op        364 allocs/op
BenchmarkType/sample_size_100-8                         3000        408302 ns/op       48407 B/op       3604 allocs/op
BenchmarkType/sample_size_1000-8                         300       4176650 ns/op      489146 B/op      36004 allocs/op
b5 commented 5 years ago

Running the above benchmarks through benchcmp:

ns/op bench comparison
benchmark                                            old ns/op     new ns/op     delta
BenchmarkAdditionalItems/sample_size_1-8             411           400           -2.68%
BenchmarkAdditionalItems/sample_size_10-8            2261          2413          +6.72%
BenchmarkAdditionalItems/sample_size_100-8           21225         22550         +6.24%
BenchmarkAdditionalItems/sample_size_1000-8          241823        259886        +7.47%
BenchmarkAdditionalProperties/sample_size_1-8        571           4163          +629.07%
BenchmarkAdditionalProperties/sample_size_10-8       4278          14283         +233.87%
BenchmarkAdditionalProperties/sample_size_100-8      105712        157243        +48.75%
BenchmarkAdditionalProperties/sample_size_1000-8     8885443       10425760      +17.34%
BenchmarkConst/sample_size_1-8                       1141          2403          +110.60%
BenchmarkConst/sample_size_10-8                      6168          12383         +100.76%
BenchmarkConst/sample_size_100-8                     58875         123338        +109.49%
BenchmarkConst/sample_size_1000-8                    643018        1390451       +116.24%
BenchmarkContains/sample_size_1-8                    496           1009          +103.43%
BenchmarkContains/sample_size_10-8                   9214          14293         +55.12%
BenchmarkContains/sample_size_100-8                  98430         155046        +57.52%
BenchmarkContains/sample_size_1000-8                 1010793       1635063       +61.76%
BenchmarkDependencies/sample_size_1-8                404           532           +31.68%
BenchmarkDependencies/sample_size_10-8               602           1645          +173.26%
BenchmarkDependencies/sample_size_100-8              2200          13244         +502.00%
BenchmarkDependencies/sample_size_1000-8             26662         119900        +349.70%
BenchmarkEnum/sample_size_1-8                        415           933           +124.82%
BenchmarkEnum/sample_size_10-8                       5131          7833          +52.66%
BenchmarkEnum/sample_size_100-8                      46125         69402         +50.47%
BenchmarkEnum/sample_size_1000-8                     455323        681553        +49.69%
BenchmarkMaximum/sample_size_1-8                     46.2          40.6          -12.12%
BenchmarkMaximum/sample_size_10-8                    46.2          41.0          -11.26%
BenchmarkMaximum/sample_size_100-8                   46.7          40.4          -13.49%
BenchmarkMaximum/sample_size_1000-8                  46.5          41.6          -10.54%
BenchmarkMinimum/sample_size_1-8                     46.7          41.3          -11.56%
BenchmarkMinimum/sample_size_10-8                    46.7          40.8          -12.63%
BenchmarkMinimum/sample_size_100-8                   47.0          41.1          -12.55%
BenchmarkMinimum/sample_size_1000-8                  46.9          41.6          -11.30%
BenchmarkExclusiveMaximum/sample_size_1-8            46.6          40.5          -13.09%
BenchmarkExclusiveMaximum/sample_size_10-8           46.7          40.9          -12.42%
BenchmarkExclusiveMaximum/sample_size_100-8          45.7          41.1          -10.07%
BenchmarkExclusiveMaximum/sample_size_1000-8         46.1          41.6          -9.76%
BenchmarkExclusiveMinimum/sample_size_1-8            45.1          40.1          -11.09%
BenchmarkExclusiveMinimum/sample_size_10-8           46.6          40.2          -13.73%
BenchmarkExclusiveMinimum/sample_size_100-8          44.9          41.3          -8.02%
BenchmarkExclusiveMinimum/sample_size_1000-8         46.0          41.3          -10.22%
BenchmarkMaxItems/sample_size_1-8                    46.0          40.6          -11.74%
BenchmarkMaxItems/sample_size_10-8                   47.1          40.2          -14.65%
BenchmarkMaxItems/sample_size_100-8                  46.3          39.8          -14.04%
BenchmarkMaxItems/sample_size_1000-8                 46.2          40.1          -13.20%
BenchmarkMinItems/sample_size_1-8                    45.7          41.7          -8.75%
BenchmarkMinItems/sample_size_10-8                   46.1          40.5          -12.15%
BenchmarkMinItems/sample_size_100-8                  45.5          40.7          -10.55%
BenchmarkMinItems/sample_size_1000-8                 45.7          39.5          -13.57%
BenchmarkMaxLength/sample_size_1-8                   47.9          43.1          -10.02%
BenchmarkMaxLength/sample_size_10-8                  52.2          47.1          -9.77%
BenchmarkMaxLength/sample_size_100-8                 115           106           -7.83%
BenchmarkMaxLength/sample_size_1000-8                614           565           -7.98%
BenchmarkMinLength/sample_size_1-8                   48.5          42.6          -12.16%
BenchmarkMinLength/sample_size_10-8                  52.5          47.1          -10.29%
BenchmarkMinLength/sample_size_100-8                 117           104           -11.11%
BenchmarkMinLength/sample_size_1000-8                608           580           -4.61%
BenchmarkMaxProperties/sample_size_1-8               45.8          49.4          +7.86%
BenchmarkMaxProperties/sample_size_10-8              46.1          48.8          +5.86%
BenchmarkMaxProperties/sample_size_100-8             45.1          52.4          +16.19%
BenchmarkMaxProperties/sample_size_1000-8            45.3          51.6          +13.91%
BenchmarkMinProperties/sample_size_1-8               45.9          49.2          +7.19%
BenchmarkMinProperties/sample_size_10-8              45.1          49.7          +10.20%
BenchmarkMinProperties/sample_size_100-8             44.8          48.9          +9.15%
BenchmarkMinProperties/sample_size_1000-8            45.0          51.1          +13.56%
BenchmarkMultipleOf/sample_size_1-8                  44.8          43.1          -3.79%
BenchmarkMultipleOf/sample_size_10-8                 46.7          42.1          -9.85%
BenchmarkMultipleOf/sample_size_100-8                46.3          42.4          -8.42%
BenchmarkMultipleOf/sample_size_1000-8               46.1          42.4          -8.03%
BenchmarkPattern/sample_size_1-8                     245           229           -6.53%
BenchmarkPattern/sample_size_10-8                    433           390           -9.93%
BenchmarkPattern/sample_size_100-8                   2184          1840          -15.75%
BenchmarkPattern/sample_size_1000-8                  19968         19207         -3.81%
BenchmarkType/sample_size_1-8                        2233          6459          +189.25%
BenchmarkType/sample_size_10-8                       23057         43821         +90.06%
BenchmarkType/sample_size_100-8                      261737        408302        +56.00%
BenchmarkType/sample_size_1000-8                     2859282       4176650       +46.07%
allocation bench comparison
benchmark                                            old allocs     new allocs     delta
BenchmarkAdditionalItems/sample_size_1-8             5              5              +0.00%
BenchmarkAdditionalItems/sample_size_10-8            32             41             +28.12%
BenchmarkAdditionalItems/sample_size_100-8           302            401            +32.78%
BenchmarkAdditionalItems/sample_size_1000-8          3902           4901           +25.60%
BenchmarkAdditionalProperties/sample_size_1-8        5              16             +220.00%
BenchmarkAdditionalProperties/sample_size_10-8       32             88             +175.00%
BenchmarkAdditionalProperties/sample_size_100-8      302            808            +167.55%
BenchmarkAdditionalProperties/sample_size_1000-8     3002           8008           +166.76%
BenchmarkConst/sample_size_1-8                       13             28             +115.38%
BenchmarkConst/sample_size_10-8                      68             131            +92.65%
BenchmarkConst/sample_size_100-8                     616            1137           +84.58%
BenchmarkConst/sample_size_1000-8                    6037           11082          +83.57%
BenchmarkContains/sample_size_1-8                    3              6              +100.00%
BenchmarkContains/sample_size_10-8                   85             150            +76.47%
BenchmarkContains/sample_size_100-8                  1094           1879           +71.76%
BenchmarkContains/sample_size_1000-8                 10994          18980          +72.64%
BenchmarkDependencies/sample_size_1-8                4              7              +75.00%
BenchmarkDependencies/sample_size_10-8               4              25             +525.00%
BenchmarkDependencies/sample_size_100-8              4              205            +5025.00%
BenchmarkDependencies/sample_size_1000-8             4              2005           +50025.00%
BenchmarkEnum/sample_size_1-8                        2              5              +150.00%
BenchmarkEnum/sample_size_10-8                       42             77             +83.33%
BenchmarkEnum/sample_size_100-8                      483            839            +73.71%
BenchmarkEnum/sample_size_1000-8                     4983           8489           +70.36%
BenchmarkMaximum/sample_size_1-8                     0              0              +0.00%
BenchmarkMaximum/sample_size_10-8                    0              0              +0.00%
BenchmarkMaximum/sample_size_100-8                   0              0              +0.00%
BenchmarkMaximum/sample_size_1000-8                  0              0              +0.00%
BenchmarkMinimum/sample_size_1-8                     0              0              +0.00%
BenchmarkMinimum/sample_size_10-8                    0              0              +0.00%
BenchmarkMinimum/sample_size_100-8                   0              0              +0.00%
BenchmarkMinimum/sample_size_1000-8                  0              0              +0.00%
BenchmarkExclusiveMaximum/sample_size_1-8            0              0              +0.00%
BenchmarkExclusiveMaximum/sample_size_10-8           0              0              +0.00%
BenchmarkExclusiveMaximum/sample_size_100-8          0              0              +0.00%
BenchmarkExclusiveMaximum/sample_size_1000-8         0              0              +0.00%
BenchmarkExclusiveMinimum/sample_size_1-8            0              0              +0.00%
BenchmarkExclusiveMinimum/sample_size_10-8           0              0              +0.00%
BenchmarkExclusiveMinimum/sample_size_100-8          0              0              +0.00%
BenchmarkExclusiveMinimum/sample_size_1000-8         0              0              +0.00%
BenchmarkMaxItems/sample_size_1-8                    0              0              +0.00%
BenchmarkMaxItems/sample_size_10-8                   0              0              +0.00%
BenchmarkMaxItems/sample_size_100-8                  0              0              +0.00%
BenchmarkMaxItems/sample_size_1000-8                 0              0              +0.00%
BenchmarkMinItems/sample_size_1-8                    0              0              +0.00%
BenchmarkMinItems/sample_size_10-8                   0              0              +0.00%
BenchmarkMinItems/sample_size_100-8                  0              0              +0.00%
BenchmarkMinItems/sample_size_1000-8                 0              0              +0.00%
BenchmarkMaxLength/sample_size_1-8                   0              0              +0.00%
BenchmarkMaxLength/sample_size_10-8                  0              0              +0.00%
BenchmarkMaxLength/sample_size_100-8                 0              0              +0.00%
BenchmarkMaxLength/sample_size_1000-8                0              0              +0.00%
BenchmarkMinLength/sample_size_1-8                   0              0              +0.00%
BenchmarkMinLength/sample_size_10-8                  0              0              +0.00%
BenchmarkMinLength/sample_size_100-8                 0              0              +0.00%
BenchmarkMinLength/sample_size_1000-8                0              0              +0.00%
BenchmarkMaxProperties/sample_size_1-8               0              0              +0.00%
BenchmarkMaxProperties/sample_size_10-8              0              0              +0.00%
BenchmarkMaxProperties/sample_size_100-8             0              0              +0.00%
BenchmarkMaxProperties/sample_size_1000-8            0              0              +0.00%
BenchmarkMinProperties/sample_size_1-8               0              0              +0.00%
BenchmarkMinProperties/sample_size_10-8              0              0              +0.00%
BenchmarkMinProperties/sample_size_100-8             0              0              +0.00%
BenchmarkMinProperties/sample_size_1000-8            0              0              +0.00%
BenchmarkMultipleOf/sample_size_1-8                  0              0              +0.00%
BenchmarkMultipleOf/sample_size_10-8                 0              0              +0.00%
BenchmarkMultipleOf/sample_size_100-8                0              0              +0.00%
BenchmarkMultipleOf/sample_size_1000-8               0              0              +0.00%
BenchmarkPattern/sample_size_1-8                     2              2              +0.00%
BenchmarkPattern/sample_size_10-8                    2              2              +0.00%
BenchmarkPattern/sample_size_100-8                   2              2              +0.00%
BenchmarkPattern/sample_size_1000-8                  2              2              +0.00%
BenchmarkType/sample_size_1-8                        19             40             +110.53%
BenchmarkType/sample_size_10-8                       181            364            +101.10%
BenchmarkType/sample_size_100-8                      1801           3604           +100.11%
BenchmarkType/sample_size_1000-8                     18001          36004          +100.01%
bytes bench comparison
benchmark                                            old bytes     new bytes     delta
BenchmarkAdditionalItems/sample_size_1-8             52            52            +0.00%
BenchmarkAdditionalItems/sample_size_10-8            232           336           +44.83%
BenchmarkAdditionalItems/sample_size_100-8           2248          3216          +43.06%
BenchmarkAdditionalItems/sample_size_1000-8          27448         39216         +42.87%
BenchmarkAdditionalProperties/sample_size_1-8        54            4102          +7496.30%
BenchmarkAdditionalProperties/sample_size_10-8       256           5024          +1862.50%
BenchmarkAdditionalProperties/sample_size_100-8      2428          14403         +493.20%
BenchmarkAdditionalProperties/sample_size_1000-8     26613         110580        +315.51%
BenchmarkConst/sample_size_1-8                       720           1520          +111.11%
BenchmarkConst/sample_size_10-8                      2134          4766          +123.34%
BenchmarkConst/sample_size_100-8                     20591         45159         +119.31%
BenchmarkConst/sample_size_1000-8                    267220        580925        +117.40%
BenchmarkContains/sample_size_1-8                    208           392           +88.46%
BenchmarkContains/sample_size_10-8                   3170          5291          +66.91%
BenchmarkContains/sample_size_100-8                  33511         55881         +66.75%
BenchmarkContains/sample_size_1000-8                 336124        568191        +69.04%
BenchmarkDependencies/sample_size_1-8                38            86            +126.32%
BenchmarkDependencies/sample_size_10-8               38            374           +884.21%
BenchmarkDependencies/sample_size_100-8              38            3254          +8463.16%
BenchmarkDependencies/sample_size_1000-8             38            32054         +84252.63%
BenchmarkEnum/sample_size_1-8                        176           360           +104.55%
BenchmarkEnum/sample_size_10-8                       1657          2898          +74.89%
BenchmarkEnum/sample_size_100-8                      15306         26644         +74.08%
BenchmarkEnum/sample_size_1000-8                     152206        264422        +73.73%
BenchmarkMaximum/sample_size_1-8                     0             0             +0.00%
BenchmarkMaximum/sample_size_10-8                    0             0             +0.00%
BenchmarkMaximum/sample_size_100-8                   0             0             +0.00%
BenchmarkMaximum/sample_size_1000-8                  0             0             +0.00%
BenchmarkMinimum/sample_size_1-8                     0             0             +0.00%
BenchmarkMinimum/sample_size_10-8                    0             0             +0.00%
BenchmarkMinimum/sample_size_100-8                   0             0             +0.00%
BenchmarkMinimum/sample_size_1000-8                  0             0             +0.00%
BenchmarkExclusiveMaximum/sample_size_1-8            0             0             +0.00%
BenchmarkExclusiveMaximum/sample_size_10-8           0             0             +0.00%
BenchmarkExclusiveMaximum/sample_size_100-8          0             0             +0.00%
BenchmarkExclusiveMaximum/sample_size_1000-8         0             0             +0.00%
BenchmarkExclusiveMinimum/sample_size_1-8            0             0             +0.00%
BenchmarkExclusiveMinimum/sample_size_10-8           0             0             +0.00%
BenchmarkExclusiveMinimum/sample_size_100-8          0             0             +0.00%
BenchmarkExclusiveMinimum/sample_size_1000-8         0             0             +0.00%
BenchmarkMaxItems/sample_size_1-8                    0             0             +0.00%
BenchmarkMaxItems/sample_size_10-8                   0             0             +0.00%
BenchmarkMaxItems/sample_size_100-8                  0             0             +0.00%
BenchmarkMaxItems/sample_size_1000-8                 0             0             +0.00%
BenchmarkMinItems/sample_size_1-8                    0             0             +0.00%
BenchmarkMinItems/sample_size_10-8                   0             0             +0.00%
BenchmarkMinItems/sample_size_100-8                  0             0             +0.00%
BenchmarkMinItems/sample_size_1000-8                 0             0             +0.00%
BenchmarkMaxLength/sample_size_1-8                   0             0             +0.00%
BenchmarkMaxLength/sample_size_10-8                  0             0             +0.00%
BenchmarkMaxLength/sample_size_100-8                 0             0             +0.00%
BenchmarkMaxLength/sample_size_1000-8                0             0             +0.00%
BenchmarkMinLength/sample_size_1-8                   0             0             +0.00%
BenchmarkMinLength/sample_size_10-8                  0             0             +0.00%
BenchmarkMinLength/sample_size_100-8                 0             0             +0.00%
BenchmarkMinLength/sample_size_1000-8                0             0             +0.00%
BenchmarkMaxProperties/sample_size_1-8               0             0             +0.00%
BenchmarkMaxProperties/sample_size_10-8              0             0             +0.00%
BenchmarkMaxProperties/sample_size_100-8             0             0             +0.00%
BenchmarkMaxProperties/sample_size_1000-8            0             0             +0.00%
BenchmarkMinProperties/sample_size_1-8               0             0             +0.00%
BenchmarkMinProperties/sample_size_10-8              0             0             +0.00%
BenchmarkMinProperties/sample_size_100-8             0             0             +0.00%
BenchmarkMinProperties/sample_size_1000-8            0             0             +0.00%
BenchmarkMultipleOf/sample_size_1-8                  0             0             +0.00%
BenchmarkMultipleOf/sample_size_10-8                 0             0             +0.00%
BenchmarkMultipleOf/sample_size_100-8                0             0             +0.00%
BenchmarkMultipleOf/sample_size_1000-8               0             0             +0.00%
BenchmarkPattern/sample_size_1-8                     152           152           +0.00%
BenchmarkPattern/sample_size_10-8                    160           160           +0.00%
BenchmarkPattern/sample_size_100-8                   256           256           +0.00%
BenchmarkPattern/sample_size_1000-8                  1168          1168          +0.00%
BenchmarkType/sample_size_1-8                        150           2460          +1540.00%
BenchmarkType/sample_size_10-8                       1360          6612          +386.18%
BenchmarkType/sample_size_100-8                      14396         48407         +236.25%
BenchmarkType/sample_size_1000-8                     159515        489146        +206.65%

narrowing in on the BenchmarkType Tests:

benchmark                                            old ns/op     new ns/op     delta
BenchmarkType/sample_size_1-8                        2233          6459          +189.25%
BenchmarkType/sample_size_10-8                       23057         43821         +90.06%
BenchmarkType/sample_size_100-8                      261737        408302        +56.00%
BenchmarkType/sample_size_1000-8                     2859282       4176650       +46.07%

benchmark                                            old allocs     new allocs     delta
BenchmarkType/sample_size_1-8                        19             40             +110.53%
BenchmarkType/sample_size_10-8                       181            364            +101.10%
BenchmarkType/sample_size_100-8                      1801           3604           +100.11%
BenchmarkType/sample_size_1000-8                     18001          36004          +100.01%

benchmark                                            old bytes     new bytes     delta
BenchmarkType/sample_size_1-8                        150           2460          +1540.00%
BenchmarkType/sample_size_10-8                       1360          6612          +386.18%
BenchmarkType/sample_size_100-8                      14396         48407         +236.25%
BenchmarkType/sample_size_1000-8                     159515        489146        +206.65%

Allocations are tracking consistently across sample sizes. Speed seems to be on a collision course as we move up the sample size, which is great to see. In my opinion the value of this feature may be worth the slowdown, so I think the next thing to do is a sanity check of perceived performance slowdowns. a %1500 increase might be just fine when overall execution time is moving from 10 to 15 ms.

dolmen commented 4 years ago

@asido As types.go is independent of the rest of the project (doesn't use anything) and could be useful outside jsonschema, it would be worth considering moving it outside this repo as a standalone library.

b5 commented 4 years ago

given that we've ended up re-implementing this package to support the latest draft of json schema, I'm going to close this PR as stale. The ideas presented here are still very exciting, however.