Skip to content

Commit

Permalink
Improve internal tracing for instruction begin/end (#163)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Nov 16, 2024
1 parent d28805a commit 713d378
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions src/evaluator/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ namespace {
auto evaluate_step(const sourcemeta::blaze::Template::value_type &step,
const std::optional<sourcemeta::blaze::Callback> &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_type>(step)}; \
const auto track{step_category.track || callback.has_value()}; \
context.push(step_category.relative_schema_location, \
Expand All @@ -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_type>(step)}; \
const auto track{step_category.track || callback.has_value()}; \
context.push(step_category.relative_schema_location, \
Expand All @@ -52,22 +51,22 @@ 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()) { \
callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \
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_type>(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()}; \
Expand All @@ -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_type>(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()}; \
Expand All @@ -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_type>(step)}; \
const auto track{step_category.track || callback.has_value()}; \
context.push(step_category.relative_schema_location, \
Expand All @@ -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_type>(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_type>(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) \
Expand Down

5 comments on commit 713d378

@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: 713d378 Previous: d28805a Ratio
Compiler_Draft6_AdaptiveCard 33117607082.99994 ns/iter 31599791208.000057 ns/iter 1.05
Compiler_2019_09_OMC_JSON_V2 6698535250.000077 ns/iter 6520979875.00008 ns/iter 1.03
Evaluator_Draft4_Meta_1_No_Callback 513.7566081119455 ns/iter 510.40104200001224 ns/iter 1.01
Evaluator_Draft4_Required_Properties 732.0178695020867 ns/iter 859.8521527414026 ns/iter 0.85
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 100.02262072338372 ns/iter 100.11952809065828 ns/iter 1.00
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 65.6954194814718 ns/iter 65.0360351297775 ns/iter 1.01
Evaluator_Draft4_Items_Schema 1216.693778136817 ns/iter 1196.966484325251 ns/iter 1.02
Evaluator_Draft4_Nested_Object 19.791268826037733 ns/iter 19.670139482644473 ns/iter 1.01
Evaluator_Draft4_Properties_Triad_Optional 1170.9814862402102 ns/iter 1133.4636191898876 ns/iter 1.03
Evaluator_Draft4_Properties_Triad_Closed 860.05758686682 ns/iter 831.567366498983 ns/iter 1.03
Evaluator_Draft4_Properties_Triad_Required 1153.1554035528789 ns/iter 1121.2873913608432 ns/iter 1.03
Evaluator_Draft4_Non_Recursive_Ref 48.35639243886681 ns/iter 46.81093742174133 ns/iter 1.03
Evaluator_Draft4_Pattern_Properties_True 1325.5867848583425 ns/iter 1264.3543767441968 ns/iter 1.05
Evaluator_Draft4_Ref_To_Single_Property 66.38268477520465 ns/iter 71.06189343268069 ns/iter 0.93
Evaluator_Draft4_Additional_Properties_Type 63.471075397421764 ns/iter 60.72049280775799 ns/iter 1.05
Evaluator_Draft4_Nested_Oneof 188.2863719115099 ns/iter 181.51907709383076 ns/iter 1.04
Evaluator_Draft6_Property_Names 622.5232904543986 ns/iter 601.7835255037897 ns/iter 1.03
Evaluator_Draft7_If_Then_Else 121.31851762733906 ns/iter 117.40238978296696 ns/iter 1.03
Evaluator_2019_09_Unevaluated_Properties 310.84945731047793 ns/iter 302.1152954148557 ns/iter 1.03
Evaluator_2019_09_OMC_JSON_V2_1 9761.362698959583 ns/iter 8509.75948251163 ns/iter 1.15
Evaluator_2020_12_Dynamic_Ref 1136.601550221143 ns/iter 1053.04869736142 ns/iter 1.08

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: 713d378 Previous: d28805a Ratio
Compiler_Draft6_AdaptiveCard 57287627280.99997 ns/iter 57297939396.00001 ns/iter 1.00
Compiler_2019_09_OMC_JSON_V2 11458632446.000025 ns/iter 11517166198.999973 ns/iter 0.99
Evaluator_Draft4_Meta_1_No_Callback 696.8189807345999 ns/iter 693.3808587906408 ns/iter 1.00
Evaluator_Draft4_Required_Properties 1351.684978050873 ns/iter 1355.1868068777942 ns/iter 1.00
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 128.35457982192702 ns/iter 128.0684629505929 ns/iter 1.00
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 91.49834376954708 ns/iter 91.41100669035815 ns/iter 1.00
Evaluator_Draft4_Items_Schema 1909.28338874922 ns/iter 1873.553281318533 ns/iter 1.02
Evaluator_Draft4_Nested_Object 29.404818268336523 ns/iter 28.215058753613686 ns/iter 1.04
Evaluator_Draft4_Properties_Triad_Optional 1650.3808396593067 ns/iter 1644.0063327993141 ns/iter 1.00
Evaluator_Draft4_Properties_Triad_Closed 1347.2152142077398 ns/iter 1334.5782799851215 ns/iter 1.01
Evaluator_Draft4_Properties_Triad_Required 1699.4523765731133 ns/iter 1707.4274431050683 ns/iter 1.00
Evaluator_Draft4_Non_Recursive_Ref 77.23213969983047 ns/iter 75.93554972782385 ns/iter 1.02
Evaluator_Draft4_Pattern_Properties_True 2380.513313251861 ns/iter 2382.6554201503204 ns/iter 1.00
Evaluator_Draft4_Ref_To_Single_Property 96.78607728621039 ns/iter 96.78120849584938 ns/iter 1.00
Evaluator_Draft4_Additional_Properties_Type 100.55784965427836 ns/iter 100.5263287681897 ns/iter 1.00
Evaluator_Draft4_Nested_Oneof 264.340916173621 ns/iter 263.9238561408143 ns/iter 1.00
Evaluator_Draft6_Property_Names 1077.5165692516694 ns/iter 1115.2128933173851 ns/iter 0.97
Evaluator_Draft7_If_Then_Else 161.29826887730687 ns/iter 159.10360996260286 ns/iter 1.01
Evaluator_2019_09_Unevaluated_Properties 384.7735828385865 ns/iter 390.89952794231283 ns/iter 0.98
Evaluator_2019_09_OMC_JSON_V2_1 12460.806699256009 ns/iter 12474.747699955167 ns/iter 1.00
Evaluator_2020_12_Dynamic_Ref 1317.056432049117 ns/iter 1317.49300074283 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 (macos/gcc)

Benchmark suite Current: 713d378 Previous: d28805a Ratio
Compiler_Draft6_AdaptiveCard 41770253896.71326 ns/iter 50833349943.16101 ns/iter 0.82
Compiler_2019_09_OMC_JSON_V2 8114820003.5095215 ns/iter 10008873939.51416 ns/iter 0.81
Evaluator_Draft4_Meta_1_No_Callback 483.66631665758285 ns/iter 593.8825505928046 ns/iter 0.81
Evaluator_Draft4_Required_Properties 759.5530533780333 ns/iter 935.3415398117619 ns/iter 0.81
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 109.55194981987553 ns/iter 134.32566256315155 ns/iter 0.82
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 77.32787181917548 ns/iter 93.23736003519117 ns/iter 0.83
Evaluator_Draft4_Items_Schema 1237.1965013369318 ns/iter 1354.8891176875322 ns/iter 0.91
Evaluator_Draft4_Nested_Object 23.83016526640211 ns/iter 29.52156450017946 ns/iter 0.81
Evaluator_Draft4_Properties_Triad_Optional 1375.1288862863516 ns/iter 1584.8479339397381 ns/iter 0.87
Evaluator_Draft4_Properties_Triad_Closed 1168.1664218957628 ns/iter 1396.0357202518953 ns/iter 0.84
Evaluator_Draft4_Properties_Triad_Required 1372.1895706811606 ns/iter 1578.7454933354836 ns/iter 0.87
Evaluator_Draft4_Non_Recursive_Ref 60.81093194723088 ns/iter 71.91247080327683 ns/iter 0.85
Evaluator_Draft4_Pattern_Properties_True 1926.436296356255 ns/iter 2064.905186979386 ns/iter 0.93
Evaluator_Draft4_Ref_To_Single_Property 77.65402255262586 ns/iter 85.12244230531532 ns/iter 0.91
Evaluator_Draft4_Additional_Properties_Type 74.56432653590795 ns/iter 83.3603000103107 ns/iter 0.89
Evaluator_Draft4_Nested_Oneof 190.08244736356343 ns/iter 212.31979963363287 ns/iter 0.90
Evaluator_Draft6_Property_Names 810.363082174786 ns/iter 861.4720051661118 ns/iter 0.94
Evaluator_Draft7_If_Then_Else 124.9772965430655 ns/iter 134.49138607787748 ns/iter 0.93
Evaluator_2019_09_Unevaluated_Properties 323.90001733456654 ns/iter 396.6915457226377 ns/iter 0.82
Evaluator_2019_09_OMC_JSON_V2_1 7677.104637488619 ns/iter 8516.513166522835 ns/iter 0.90
Evaluator_2020_12_Dynamic_Ref 1094.3166207743418 ns/iter 1202.6579298774861 ns/iter 0.91

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: 713d378 Previous: d28805a Ratio
Evaluator_2020_12_Dynamic_Ref 1342.2459448166285 ns/iter 1341.9795459013692 ns/iter 1.00
Evaluator_2019_09_Unevaluated_Properties 408.07695331379426 ns/iter 412.31270411110313 ns/iter 0.99
Evaluator_2019_09_OMC_JSON_V2_1 14166.536446032318 ns/iter 14066.623606267727 ns/iter 1.01
Evaluator_Draft7_If_Then_Else 146.9042662845518 ns/iter 146.88065398913568 ns/iter 1.00
Evaluator_Draft6_Property_Names 1544.4849388511327 ns/iter 1549.9339014451402 ns/iter 1.00
Evaluator_Draft4_Meta_1_No_Callback 782.7316056331518 ns/iter 768.5608730853709 ns/iter 1.02
Evaluator_Draft4_Required_Properties 1978.5816863790603 ns/iter 2007.257761881421 ns/iter 0.99
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 133.4981706808087 ns/iter 135.08209006349915 ns/iter 0.99
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 98.80365012383797 ns/iter 105.4457010612771 ns/iter 0.94
Evaluator_Draft4_Items_Schema 1536.0511294971902 ns/iter 1534.973007157 ns/iter 1.00
Evaluator_Draft4_Nested_Object 26.434745291335446 ns/iter 27.619232913884566 ns/iter 0.96
Evaluator_Draft4_Properties_Triad_Optional 1583.4817766010676 ns/iter 1618.6531852012179 ns/iter 0.98
Evaluator_Draft4_Properties_Triad_Closed 1287.1567339093253 ns/iter 1317.9555510110572 ns/iter 0.98
Evaluator_Draft4_Properties_Triad_Required 1627.3668966830842 ns/iter 1664.4322293970818 ns/iter 0.98
Evaluator_Draft4_Non_Recursive_Ref 95.64883475171723 ns/iter 95.58171235954265 ns/iter 1.00
Evaluator_Draft4_Pattern_Properties_True 2234.5607472163297 ns/iter 2237.48907452557 ns/iter 1.00
Evaluator_Draft4_Ref_To_Single_Property 102.28481474385143 ns/iter 105.36842538566519 ns/iter 0.97
Evaluator_Draft4_Additional_Properties_Type 149.40032786639998 ns/iter 149.23071626502968 ns/iter 1.00
Evaluator_Draft4_Nested_Oneof 235.5991678525705 ns/iter 239.32604244540337 ns/iter 0.98
Compiler_2019_09_OMC_JSON_V2 11275233382.00001 ns/iter 11529369581.000082 ns/iter 0.98
Compiler_Draft6_AdaptiveCard 64919352965.00003 ns/iter 67187179626.99998 ns/iter 0.97

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: 713d378 Previous: d28805a Ratio
Compiler_Draft6_AdaptiveCard 123719615700.00008 ns/iter 124311104000 ns/iter 1.00
Compiler_2019_09_OMC_JSON_V2 23923544300.000004 ns/iter 24192356400.000107 ns/iter 0.99
Evaluator_Draft4_Meta_1_No_Callback 1525.0390625004075 ns/iter 1517.787331839019 ns/iter 1.00
Evaluator_Draft4_Required_Properties 1498.9017857145777 ns/iter 1477.0841517857443 ns/iter 1.01
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 353.0446764751757 ns/iter 353.6894189840156 ns/iter 1.00
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 250.19089285714404 ns/iter 248.6488928571719 ns/iter 1.01
Evaluator_Draft4_Items_Schema 3238.890625000173 ns/iter 3234.766773072096 ns/iter 1.00
Evaluator_Draft4_Nested_Object 113.56739062499345 ns/iter 111.6220781249666 ns/iter 1.02
Evaluator_Draft4_Properties_Triad_Optional 5129.867000000559 ns/iter 5107.035999999425 ns/iter 1.00
Evaluator_Draft4_Properties_Triad_Closed 4147.655581403531 ns/iter 4144.2118775843655 ns/iter 1.00
Evaluator_Draft4_Properties_Triad_Required 5059.966000001168 ns/iter 5052.76399999957 ns/iter 1.00
Evaluator_Draft4_Non_Recursive_Ref 261.0234759385007 ns/iter 256.1977904552423 ns/iter 1.02
Evaluator_Draft4_Pattern_Properties_True 8013.911830356031 ns/iter 8091.5870535694785 ns/iter 0.99
Evaluator_Draft4_Ref_To_Single_Property 255.51486854984392 ns/iter 254.37849999994862 ns/iter 1.00
Evaluator_Draft4_Additional_Properties_Type 343.4161574256636 ns/iter 341.6728190113698 ns/iter 1.01
Evaluator_Draft4_Nested_Oneof 564.9666999997862 ns/iter 563.6285714284587 ns/iter 1.00
Evaluator_Draft6_Property_Names 1727.2284192667405 ns/iter 1725.009760037848 ns/iter 1.00
Evaluator_Draft7_If_Then_Else 389.07059151784193 ns/iter 384.45479910720337 ns/iter 1.01
Evaluator_2019_09_Unevaluated_Properties 978.502343750165 ns/iter 960.7463568096808 ns/iter 1.02
Evaluator_2019_09_OMC_JSON_V2_1 18508.740256609934 ns/iter 18749.358713943533 ns/iter 0.99
Evaluator_2020_12_Dynamic_Ref 3080.3141792024167 ns/iter 3069.323660714726 ns/iter 1.00

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

Please sign in to comment.