Skip to content

Commit

Permalink
Don't directly unroll $ref in exhaustive mode (#1282)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Oct 10, 2024
1 parent d7929a2 commit 73275ef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/jsonschema/default_compiler_draft4.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ auto compiler_draft4_core_ref(

if (!is_recursive && direct_children_references <= 5) {
// TODO: Enable this optimization for 2019-09 on-wards
if (schema_context.vocabularies.contains(
"http://json-schema.org/draft-04/schema#") ||
schema_context.vocabularies.contains(
"http://json-schema.org/draft-06/schema#") ||
schema_context.vocabularies.contains(
"http://json-schema.org/draft-07/schema#")) {
if (context.mode == SchemaCompilerMode::FastValidation &&
(schema_context.vocabularies.contains(
"http://json-schema.org/draft-04/schema#") ||
schema_context.vocabularies.contains(
"http://json-schema.org/draft-06/schema#") ||
schema_context.vocabularies.contains(
"http://json-schema.org/draft-07/schema#"))) {
return compile(context, new_schema_context, dynamic_context,
empty_pointer, empty_pointer, reference.destination);
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/jsonschema/default_compiler_draft7.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ auto compiler_draft7_applicator_if(
}

// We handle `then` as part of `if`
// TODO: Stop collapsing this keyword on exhaustive mode for debuggability
// purposes
auto compiler_draft7_applicator_then(const SchemaCompilerContext &,
const SchemaCompilerSchemaContext &,
const SchemaCompilerDynamicContext &)
Expand All @@ -74,6 +76,8 @@ auto compiler_draft7_applicator_then(const SchemaCompilerContext &,
}

// We handle `else` as part of `if`
// TODO: Stop collapsing this keyword on exhaustive mode for debuggability
// purposes
auto compiler_draft7_applicator_else(const SchemaCompilerContext &,
const SchemaCompilerSchemaContext &,
const SchemaCompilerDynamicContext &)
Expand Down
35 changes: 35 additions & 0 deletions test/evaluator/evaluator_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,41 @@ TEST(JSONSchema_evaluator_draft4, ref_2) {
"The string value was expected to validate against the given subschema");
}

TEST(JSONSchema_evaluator_draft4, ref_2_exhaustive) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"allOf": [ { "$ref": "#/definitions/string" } ],
"definitions": {
"string": { "type": "string" }
}
})JSON")};

const sourcemeta::jsontoolkit::JSON instance{"foo"};

EVALUATE_WITH_TRACE_EXHAUSTIVE_SUCCESS(schema, instance, 3);

EVALUATE_TRACE_PRE(0, LogicalAnd, "/allOf", "#/allOf", "");
EVALUATE_TRACE_PRE(1, LogicalAnd, "/allOf/0/$ref", "#/allOf/0/$ref", "");
EVALUATE_TRACE_PRE(2, AssertionTypeStrict, "/allOf/0/$ref/type",
"#/definitions/string/type", "");

EVALUATE_TRACE_POST_SUCCESS(0, AssertionTypeStrict, "/allOf/0/$ref/type",
"#/definitions/string/type", "");
EVALUATE_TRACE_POST_SUCCESS(1, LogicalAnd, "/allOf/0/$ref", "#/allOf/0/$ref",
"");
EVALUATE_TRACE_POST_SUCCESS(2, LogicalAnd, "/allOf", "#/allOf", "");

EVALUATE_TRACE_POST_DESCRIBE(instance, 0,
"The value was expected to be of type string");
EVALUATE_TRACE_POST_DESCRIBE(instance, 1,
"The string value was expected to validate "
"against the statically referenced schema");
EVALUATE_TRACE_POST_DESCRIBE(
instance, 2,
"The string value was expected to validate against the given subschema");
}

TEST(JSONSchema_evaluator_draft4, ref_3) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
Expand Down

4 comments on commit 73275ef

@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 (macos/llvm)

Benchmark suite Current: 73275ef Previous: d7929a2 Ratio
JSON_Array_Of_Objects_Unique 3554.7801552089704 ns/iter 3532.9775363117005 ns/iter 1.01
JSONSchema_Validate_Draft4_Meta_1_No_Callback 742.3011912989036 ns/iter 733.7035222854163 ns/iter 1.01
JSONSchema_Validate_Draft4_Required_Properties 939.9547028686065 ns/iter 941.1993314478193 ns/iter 1.00
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 152.32464566166672 ns/iter 151.44525578483578 ns/iter 1.01
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 105.97448830120834 ns/iter 105.52662040244232 ns/iter 1.00
JSONSchema_Validate_Draft4_Items_Schema 2626.4518507313874 ns/iter 2699.0592012505304 ns/iter 0.97
JSONSchema_Validate_Draft4_Nested_Object 23.499608088678084 ns/iter 22.727402001829955 ns/iter 1.03
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1308.4993993205571 ns/iter 1316.250546748359 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Closed 954.294428293158 ns/iter 968.1358405158121 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Required 1287.322828360174 ns/iter 1281.4382141139556 ns/iter 1.00
JSONSchema_Validate_Draft4_Non_Recursive_Ref 215.02499070223888 ns/iter 212.47696367185648 ns/iter 1.01
JSONSchema_Validate_Draft4_Pattern_Properties_True 1364.558605399449 ns/iter 1349.3413918408087 ns/iter 1.01
JSONSchema_Validate_Draft4_Ref_To_Single_Property 105.7334447150982 ns/iter 107.40701572776767 ns/iter 0.98
JSONSchema_Validate_Draft4_Additional_Properties_Type 407.6268944158721 ns/iter 405.24265704863063 ns/iter 1.01
JSONSchema_Validate_Draft4_Nested_Oneof 359.8783467763029 ns/iter 364.8110968967731 ns/iter 0.99
JSONSchema_Validate_Draft6_Property_Names 774.0832172348311 ns/iter 775.0083705414057 ns/iter 1.00
JSONSchema_Validate_Draft7_If_Then_Else 168.29681188480905 ns/iter 168.98620830876314 ns/iter 1.00
JSONSchema_Compiler_Draft6_AdaptiveCard 2982330957.9999204 ns/iter 2875372917.0000496 ns/iter 1.04

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.

Benchmark (linux/llvm)

Benchmark suite Current: 73275ef Previous: d7929a2 Ratio
JSON_Array_Of_Objects_Unique 2136.352662386968 ns/iter 2135.7396648484587 ns/iter 1.00
JSONSchema_Validate_Draft4_Meta_1_No_Callback 969.6580404212965 ns/iter 956.0751725731602 ns/iter 1.01
JSONSchema_Validate_Draft4_Required_Properties 1624.3589105705537 ns/iter 1605.1569766261202 ns/iter 1.01
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 179.53087584359727 ns/iter 179.4524554466117 ns/iter 1.00
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 131.3788323741565 ns/iter 130.60677402774948 ns/iter 1.01
JSONSchema_Validate_Draft4_Items_Schema 3531.6118436671886 ns/iter 3544.2524505272713 ns/iter 1.00
JSONSchema_Validate_Draft4_Nested_Object 33.069912531345686 ns/iter 33.449290037406946 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1812.212505167662 ns/iter 1812.8220854612528 ns/iter 1.00
JSONSchema_Validate_Draft4_Properties_Triad_Closed 1483.1050044134547 ns/iter 1496.3260140177208 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Required 1874.8941280902843 ns/iter 1897.4478142873231 ns/iter 0.99
JSONSchema_Validate_Draft4_Non_Recursive_Ref 482.039007683336 ns/iter 481.4698865501708 ns/iter 1.00
JSONSchema_Validate_Draft4_Pattern_Properties_True 2458.160970640438 ns/iter 2424.343904452599 ns/iter 1.01
JSONSchema_Validate_Draft4_Ref_To_Single_Property 134.3032565119076 ns/iter 133.14509385951501 ns/iter 1.01
JSONSchema_Validate_Draft4_Additional_Properties_Type 606.1745326558644 ns/iter 605.3871128443448 ns/iter 1.00
JSONSchema_Validate_Draft4_Nested_Oneof 473.9668792029137 ns/iter 473.31565413507064 ns/iter 1.00
JSONSchema_Validate_Draft6_Property_Names 1248.1741195366385 ns/iter 1257.4498797324077 ns/iter 0.99
JSONSchema_Validate_Draft7_If_Then_Else 209.49261367760937 ns/iter 208.93379048145422 ns/iter 1.00
JSONSchema_Compiler_Draft6_AdaptiveCard 5529102521.000027 ns/iter 5495286106.9999695 ns/iter 1.01

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.

Benchmark (linux/gcc)

Benchmark suite Current: 73275ef Previous: d7929a2 Ratio
JSONSchema_Compiler_Draft6_AdaptiveCard 6102350798.999965 ns/iter 6198320409.999951 ns/iter 0.98
JSONSchema_Validate_Draft4_Meta_1_No_Callback 1077.1487209656746 ns/iter 1073.1876285065396 ns/iter 1.00
JSONSchema_Validate_Draft4_Required_Properties 2280.979906777337 ns/iter 2307.4004281950274 ns/iter 0.99
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 190.58389323509502 ns/iter 192.09161641603438 ns/iter 0.99
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 138.89750584691024 ns/iter 135.78709189602924 ns/iter 1.02
JSONSchema_Validate_Draft4_Items_Schema 3097.0873300534845 ns/iter 3112.6289156952926 ns/iter 1.00
JSONSchema_Validate_Draft4_Nested_Object 22.989111889799084 ns/iter 22.413635720226996 ns/iter 1.03
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1652.0168028168343 ns/iter 1721.9589401082628 ns/iter 0.96
JSONSchema_Validate_Draft4_Properties_Triad_Closed 1376.4713264886561 ns/iter 1423.8432937941134 ns/iter 0.97
JSONSchema_Validate_Draft4_Properties_Triad_Required 1734.719536209943 ns/iter 1809.4157465898522 ns/iter 0.96
JSONSchema_Validate_Draft4_Non_Recursive_Ref 453.0170563654138 ns/iter 470.7458828899615 ns/iter 0.96
JSONSchema_Validate_Draft4_Pattern_Properties_True 2205.0763082633066 ns/iter 2333.9745890479694 ns/iter 0.94
JSONSchema_Validate_Draft4_Ref_To_Single_Property 147.05966679118274 ns/iter 142.17822446547484 ns/iter 1.03
JSONSchema_Validate_Draft4_Additional_Properties_Type 1079.0392566520616 ns/iter 1113.3194527245948 ns/iter 0.97
JSONSchema_Validate_Draft4_Nested_Oneof 436.4525220904463 ns/iter 422.7490345732372 ns/iter 1.03
JSONSchema_Validate_Draft6_Property_Names 1724.2800962664235 ns/iter 1771.8267354682396 ns/iter 0.97
JSONSchema_Validate_Draft7_If_Then_Else 195.60148618877773 ns/iter 197.40855727063544 ns/iter 0.99
JSON_Array_Of_Objects_Unique 3179.5933011703196 ns/iter 3267.74614833222 ns/iter 0.97

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.

Benchmark (windows/msvc)

Benchmark suite Current: 73275ef Previous: d7929a2 Ratio
JSON_Array_Of_Objects_Unique 5139.909821429407 ns/iter 5075.467000001481 ns/iter 1.01
JSONSchema_Validate_Draft4_Meta_1_No_Callback 2377.099244308454 ns/iter 2382.4905329348726 ns/iter 1.00
JSONSchema_Validate_Draft4_Required_Properties 2066.59750736309 ns/iter 2068.6705453906825 ns/iter 1.00
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 560.3268749999708 ns/iter 559.255199999825 ns/iter 1.00
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 415.1845216436016 ns/iter 415.2965305672197 ns/iter 1.00
JSONSchema_Validate_Draft4_Items_Schema 6420.2214285700165 ns/iter 6522.911607141119 ns/iter 0.98
JSONSchema_Validate_Draft4_Nested_Object 160.24649553573664 ns/iter 158.75198607713745 ns/iter 1.01
JSONSchema_Validate_Draft4_Properties_Triad_Optional 5296.520535714373 ns/iter 5378.892857143975 ns/iter 0.98
JSONSchema_Validate_Draft4_Properties_Triad_Closed 4362.993750000044 ns/iter 4395.797595851294 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Required 5370.130999999674 ns/iter 5433.4670000002925 ns/iter 0.99
JSONSchema_Validate_Draft4_Non_Recursive_Ref 543.4537499998069 ns/iter 552.7878000000328 ns/iter 0.98
JSONSchema_Validate_Draft4_Pattern_Properties_True 7982.660714286486 ns/iter 8173.260044644439 ns/iter 0.98
JSONSchema_Validate_Draft4_Ref_To_Single_Property 420.86196308102336 ns/iter 420.50397195374035 ns/iter 1.00
JSONSchema_Validate_Draft4_Additional_Properties_Type 742.7904464285413 ns/iter 761.1329241069394 ns/iter 0.98
JSONSchema_Validate_Draft4_Nested_Oneof 1112.667968750003 ns/iter 1120.4892857145003 ns/iter 0.99
JSONSchema_Validate_Draft6_Property_Names 1849.7392408382273 ns/iter 1870.693613374562 ns/iter 0.99
JSONSchema_Validate_Draft7_If_Then_Else 572.7018749999908 ns/iter 573.1597999999849 ns/iter 1.00
JSONSchema_Compiler_Draft6_AdaptiveCard 10029944699.999987 ns/iter 10519105300.000092 ns/iter 0.95

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

Please sign in to comment.