From 713d3786b9a2990ee8d231d4dde6a898e0a64ee0 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 15 Nov 2024 22:04:31 -0400 Subject: [PATCH] Improve internal tracing for instruction begin/end (#163) Signed-off-by: Juan Cruz Viotti --- src/evaluator/evaluator.cc | 64 ++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/evaluator/evaluator.cc b/src/evaluator/evaluator.cc index 630dd713..42779b76 100644 --- a/src/evaluator/evaluator.cc +++ b/src/evaluator/evaluator.cc @@ -13,17 +13,16 @@ namespace { auto evaluate_step(const sourcemeta::blaze::Template::value_type &step, const std::optional &callback, sourcemeta::blaze::EvaluationContext &context) -> bool { - SOURCEMETA_TRACE_REGISTER_ID(trace_dispatch_id); + SOURCEMETA_TRACE_REGISTER_ID(trace_begin_id); + SOURCEMETA_TRACE_REGISTER_ID(trace_end_id); SOURCEMETA_TRACE_REGISTER_ID(trace_id); - SOURCEMETA_TRACE_START(trace_dispatch_id, "Dispatch"); + SOURCEMETA_TRACE_START(trace_begin_id, "Begin"); using namespace sourcemeta::jsontoolkit; using namespace sourcemeta::blaze; #define STRINGIFY(x) #x #define EVALUATE_BEGIN(step_category, step_type, precondition) \ - SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch"); \ - SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); \ const auto &step_category{std::get(step)}; \ const auto track{step_category.track || callback.has_value()}; \ context.push(step_category.relative_schema_location, \ @@ -32,18 +31,18 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step, const auto &target{context.resolve_target()}; \ if (!(precondition)) { \ context.pop(step_category.dynamic, track); \ - SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ return true; \ } \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ context.instance_location, context.null); \ } \ - bool result{false}; + bool result{false}; \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ + SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); #define EVALUATE_BEGIN_IF_STRING(step_category, step_type) \ - SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch"); \ - SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); \ const auto &step_category{std::get(step)}; \ const auto track{step_category.track || callback.has_value()}; \ context.push(step_category.relative_schema_location, \ @@ -52,7 +51,7 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step, const auto &maybe_target{context.resolve_string_target()}; \ if (!maybe_target.has_value()) { \ context.pop(step_category.dynamic, track); \ - SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ return true; \ } \ if (callback.has_value()) { \ @@ -60,14 +59,14 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step, context.instance_location, context.null); \ } \ const auto &target{maybe_target.value().get()}; \ - bool result{false}; + bool result{false}; \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ + SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); #define EVALUATE_BEGIN_NO_TARGET(step_category, step_type, precondition) \ - SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch"); \ - SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); \ const auto &step_category{std::get(step)}; \ if (!(precondition)) { \ - SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ return true; \ } \ const auto track{step_category.track || callback.has_value()}; \ @@ -78,25 +77,24 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step, callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ context.instance_location, context.null); \ } \ - bool result{false}; + bool result{false} SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ + SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); // This is a slightly complicated dance to avoid traversing the relative // instance location twice. We first need to traverse it to check if its // valid in the document as part of the condition, but if it is, we can // pass it to `.push()` so that it doesn't need to traverse it again. #define EVALUATE_BEGIN_TRY_TARGET(step_category, step_type, precondition) \ - SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch"); \ - SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); \ const auto &target{context.resolve_target()}; \ const auto &step_category{std::get(step)}; \ if (!(precondition)) { \ - SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ return true; \ } \ auto target_check{ \ try_get(target, step_category.relative_instance_location)}; \ if (!target_check.has_value()) { \ - SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ return true; \ } \ const auto track{step_category.track || callback.has_value()}; \ @@ -108,11 +106,11 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step, callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ context.instance_location, context.null); \ } \ - bool result{false}; + bool result{false}; \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ + SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); #define EVALUATE_BEGIN_NO_PRECONDITION(step_category, step_type) \ - SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch"); \ - SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); \ const auto &step_category{std::get(step)}; \ const auto track{step_category.track || callback.has_value()}; \ context.push(step_category.relative_schema_location, \ @@ -122,41 +120,47 @@ auto evaluate_step(const sourcemeta::blaze::Template::value_type &step, callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ context.instance_location, context.null); \ } \ - bool result{false}; + bool result{false}; \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ + SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); #define EVALUATE_BEGIN_NO_PRECONDITION_AND_NO_PUSH(step_category, step_type) \ - SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch"); \ - SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); \ const auto &step_category{std::get(step)}; \ if (callback.has_value()) { \ callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \ context.instance_location, context.null); \ } \ - bool result{true}; + bool result{true}; \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ + SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); #define EVALUATE_BEGIN_PASS_THROUGH(step_category, step_type) \ - SOURCEMETA_TRACE_END(trace_dispatch_id, "Dispatch"); \ - SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); \ const auto &step_category{std::get(step)}; \ - bool result{true}; + bool result{true}; \ + SOURCEMETA_TRACE_END(trace_begin_id, "Begin"); \ + SOURCEMETA_TRACE_START(trace_id, STRINGIFY(step_type)); #define EVALUATE_END(step_category, step_type) \ + SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_START(trace_end_id, "End"); \ if (callback.has_value()) { \ callback.value()(EvaluationType::Post, result, step, \ context.evaluate_path, context.instance_location, \ context.null); \ } \ context.pop(step_category.dynamic, track); \ - SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_END(trace_end_id, "End"); \ return result; #define EVALUATE_END_NO_POP(step_category, step_type) \ + SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_START(trace_end_id, "End"); \ if (callback.has_value()) { \ callback.value()(EvaluationType::Post, result, step, \ context.evaluate_path, context.instance_location, \ context.null); \ } \ - SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \ + SOURCEMETA_TRACE_END(trace_end_id, "End"); \ return result; #define EVALUATE_END_PASS_THROUGH(step_type) \