Skip to content

Commit

Permalink
Simplify unevaluatedItems instruction wrapping (#1080)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Aug 31, 2024
1 parent 5165ffb commit a4a0cbd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/jsonschema/compile_evaluate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1192,9 +1192,11 @@ auto evaluate_step(
context.push(loop);
EVALUATE_CONDITION_GUARD("SchemaCompilerLoopItemsFromAnnotationIndex", loop,
instance);
const auto &target{context.resolve_target<JSON>(loop.target, instance)};
EVALUATE_IMPLICIT_PRECONDITION("SchemaCompilerLoopItemsFromAnnotationIndex",
loop, target.is_array());
CALLBACK_PRE(loop, context.instance_location());
const auto &value{context.resolve_value(loop.value, instance)};
const auto &target{context.resolve_target<JSON>(loop.target, instance)};
assert(target.is_array());
const auto &array{target.as_array()};
result = true;
Expand Down
48 changes: 24 additions & 24 deletions src/jsonschema/default_compiler_2019_09.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,34 +247,35 @@ auto compiler_2019_09_applicator_unevaluateditems(
children = std::move(loop_children);
}

SchemaCompilerTemplate loop;
if (schema_context.vocabularies.contains(
"https://json-schema.org/draft/2019-09/vocab/applicator") &&
dependencies.contains("items")) {
loop.push_back(make<SchemaCompilerLoopItemsFromAnnotationIndex>(
true, context, schema_context, relative_dynamic_context,
SchemaCompilerTemplate condition{make<SchemaCompilerAssertionNoAnnotation>(
false, context, schema_context, relative_dynamic_context, JSON{true},
{}, SchemaCompilerTargetType::Annotations, std::move(dependencies))};
return {make<SchemaCompilerLoopItemsFromAnnotationIndex>(
true, context, schema_context, dynamic_context,
SchemaCompilerValueString{"items"}, std::move(children),
SchemaCompilerTemplate{}));
std::move(condition))};
} else if (schema_context.vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/applicator") &&
dependencies.contains("prefixItems")) {
loop.push_back(make<SchemaCompilerLoopItemsFromAnnotationIndex>(
true, context, schema_context, relative_dynamic_context,
SchemaCompilerTemplate condition{make<SchemaCompilerAssertionNoAnnotation>(
false, context, schema_context, relative_dynamic_context, JSON{true},
{}, SchemaCompilerTargetType::Annotations, std::move(dependencies))};
return {make<SchemaCompilerLoopItemsFromAnnotationIndex>(
true, context, schema_context, dynamic_context,
SchemaCompilerValueString{"prefixItems"}, std::move(children),
SchemaCompilerTemplate{}));
std::move(condition))};
} else {
loop.push_back(make<SchemaCompilerLoopItems>(
true, context, schema_context, relative_dynamic_context,
SchemaCompilerTemplate condition{make<SchemaCompilerAssertionNoAnnotation>(
false, context, schema_context, relative_dynamic_context, JSON{true},
{}, SchemaCompilerTargetType::Annotations, std::move(dependencies))};
return {make<SchemaCompilerLoopItems>(
true, context, schema_context, dynamic_context,
SchemaCompilerValueUnsignedInteger{0}, std::move(children),
SchemaCompilerTemplate{}));
std::move(condition))};
}

SchemaCompilerTemplate condition{make<SchemaCompilerAssertionNoAnnotation>(
false, context, schema_context, relative_dynamic_context, JSON{true}, {},
SchemaCompilerTargetType::Annotations, std::move(dependencies))};
return {make<SchemaCompilerLogicalWhenType>(
false, context, schema_context, dynamic_context, JSON::Type::Array,
std::move(loop), std::move(condition))};
}

auto compiler_2019_09_applicator_unevaluatedproperties(
Expand All @@ -298,20 +299,19 @@ auto compiler_2019_09_applicator_unevaluatedproperties(
dependencies.emplace("additionalProperties");
}

SchemaCompilerTemplate condition{make<SchemaCompilerAssertionNoAnnotation>(
false, context, schema_context, relative_dynamic_context,
SchemaCompilerTarget{SchemaCompilerTargetType::InstanceBasename,
empty_pointer},
{}, SchemaCompilerTargetType::ParentAnnotations,
std::move(dependencies))};

SchemaCompilerTemplate children{compile(context, schema_context,
relative_dynamic_context,
empty_pointer, empty_pointer)};
children.push_back(make<SchemaCompilerAnnotationBasenameToParent>(
true, context, schema_context, relative_dynamic_context,
SchemaCompilerValueNone{}, {}, SchemaCompilerTargetType::Instance));

SchemaCompilerTemplate condition{make<SchemaCompilerAssertionNoAnnotation>(
false, context, schema_context, relative_dynamic_context,
SchemaCompilerTarget{SchemaCompilerTargetType::InstanceBasename,
empty_pointer},
{}, SchemaCompilerTargetType::ParentAnnotations,
std::move(dependencies))};
SchemaCompilerTemplate wrapper{make<SchemaCompilerLogicalAnd>(
false, context, schema_context, relative_dynamic_context,
SchemaCompilerValueNone{}, std::move(children), std::move(condition))};
Expand Down

4 comments on commit a4a0cbd

@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)

Benchmark suite Current: a4a0cbd Previous: 5165ffb Ratio
JSONSchema_Compile_Basic 197034.1969384449 ns/iter 216568.0071695568 ns/iter 0.91
JSONSchema_Validate_Draft4_Meta_1_No_Callback 5988.400992934582 ns/iter 6610.233369352394 ns/iter 0.91
JSONSchema_Validate_Draft4_Required_Properties 3428.6066442324927 ns/iter 3646.1036065253793 ns/iter 0.94
JSONSchema_Validate_Draft4_Optional_Properties_Minimal_Match 1232.906348950351 ns/iter 1295.4398624418723 ns/iter 0.95
JSONSchema_Validate_Draft4_Items_Schema 9455.953181785766 ns/iter 9804.167022549027 ns/iter 0.96

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: a4a0cbd Previous: 5165ffb Ratio
JSONSchema_Compile_Basic 392833.1775754006 ns/iter 388492.1071829152 ns/iter 1.01
JSONSchema_Validate_Draft4_Meta_1_No_Callback 6073.637966411492 ns/iter 6132.665899922391 ns/iter 0.99
JSONSchema_Validate_Draft4_Required_Properties 3426.5693431733707 ns/iter 3342.569075828852 ns/iter 1.03
JSONSchema_Validate_Draft4_Optional_Properties_Minimal_Match 795.527638042247 ns/iter 811.8733566622279 ns/iter 0.98
JSONSchema_Validate_Draft4_Items_Schema 10578.722750274392 ns/iter 10330.8688879653 ns/iter 1.02

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: a4a0cbd Previous: 5165ffb Ratio
JSONSchema_Validate_Draft4_Meta_1_No_Callback 6502.3900247633965 ns/iter 6272.835288828571 ns/iter 1.04
JSONSchema_Validate_Draft4_Required_Properties 3588.421989371823 ns/iter 3495.958208820784 ns/iter 1.03
JSONSchema_Validate_Draft4_Optional_Properties_Minimal_Match 856.364016440521 ns/iter 831.8309535988003 ns/iter 1.03
JSONSchema_Validate_Draft4_Items_Schema 11737.717529793783 ns/iter 12333.739910236303 ns/iter 0.95
JSONSchema_Compile_Basic 385491.1359867286 ns/iter 387044.3375760925 ns/iter 1.00

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)

Benchmark suite Current: a4a0cbd Previous: 5165ffb Ratio
JSONSchema_Compile_Basic 802887.6116070093 ns/iter 844704.5758927121 ns/iter 0.95
JSONSchema_Validate_Draft4_Meta_1_No_Callback 13029.044959619308 ns/iter 13159.48210052974 ns/iter 0.99
JSONSchema_Validate_Draft4_Required_Properties 5558.538392857518 ns/iter 5502.762999999504 ns/iter 1.01
JSONSchema_Validate_Draft4_Optional_Properties_Minimal_Match 2301.035937500018 ns/iter 2289.602187499895 ns/iter 1.00
JSONSchema_Validate_Draft4_Items_Schema 27012.901281689457 ns/iter 27442.621238304007 ns/iter 0.98

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

Please sign in to comment.