Skip to content

Commit

Permalink
Don't rely on exhaustive flag for LogicalOr (#1289)
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 11, 2024
1 parent 671a503 commit e81ee8d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 33 deletions.
5 changes: 2 additions & 3 deletions src/evaluator/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,8 @@ auto evaluate_step(
for (const auto &child : logical.children) {
if (evaluate_step(child, callback, context)) {
result = true;
// This boolean value controls whether we should still evaluate
// every disjunction even on fast mode
if (!logical.exhaustive && !logical.value) {
// This boolean value controls whether we should be exhaustive
if (!logical.value) {
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/jsonschema/default_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ auto sourcemeta::jsontoolkit::default_schema_compiler(
COMPILE("https://json-schema.org/draft/2020-12/vocab/applicator",
"additionalProperties",
compiler_2019_09_applicator_additionalproperties);
COMPILE("https://json-schema.org/draft/2020-12/vocab/applicator", "anyOf",
compiler_2019_09_applicator_anyof);
COMPILE("https://json-schema.org/draft/2020-12/vocab/unevaluated",
"unevaluatedProperties",
compiler_2019_09_applicator_unevaluatedproperties);
Expand Down Expand Up @@ -129,6 +127,8 @@ auto sourcemeta::jsontoolkit::default_schema_compiler(

COMPILE("https://json-schema.org/draft/2020-12/vocab/applicator", "allOf",
compiler_draft4_applicator_allof);
COMPILE("https://json-schema.org/draft/2020-12/vocab/applicator", "anyOf",
compiler_draft4_applicator_anyof);
COMPILE("https://json-schema.org/draft/2020-12/vocab/applicator", "oneOf",
compiler_draft4_applicator_oneof);
COMPILE("https://json-schema.org/draft/2020-12/vocab/applicator", "not",
Expand Down Expand Up @@ -182,8 +182,6 @@ auto sourcemeta::jsontoolkit::default_schema_compiler(
compiler_2019_09_applicator_items);
COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator",
"additionalItems", compiler_2019_09_applicator_additionalitems);
COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator", "anyOf",
compiler_2019_09_applicator_anyof);
COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator",
"properties", compiler_2019_09_applicator_properties);
COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator",
Expand Down Expand Up @@ -225,6 +223,8 @@ auto sourcemeta::jsontoolkit::default_schema_compiler(

COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator", "allOf",
compiler_draft4_applicator_allof);
COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator", "anyOf",
compiler_draft4_applicator_anyof);
COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator", "oneOf",
compiler_draft4_applicator_oneof);
COMPILE("https://json-schema.org/draft/2019-09/vocab/applicator", "not",
Expand Down
14 changes: 0 additions & 14 deletions src/jsonschema/default_compiler_2019_09.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,6 @@ auto compiler_2019_09_core_recursiveref(
true, context, schema_context, dynamic_context, "")};
}

auto compiler_2019_09_applicator_anyof(
const SchemaCompilerContext &context,
const SchemaCompilerSchemaContext &schema_context,
const SchemaCompilerDynamicContext &dynamic_context)
-> SchemaCompilerTemplate {
return compiler_draft4_applicator_anyof_conditional_exhaustive(
context, schema_context, dynamic_context,
// TODO: This set to true means that every disjunction of `anyOf`
// is always evaluated. In fact, we only need to enable this if
// the schema makes any use of `unevaluatedItems` or
// `unevaluatedProperties`
true);
}

auto compiler_2019_09_applicator_properties(
const SchemaCompilerContext &context,
const SchemaCompilerSchemaContext &schema_context,
Expand Down
19 changes: 7 additions & 12 deletions src/jsonschema/default_compiler_draft4.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ auto compiler_draft4_applicator_allof(
std::move(children))};
}

auto compiler_draft4_applicator_anyof_conditional_exhaustive(
auto compiler_draft4_applicator_anyof(
const SchemaCompilerContext &context,
const SchemaCompilerSchemaContext &schema_context,
const SchemaCompilerDynamicContext &dynamic_context, const bool exhaustive)
const SchemaCompilerDynamicContext &dynamic_context)
-> SchemaCompilerTemplate {
assert(schema_context.schema.at(dynamic_context.keyword).is_array());
assert(!schema_context.schema.at(dynamic_context.keyword).empty());
Expand All @@ -331,18 +331,13 @@ auto compiler_draft4_applicator_anyof_conditional_exhaustive(
{static_cast<Pointer::Token::Index>(index)})));
}

const auto requires_exhaustive{
context.mode == SchemaCompilerMode::Exhaustive ||
context.uses_unevaluated_properties || context.uses_unevaluated_items};

return {make<SchemaCompilerLogicalOr>(
true, context, schema_context, dynamic_context,
SchemaCompilerValueBoolean{exhaustive}, std::move(disjunctors))};
}

auto compiler_draft4_applicator_anyof(
const SchemaCompilerContext &context,
const SchemaCompilerSchemaContext &schema_context,
const SchemaCompilerDynamicContext &dynamic_context)
-> SchemaCompilerTemplate {
return compiler_draft4_applicator_anyof_conditional_exhaustive(
context, schema_context, dynamic_context, false);
SchemaCompilerValueBoolean{requires_exhaustive}, std::move(disjunctors))};
}

auto compiler_draft4_applicator_oneof(
Expand Down

4 comments on commit e81ee8d

@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: e81ee8d Previous: 671a503 Ratio
JSON_Array_Of_Objects_Unique 3790.6076100670916 ns/iter 4270.997331375557 ns/iter 0.89
JSONSchema_Validate_Draft4_Meta_1_No_Callback 785.7815782549118 ns/iter 819.0346488693392 ns/iter 0.96
JSONSchema_Validate_Draft4_Required_Properties 962.7349092331559 ns/iter 1026.368950810276 ns/iter 0.94
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 152.66719635201167 ns/iter 163.01117089037666 ns/iter 0.94
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 104.73793905209541 ns/iter 113.58044846663202 ns/iter 0.92
JSONSchema_Validate_Draft4_Items_Schema 2626.6786192093177 ns/iter 2815.7128890905005 ns/iter 0.93
JSONSchema_Validate_Draft4_Nested_Object 22.74441296508522 ns/iter 24.28533952303458 ns/iter 0.94
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1284.4386312735396 ns/iter 1362.8255087364619 ns/iter 0.94
JSONSchema_Validate_Draft4_Properties_Triad_Closed 998.7294266385628 ns/iter 976.901122083638 ns/iter 1.02
JSONSchema_Validate_Draft4_Properties_Triad_Required 1380.5551699963892 ns/iter 1341.4592927357608 ns/iter 1.03
JSONSchema_Validate_Draft4_Non_Recursive_Ref 226.83082476933222 ns/iter 258.535943206085 ns/iter 0.88
JSONSchema_Validate_Draft4_Pattern_Properties_True 1406.9620198747607 ns/iter 1738.9767288269034 ns/iter 0.81
JSONSchema_Validate_Draft4_Ref_To_Single_Property 118.07772986176279 ns/iter 138.73156232159604 ns/iter 0.85
JSONSchema_Validate_Draft4_Additional_Properties_Type 482.61263555218864 ns/iter 573.5719704675943 ns/iter 0.84
JSONSchema_Validate_Draft4_Nested_Oneof 371.07878973275103 ns/iter 547.4019036243291 ns/iter 0.68
JSONSchema_Validate_Draft6_Property_Names 775.8141289275607 ns/iter 984.0126253259876 ns/iter 0.79
JSONSchema_Validate_Draft7_If_Then_Else 172.74703380579237 ns/iter 186.6469001588933 ns/iter 0.93
JSONSchema_Compiler_Draft6_AdaptiveCard 2924266416.999899 ns/iter 3356218249.9999833 ns/iter 0.87

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: e81ee8d Previous: 671a503 Ratio
JSON_Array_Of_Objects_Unique 2170.3743446640433 ns/iter 2173.2134143557723 ns/iter 1.00
JSONSchema_Validate_Draft4_Meta_1_No_Callback 1026.788488768698 ns/iter 979.3410819526543 ns/iter 1.05
JSONSchema_Validate_Draft4_Required_Properties 1624.1099953682283 ns/iter 1588.8717214413202 ns/iter 1.02
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 195.35793659453046 ns/iter 179.4538996182834 ns/iter 1.09
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 131.2571239207513 ns/iter 130.66353059515342 ns/iter 1.00
JSONSchema_Validate_Draft4_Items_Schema 3801.0493610501057 ns/iter 3507.1594458249906 ns/iter 1.08
JSONSchema_Validate_Draft4_Nested_Object 32.09739736099945 ns/iter 32.54991829237319 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1804.4053999071805 ns/iter 1787.723163582305 ns/iter 1.01
JSONSchema_Validate_Draft4_Properties_Triad_Closed 1505.8608170194593 ns/iter 1500.563687925136 ns/iter 1.00
JSONSchema_Validate_Draft4_Properties_Triad_Required 1891.471382412993 ns/iter 1871.1931806557718 ns/iter 1.01
JSONSchema_Validate_Draft4_Non_Recursive_Ref 480.5282120123193 ns/iter 482.1329415019169 ns/iter 1.00
JSONSchema_Validate_Draft4_Pattern_Properties_True 2443.066355130267 ns/iter 2429.7498096707686 ns/iter 1.01
JSONSchema_Validate_Draft4_Ref_To_Single_Property 138.1487958838616 ns/iter 133.5130175741457 ns/iter 1.03
JSONSchema_Validate_Draft4_Additional_Properties_Type 598.5307973600096 ns/iter 610.2876083325023 ns/iter 0.98
JSONSchema_Validate_Draft4_Nested_Oneof 516.1536588657121 ns/iter 477.1177325521879 ns/iter 1.08
JSONSchema_Validate_Draft6_Property_Names 1301.0700728761556 ns/iter 1262.7927907103801 ns/iter 1.03
JSONSchema_Validate_Draft7_If_Then_Else 222.28742288759543 ns/iter 209.0990445720801 ns/iter 1.06
JSONSchema_Compiler_Draft6_AdaptiveCard 5481160259.000034 ns/iter 5779102297.999998 ns/iter 0.95

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: e81ee8d Previous: 671a503 Ratio
JSONSchema_Compiler_Draft6_AdaptiveCard 6292233868.000039 ns/iter 6134009278.999997 ns/iter 1.03
JSONSchema_Validate_Draft4_Meta_1_No_Callback 1075.6566026785838 ns/iter 1046.4594539673326 ns/iter 1.03
JSONSchema_Validate_Draft4_Required_Properties 2245.5920400407244 ns/iter 2252.6332241611385 ns/iter 1.00
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 192.38172654154445 ns/iter 193.41869447388405 ns/iter 0.99
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 148.47241829363915 ns/iter 141.0571796947764 ns/iter 1.05
JSONSchema_Validate_Draft4_Items_Schema 3079.130438019176 ns/iter 3030.290289394675 ns/iter 1.02
JSONSchema_Validate_Draft4_Nested_Object 23.13862871737906 ns/iter 22.995305214894287 ns/iter 1.01
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1685.262255736356 ns/iter 1728.5034506852576 ns/iter 0.97
JSONSchema_Validate_Draft4_Properties_Triad_Closed 1409.4985657929249 ns/iter 1419.1763149747374 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Required 1805.04348464805 ns/iter 1798.8840605396715 ns/iter 1.00
JSONSchema_Validate_Draft4_Non_Recursive_Ref 468.5879390319951 ns/iter 452.94260355909375 ns/iter 1.03
JSONSchema_Validate_Draft4_Pattern_Properties_True 2273.8316780901328 ns/iter 2299.3216575336032 ns/iter 0.99
JSONSchema_Validate_Draft4_Ref_To_Single_Property 151.67002055432079 ns/iter 147.57867886251103 ns/iter 1.03
JSONSchema_Validate_Draft4_Additional_Properties_Type 1113.410831137125 ns/iter 1070.8250193710508 ns/iter 1.04
JSONSchema_Validate_Draft4_Nested_Oneof 439.5563979367849 ns/iter 429.43585663532616 ns/iter 1.02
JSONSchema_Validate_Draft6_Property_Names 1647.1278889106768 ns/iter 1575.6024097457812 ns/iter 1.05
JSONSchema_Validate_Draft7_If_Then_Else 200.0877910077106 ns/iter 193.0356439334244 ns/iter 1.04
JSON_Array_Of_Objects_Unique 3315.905674450938 ns/iter 3161.4627922331497 ns/iter 1.05

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: e81ee8d Previous: 671a503 Ratio
JSON_Array_Of_Objects_Unique 5236.944000000676 ns/iter 5119.7720000004665 ns/iter 1.02
JSONSchema_Validate_Draft4_Meta_1_No_Callback 2343.1426973854627 ns/iter 2256.135428420247 ns/iter 1.04
JSONSchema_Validate_Draft4_Required_Properties 1999.8253123048767 ns/iter 1970.219520334252 ns/iter 1.02
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 557.5611000001572 ns/iter 551.2734999999793 ns/iter 1.01
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 408.5090799772833 ns/iter 414.2499725781574 ns/iter 0.99
JSONSchema_Validate_Draft4_Items_Schema 6467.5392857144025 ns/iter 6315.163392857568 ns/iter 1.02
JSONSchema_Validate_Draft4_Nested_Object 156.77029017854443 ns/iter 156.46511160716565 ns/iter 1.00
JSONSchema_Validate_Draft4_Properties_Triad_Optional 5476.056250001549 ns/iter 5350.625999999465 ns/iter 1.02
JSONSchema_Validate_Draft4_Properties_Triad_Closed 4499.311898396843 ns/iter 4376.260624999873 ns/iter 1.03
JSONSchema_Validate_Draft4_Properties_Triad_Required 5561.198214286784 ns/iter 5446.33900000008 ns/iter 1.02
JSONSchema_Validate_Draft4_Non_Recursive_Ref 548.3143750000505 ns/iter 544.2524000000049 ns/iter 1.01
JSONSchema_Validate_Draft4_Pattern_Properties_True 8132.5334821436 ns/iter 8074.694196429684 ns/iter 1.01
JSONSchema_Validate_Draft4_Ref_To_Single_Property 414.23689731379795 ns/iter 414.5821719212896 ns/iter 1.00
JSONSchema_Validate_Draft4_Additional_Properties_Type 739.0948660714563 ns/iter 748.6414062500199 ns/iter 0.99
JSONSchema_Validate_Draft4_Nested_Oneof 1110.1135937501283 ns/iter 1061.4582812500828 ns/iter 1.05
JSONSchema_Validate_Draft6_Property_Names 1912.3073574863224 ns/iter 1953.0697795268748 ns/iter 0.98
JSONSchema_Validate_Draft7_If_Then_Else 552.7646428571334 ns/iter 546.7425000000097 ns/iter 1.01
JSONSchema_Compiler_Draft6_AdaptiveCard 10318694499.999992 ns/iter 10135436900.00006 ns/iter 1.02

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

Please sign in to comment.