From ecedb9adc3d87b47ef3260b5629f4d5cac16780d Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 8 Jul 2026 13:55:39 -0300 Subject: [PATCH] Benchmark annotation dropping in `SimpleOutput` Signed-off-by: Juan Cruz Viotti --- benchmark/micro/2020_12.cc | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/benchmark/micro/2020_12.cc b/benchmark/micro/2020_12.cc index 2c57265c2..1639cb19f 100644 --- a/benchmark/micro/2020_12.cc +++ b/benchmark/micro/2020_12.cc @@ -1,6 +1,8 @@ #include #include // assert +#include // std::size_t +#include // std::int64_t #include // std::filesystem::path #include @@ -214,6 +216,84 @@ static void Micro_2020_12_Simple_Output_Annotations(benchmark::State &state) { } } +static void +Micro_2020_12_Simple_Output_Annotation_Dropping(benchmark::State &state) { + const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "array", + "items": { + "oneOf": [ + { + "type": "string", + "title": "String branch", + "description": "This branch matches string values" + }, + { + "type": "integer", + "title": "Integer branch", + "description": "This branch matches integer values" + }, + { + "type": "boolean", + "title": "Boolean branch", + "description": "This branch matches boolean values" + }, + { + "type": "object", + "title": "Object branch", + "description": "This branch matches object values" + }, + { + "type": "array", + "title": "Array branch", + "description": "This branch matches array values" + } + ] + } + })JSON")}; + + // Every element matches exactly one branch, so the array validates, but the + // four losing branches each collect a title and a description annotation that + // are then dropped. Exhaustive mode forces every branch to run, so this + // stresses the annotation collection and retraction path heavily + auto instance{sourcemeta::core::JSON::make_array()}; + for (std::size_t index = 0; index < 512; index += 1) { + switch (index % 5) { + case 0: + instance.push_back( + sourcemeta::core::JSON{sourcemeta::core::JSON::String{"text"}}); + break; + case 1: + instance.push_back( + sourcemeta::core::JSON{static_cast(index)}); + break; + case 2: + instance.push_back(sourcemeta::core::JSON{index % 2 == 0}); + break; + case 3: + instance.push_back(sourcemeta::core::JSON::make_object()); + break; + default: + instance.push_back(sourcemeta::core::JSON::make_array()); + break; + } + } + + const auto schema_template{ + sourcemeta::blaze::compile(schema, sourcemeta::blaze::schema_walker, + sourcemeta::blaze::schema_resolver, + sourcemeta::blaze::default_schema_compiler, + sourcemeta::blaze::Mode::Exhaustive)}; + sourcemeta::blaze::Evaluator evaluator; + for (auto _ : state) { + sourcemeta::blaze::SimpleOutput output{instance}; + auto result{ + evaluator.validate(schema_template, instance, std::ref(output))}; + assert(result); + benchmark::DoNotOptimize(result); + } +} + static void Micro_2020_12_Compile_NonCircular_Shared_Refs(benchmark::State &state) { const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({ @@ -397,6 +477,7 @@ BENCHMARK(Micro_2020_12_Dynamic_Ref); BENCHMARK(Micro_2020_12_Dynamic_Ref_Single); BENCHMARK(Micro_2020_12_Simple_Output_Mask); BENCHMARK(Micro_2020_12_Simple_Output_Annotations); +BENCHMARK(Micro_2020_12_Simple_Output_Annotation_Dropping); BENCHMARK(Micro_2020_12_Compile_NonCircular_Shared_Refs); BENCHMARK(Micro_2020_12_Exhaustive_Deep_Numeric); BENCHMARK(Micro_2020_12_Exhaustive_Deep_Numeric_SimpleOutput);