Skip to content

Commit

Permalink
Reduce generated steps for items schemas before 2019-09 (#1063)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti authored Aug 29, 2024
1 parent 0e5e3e2 commit ddc6917
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/jsonschema/compile_evaluate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,11 @@ auto evaluate_step(
const auto &loop{std::get<SchemaCompilerLoopItems>(step)};
context.push(loop);
EVALUATE_CONDITION_GUARD("SchemaCompilerLoopItems", loop, instance);
const auto &target{context.resolve_target<JSON>(loop.target, instance)};
EVALUATE_IMPLICIT_PRECONDITION("SchemaCompilerLoopItems", 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;
auto iterator{array.cbegin()};
Expand Down
40 changes: 23 additions & 17 deletions src/jsonschema/default_compiler_draft4.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,29 +658,35 @@ auto compiler_draft4_applicator_items_conditional_annotation(
const SchemaCompilerDynamicContext &dynamic_context,
const bool annotate) -> SchemaCompilerTemplate {
if (is_schema(schema_context.schema.at(dynamic_context.keyword))) {
SchemaCompilerTemplate children;
children.push_back(make<SchemaCompilerLoopItems>(
true, context, schema_context, relative_dynamic_context,
SchemaCompilerValueUnsignedInteger{0},
compile(context, schema_context, relative_dynamic_context,
empty_pointer, empty_pointer),
SchemaCompilerTemplate{}));

if (annotate) {
SchemaCompilerTemplate children;
children.push_back(make<SchemaCompilerLoopItems>(
true, context, schema_context, relative_dynamic_context,
SchemaCompilerValueUnsignedInteger{0},
compile(context, schema_context, relative_dynamic_context,
empty_pointer, empty_pointer),
SchemaCompilerTemplate{}));
children.push_back(make<SchemaCompilerAnnotationEmit>(
true, context, schema_context, relative_dynamic_context, JSON{true},
{}, SchemaCompilerTargetType::Instance));

// TODO: We could get rid of the precondition here if we extend
// LogicalAnd, which currently doesn't take any value, to take a
// conditional type as a value and then use it as an implicit precondition
return {make<SchemaCompilerLogicalAnd>(
false, context, schema_context, dynamic_context,
SchemaCompilerValueNone{}, std::move(children),
{make<SchemaCompilerAssertionTypeStrict>(
true, context, schema_context, relative_dynamic_context,
JSON::Type::Array, {}, SchemaCompilerTargetType::Instance)})};
}

// TODO: We could get rid of the precondition here if we extend LogicalAnd,
// which currently doesn't take any value, to take a conditional type
// as a value and then use it as an implicit precondition
return {make<SchemaCompilerLogicalAnd>(
false, context, schema_context, dynamic_context,
SchemaCompilerValueNone{}, std::move(children),
{make<SchemaCompilerAssertionTypeStrict>(
true, context, schema_context, relative_dynamic_context,
JSON::Type::Array, {}, SchemaCompilerTargetType::Instance)})};
return {make<SchemaCompilerLoopItems>(
true, context, schema_context, dynamic_context,
SchemaCompilerValueUnsignedInteger{0},
compile(context, schema_context, relative_dynamic_context,
empty_pointer, empty_pointer),
SchemaCompilerTemplate{})};
}

return compiler_draft4_applicator_items_array(context, schema_context,
Expand Down

4 comments on commit ddc6917

@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: ddc6917 Previous: 0e5e3e2 Ratio
JSONSchema_Compile_Basic 231541.3690360341 ns/iter 213199.14829657608 ns/iter 1.09
JSONSchema_Validate_Draft4_Meta_1_No_Callback 9665.826947852864 ns/iter 8748.517046228002 ns/iter 1.10
JSONSchema_Validate_Draft4_Required_Properties 3666.7415151626155 ns/iter 3407.901723666532 ns/iter 1.08
JSONSchema_Validate_Draft4_Items_Schema 10233.30795099022 ns/iter 10913.11946916623 ns/iter 0.94

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: ddc6917 Previous: 0e5e3e2 Ratio
JSONSchema_Compile_Basic 397076.81792073854 ns/iter 391118.3772954927 ns/iter 1.02
JSONSchema_Validate_Draft4_Meta_1_No_Callback 12209.443939526645 ns/iter 12544.142768686295 ns/iter 0.97
JSONSchema_Validate_Draft4_Required_Properties 3275.0369452681593 ns/iter 3307.3900433451104 ns/iter 0.99
JSONSchema_Validate_Draft4_Items_Schema 10562.578523530965 ns/iter 13957.481800594996 ns/iter 0.76

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: ddc6917 Previous: 0e5e3e2 Ratio
JSONSchema_Validate_Draft4_Meta_1_No_Callback 12262.226449983362 ns/iter 12616.912451742533 ns/iter 0.97
JSONSchema_Validate_Draft4_Required_Properties 3428.1304547270774 ns/iter 3432.5377427353815 ns/iter 1.00
JSONSchema_Validate_Draft4_Items_Schema 12655.551537957925 ns/iter 15278.06068745553 ns/iter 0.83
JSONSchema_Compile_Basic 408999.7354466903 ns/iter 397792.2929010526 ns/iter 1.03

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: ddc6917 Previous: 0e5e3e2 Ratio
JSONSchema_Compile_Basic 788483.4821427132 ns/iter 798743.5267858041 ns/iter 0.99
JSONSchema_Validate_Draft4_Meta_1_No_Callback 23651.071428576288 ns/iter 23167.445675829702 ns/iter 1.02
JSONSchema_Validate_Draft4_Required_Properties 5190.574999999171 ns/iter 5332.669642857216 ns/iter 0.97
JSONSchema_Validate_Draft4_Items_Schema 25595.279474819272 ns/iter 30950.928571427434 ns/iter 0.83

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

Please sign in to comment.