Skip to content

Commit

Permalink
Maintain common static JSON values in EvaluationContext (#203)
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 21, 2024
1 parent 16f9744 commit acc801f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/evaluator/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace sourcemeta::blaze {

const sourcemeta::jsontoolkit::JSON EvaluationContext::null{nullptr};
const sourcemeta::jsontoolkit::JSON EvaluationContext::empty_string{""};

// TODO: Completely inline push/pop

auto EvaluationContext::push(
Expand Down
4 changes: 2 additions & 2 deletions src/evaluator/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,10 @@ HANDLER_START {
auto destination = context.instance_location;
destination.push_back(control.value);
callback.value()(EvaluationType::Pre, true, step, context.evaluate_path,
destination, context.null);
destination, EvaluationContext::null);
context.evaluate(control.value);
callback.value()(EvaluationType::Post, true, step, context.evaluate_path,
destination, context.null);
destination, EvaluationContext::null);
} else {
context.evaluate(control.value);
}
Expand Down
19 changes: 9 additions & 10 deletions src/evaluator/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ inline auto resolve_target(
// to cope with non-string keywords inside `propertyNames`
// that need to fail validation. But then, the actual string
// we return doesn't matter, so we can always return a dummy one.
static const sourcemeta::jsontoolkit::JSON empty_string{""};
return empty_string;
return sourcemeta::blaze::EvaluationContext::empty_string;
}

return instance;
Expand Down Expand Up @@ -101,7 +100,7 @@ auto evaluate_step(
} \
if (callback.has_value()) { \
callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \
context.instance_location, context.null); \
context.instance_location, EvaluationContext::null); \
} \
bool result{false};

Expand All @@ -127,7 +126,7 @@ auto evaluate_step(
} \
if (callback.has_value()) { \
callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \
context.instance_location, context.null); \
context.instance_location, EvaluationContext::null); \
} \
const auto &target{maybe_target.value().get()}; \
bool result{false};
Expand Down Expand Up @@ -159,7 +158,7 @@ auto evaluate_step(
assert(!step_category.relative_instance_location.empty()); \
if (callback.has_value()) { \
callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \
context.instance_location, context.null); \
context.instance_location, EvaluationContext::null); \
} \
bool result{false};

Expand All @@ -174,7 +173,7 @@ auto evaluate_step(
} \
if (callback.has_value()) { \
callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \
context.instance_location, context.null); \
context.instance_location, EvaluationContext::null); \
} \
bool result{false};

Expand All @@ -183,7 +182,7 @@ auto evaluate_step(
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); \
context.instance_location, EvaluationContext::null); \
} \
bool result{true};

Expand All @@ -196,7 +195,7 @@ auto evaluate_step(
if (callback.has_value()) { \
callback.value()(EvaluationType::Post, result, step, \
context.evaluate_path, context.instance_location, \
context.null); \
EvaluationContext::null); \
} \
context.pop(step_category.relative_schema_location.size(), \
step_category.relative_instance_location.size(), track); \
Expand All @@ -210,7 +209,7 @@ auto evaluate_step(
if (callback.has_value()) { \
callback.value()(EvaluationType::Post, result, step, \
context.evaluate_path, context.instance_location, \
context.null); \
EvaluationContext::null); \
} \
SOURCEMETA_TRACE_END(trace_id, STRINGIFY(step_type)); \
return result;
Expand All @@ -229,7 +228,7 @@ auto evaluate_step(
step_category.relative_instance_location, track); \
if (callback.has_value()) { \
callback.value()(EvaluationType::Pre, true, step, context.evaluate_path, \
destination, context.null); \
destination, EvaluationContext::null); \
callback.value()(EvaluationType::Post, true, step, context.evaluate_path, \
destination, annotation_value); \
} \
Expand Down
3 changes: 2 additions & 1 deletion src/evaluator/include/sourcemeta/blaze/evaluator_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class SOURCEMETA_BLAZE_EVALUATOR_EXPORT EvaluationContext {
-> bool;
auto unevaluate() -> void;

const sourcemeta::jsontoolkit::JSON null{nullptr};
static const sourcemeta::jsontoolkit::JSON null;
static const sourcemeta::jsontoolkit::JSON empty_string;

// Exporting symbols that depends on the standard C++ library is considered
// safe.
Expand Down

5 comments on commit acc801f

@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: acc801f Previous: 16f9744 Ratio
Compiler_Draft6_AdaptiveCard 37025597375.00008 ns/iter 37231452750.000015 ns/iter 0.99
Compiler_2019_09_OMC_JSON_V2 8880079582.999996 ns/iter 7642631583.000025 ns/iter 1.16
Evaluator_Draft4_Meta_1_No_Callback 370.15430680133716 ns/iter 296.3929650773176 ns/iter 1.25
Evaluator_Draft4_Required_Properties 400.5554021947569 ns/iter 585.7929160212107 ns/iter 0.68
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 27.063883823156377 ns/iter 24.666403406138098 ns/iter 1.10
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 12.100931966333993 ns/iter 14.650470543992911 ns/iter 0.83
Evaluator_Draft4_Items_Schema 784.558049470951 ns/iter 795.4848537542521 ns/iter 0.99
Evaluator_Draft4_Nested_Object 1.6442799359304776 ns/iter 1.6060780259278136 ns/iter 1.02
Evaluator_Draft4_Properties_Triad_Optional 1150.2834132155895 ns/iter 1138.4666988772044 ns/iter 1.01
Evaluator_Draft4_Properties_Triad_Closed 838.865896899604 ns/iter 799.5393510329767 ns/iter 1.05
Evaluator_Draft4_Properties_Triad_Required 1155.1430050511315 ns/iter 1143.888460376355 ns/iter 1.01
Evaluator_Draft4_Properties_Closed 103.89729811883957 ns/iter 105.951748196467 ns/iter 0.98
Evaluator_Draft4_Non_Recursive_Ref 13.542604537161393 ns/iter 12.873556569939865 ns/iter 1.05
Evaluator_Draft4_Pattern_Properties_True 1354.4873850483061 ns/iter 1516.1292771390824 ns/iter 0.89
Evaluator_Draft4_Ref_To_Single_Property 12.887720633180765 ns/iter 17.084054430929047 ns/iter 0.75
Evaluator_Draft4_Additional_Properties_Type 29.13321815011551 ns/iter 24.295870620059105 ns/iter 1.20
Evaluator_Draft4_Nested_Oneof 96.88867798083832 ns/iter 91.93584388860259 ns/iter 1.05
Evaluator_Draft4_Long_Enum 23.230316211794886 ns/iter 21.911566174952114 ns/iter 1.06
Evaluator_Draft4_Type_Object 6.991376490962309 ns/iter 6.633419767634595 ns/iter 1.05
Evaluator_Draft6_Property_Names 313.2354168512261 ns/iter 295.5523514117739 ns/iter 1.06
Evaluator_Draft7_If_Then_Else 43.094047140348984 ns/iter 40.81415207892314 ns/iter 1.06
Evaluator_Draft7_Vercel_1 101928.91634298988 ns/iter 102734.95797078637 ns/iter 0.99
Evaluator_2019_09_Unevaluated_Properties 163.56009844859224 ns/iter 159.97347667115034 ns/iter 1.02
Evaluator_2019_09_OMC_JSON_V2_1 7512.394399721322 ns/iter 8276.957502816686 ns/iter 0.91
Evaluator_2020_12_Dynamic_Ref 579.1575656271015 ns/iter 586.482005057643 ns/iter 0.99

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: acc801f Previous: 16f9744 Ratio
Compiler_Draft6_AdaptiveCard 119231063699.99994 ns/iter 117694351900.00002 ns/iter 1.01
Compiler_2019_09_OMC_JSON_V2 25221795100.000008 ns/iter 25111846100.00009 ns/iter 1.00
Evaluator_Draft4_Meta_1_No_Callback 841.911941964289 ns/iter 820.4536830356902 ns/iter 1.03
Evaluator_Draft4_Required_Properties 1301.975177689725 ns/iter 1311.1879191127123 ns/iter 0.99
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 82.95674107142555 ns/iter 81.99416294643365 ns/iter 1.01
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 41.58990701452946 ns/iter 42.01269204879766 ns/iter 0.99
Evaluator_Draft4_Items_Schema 2698.8695741264787 ns/iter 2594.23535714315 ns/iter 1.04
Evaluator_Draft4_Nested_Object 2.813447898017909 ns/iter 2.1556406249999327 ns/iter 1.31
Evaluator_Draft4_Properties_Triad_Optional 4744.728894484019 ns/iter 4731.931321274954 ns/iter 1.00
Evaluator_Draft4_Properties_Triad_Closed 3815.946578666879 ns/iter 3812.22544642869 ns/iter 1.00
Evaluator_Draft4_Properties_Triad_Required 4774.456807551196 ns/iter 4730.61737108757 ns/iter 1.01
Evaluator_Draft4_Properties_Closed 288.53146556566537 ns/iter 280.58789725580823 ns/iter 1.03
Evaluator_Draft4_Non_Recursive_Ref 94.56424131408544 ns/iter 93.83048688255313 ns/iter 1.01
Evaluator_Draft4_Pattern_Properties_True 7693.456473214511 ns/iter 7534.040178570781 ns/iter 1.02
Evaluator_Draft4_Ref_To_Single_Property 42.293443827614006 ns/iter 44.68296656533049 ns/iter 0.95
Evaluator_Draft4_Additional_Properties_Type 180.0567776823758 ns/iter 176.7402037995828 ns/iter 1.02
Evaluator_Draft4_Nested_Oneof 312.4523214285294 ns/iter 295.81711653825346 ns/iter 1.06
Evaluator_Draft4_Long_Enum 56.6175357142892 ns/iter 58.314982142856515 ns/iter 0.97
Evaluator_Draft4_Type_Object 23.550090138948445 ns/iter 21.401343750000024 ns/iter 1.10
Evaluator_Draft6_Property_Names 1126.5165624999795 ns/iter 1042.9732812500702 ns/iter 1.08
Evaluator_Draft7_If_Then_Else 122.40753571429002 ns/iter 117.41626562498553 ns/iter 1.04
Evaluator_Draft7_Vercel_1 241702.54435889196 ns/iter 236935.4536323773 ns/iter 1.02
Evaluator_2019_09_Unevaluated_Properties 450.8637207508093 ns/iter 433.8115624999972 ns/iter 1.04
Evaluator_2019_09_OMC_JSON_V2_1 16121.381696428167 ns/iter 15390.00223214515 ns/iter 1.05
Evaluator_2020_12_Dynamic_Ref 1634.8834811048785 ns/iter 1562.165178571868 ns/iter 1.05

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: acc801f Previous: 16f9744 Ratio
Compiler_Draft6_AdaptiveCard 57880214162.000015 ns/iter 57872197084.999985 ns/iter 1.00
Compiler_2019_09_OMC_JSON_V2 11004294662.999996 ns/iter 11122766262.000027 ns/iter 0.99
Evaluator_Draft4_Meta_1_No_Callback 472.99551097211014 ns/iter 459.62358453499934 ns/iter 1.03
Evaluator_Draft4_Required_Properties 894.5199188277011 ns/iter 952.3574116982912 ns/iter 0.94
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 49.83272061913577 ns/iter 47.9662348596848 ns/iter 1.04
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 29.160639443985552 ns/iter 30.63031271065348 ns/iter 0.95
Evaluator_Draft4_Items_Schema 1456.5726003407497 ns/iter 1479.5507886928983 ns/iter 0.98
Evaluator_Draft4_Nested_Object 4.950743098756933 ns/iter 5.5178214080464265 ns/iter 0.90
Evaluator_Draft4_Properties_Triad_Optional 1297.3808133488035 ns/iter 1353.9504521863391 ns/iter 0.96
Evaluator_Draft4_Properties_Triad_Closed 1024.0720243936182 ns/iter 1038.7970535576924 ns/iter 0.99
Evaluator_Draft4_Properties_Triad_Required 1337.130646212691 ns/iter 1384.3913004272713 ns/iter 0.97
Evaluator_Draft4_Properties_Closed 171.6045213859255 ns/iter 162.7838178029617 ns/iter 1.05
Evaluator_Draft4_Non_Recursive_Ref 39.89016283855093 ns/iter 38.65172016609823 ns/iter 1.03
Evaluator_Draft4_Pattern_Properties_True 2031.674244400487 ns/iter 2086.9786169285017 ns/iter 0.97
Evaluator_Draft4_Ref_To_Single_Property 28.796616397017637 ns/iter 30.666684073415 ns/iter 0.94
Evaluator_Draft4_Additional_Properties_Type 70.14980160460932 ns/iter 67.39952060946388 ns/iter 1.04
Evaluator_Draft4_Nested_Oneof 184.14120869153035 ns/iter 173.75177613750625 ns/iter 1.06
Evaluator_Draft4_Long_Enum 27.889389014238215 ns/iter 26.614530323171948 ns/iter 1.05
Evaluator_Draft4_Type_Object 17.015000867510278 ns/iter 16.125062112429664 ns/iter 1.06
Evaluator_Draft6_Property_Names 630.1136428593205 ns/iter 586.298711182637 ns/iter 1.07
Evaluator_Draft7_If_Then_Else 84.32898808208243 ns/iter 80.8411852597202 ns/iter 1.04
Evaluator_Draft7_Vercel_1 142100.437993117 ns/iter 138901.5941655247 ns/iter 1.02
Evaluator_2019_09_Unevaluated_Properties 220.7304332453824 ns/iter 225.2897258999627 ns/iter 0.98
Evaluator_2019_09_OMC_JSON_V2_1 10447.596592090307 ns/iter 10149.913385480646 ns/iter 1.03
Evaluator_2020_12_Dynamic_Ref 1015.2641745027734 ns/iter 1009.3576777827276 ns/iter 1.01

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: acc801f Previous: 16f9744 Ratio
Compiler_Draft6_AdaptiveCard 44964429855.34668 ns/iter 45626974821.0907 ns/iter 0.99
Compiler_2019_09_OMC_JSON_V2 9027891159.057617 ns/iter 8596388816.833496 ns/iter 1.05
Evaluator_Draft4_Meta_1_No_Callback 312.7106874545706 ns/iter 291.385842303236 ns/iter 1.07
Evaluator_Draft4_Required_Properties 535.6725995475489 ns/iter 481.6893206483322 ns/iter 1.11
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 29.63188205450001 ns/iter 26.240243853030773 ns/iter 1.13
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 17.45538895704876 ns/iter 15.098483500838803 ns/iter 1.16
Evaluator_Draft4_Items_Schema 957.4067689247793 ns/iter 910.913200675566 ns/iter 1.05
Evaluator_Draft4_Nested_Object 2.098977301772247 ns/iter 1.986061311179066 ns/iter 1.06
Evaluator_Draft4_Properties_Triad_Optional 1105.5282680767439 ns/iter 1046.5920532925675 ns/iter 1.06
Evaluator_Draft4_Properties_Triad_Closed 997.7832939790832 ns/iter 831.8832845100599 ns/iter 1.20
Evaluator_Draft4_Properties_Triad_Required 1156.9840572359788 ns/iter 1041.6576329475981 ns/iter 1.11
Evaluator_Draft4_Properties_Closed 123.63913684708743 ns/iter 111.45911000948891 ns/iter 1.11
Evaluator_Draft4_Non_Recursive_Ref 22.922598696616188 ns/iter 21.13807884084365 ns/iter 1.08
Evaluator_Draft4_Pattern_Properties_True 1717.4229753893414 ns/iter 1631.6803170505714 ns/iter 1.05
Evaluator_Draft4_Ref_To_Single_Property 15.479721333819604 ns/iter 14.587453769724089 ns/iter 1.06
Evaluator_Draft4_Additional_Properties_Type 38.13724640357168 ns/iter 34.58570083734953 ns/iter 1.10
Evaluator_Draft4_Nested_Oneof 107.51658107173067 ns/iter 99.86686427414712 ns/iter 1.08
Evaluator_Draft4_Long_Enum 13.2164186556657 ns/iter 12.297421565138528 ns/iter 1.07
Evaluator_Draft4_Type_Object 8.776696866053394 ns/iter 8.491478933211871 ns/iter 1.03
Evaluator_Draft6_Property_Names 440.33385359927036 ns/iter 401.37376762275676 ns/iter 1.10
Evaluator_Draft7_If_Then_Else 46.30335753932707 ns/iter 41.173670875614796 ns/iter 1.12
Evaluator_Draft7_Vercel_1 120078.26075517028 ns/iter 96779.0905018122 ns/iter 1.24
Evaluator_2019_09_Unevaluated_Properties 185.43691607258313 ns/iter 164.32197154808878 ns/iter 1.13
Evaluator_2019_09_OMC_JSON_V2_1 6565.031250545159 ns/iter 7114.12894997059 ns/iter 0.92
Evaluator_2020_12_Dynamic_Ref 650.3611236081846 ns/iter 749.1883845522307 ns/iter 0.87

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: acc801f Previous: 16f9744 Ratio
Evaluator_2020_12_Dynamic_Ref 970.4059911490804 ns/iter 996.9004826174308 ns/iter 0.97
Evaluator_2019_09_Unevaluated_Properties 284.6982721818131 ns/iter 253.36890288661345 ns/iter 1.12
Evaluator_2019_09_OMC_JSON_V2_1 12565.580447250075 ns/iter 12442.274455294912 ns/iter 1.01
Evaluator_Draft7_If_Then_Else 70.50275470834484 ns/iter 65.99194289134826 ns/iter 1.07
Evaluator_Draft7_Vercel_1 149925.98851295415 ns/iter 144685.39987339202 ns/iter 1.04
Evaluator_Draft6_Property_Names 1253.8878730356907 ns/iter 1279.0519020223824 ns/iter 0.98
Evaluator_Draft4_Meta_1_No_Callback 532.01530299517 ns/iter 552.2959914164366 ns/iter 0.96
Evaluator_Draft4_Required_Properties 1779.5819344727126 ns/iter 1922.4439833683502 ns/iter 0.93
Evaluator_Draft4_Many_Optional_Properties_Minimal_Match 49.02998909656143 ns/iter 47.611687520708536 ns/iter 1.03
Evaluator_Draft4_Few_Optional_Properties_Minimal_Match 37.18503945970424 ns/iter 41.08256142177323 ns/iter 0.91
Evaluator_Draft4_Items_Schema 1308.1010347468773 ns/iter 1248.3045184301723 ns/iter 1.05
Evaluator_Draft4_Nested_Object 3.439259393585263 ns/iter 3.7915400162095874 ns/iter 0.91
Evaluator_Draft4_Properties_Triad_Optional 1378.8940239668466 ns/iter 1376.4520359908763 ns/iter 1.00
Evaluator_Draft4_Properties_Triad_Closed 1088.6001701273149 ns/iter 1086.5672347494142 ns/iter 1.00
Evaluator_Draft4_Properties_Triad_Required 1428.150524826599 ns/iter 1429.125354139083 ns/iter 1.00
Evaluator_Draft4_Properties_Closed 208.97058780566863 ns/iter 208.6734825402532 ns/iter 1.00
Evaluator_Draft4_Non_Recursive_Ref 54.59439988156766 ns/iter 58.34239857690855 ns/iter 0.94
Evaluator_Draft4_Pattern_Properties_True 2130.487439690778 ns/iter 2129.2520559857626 ns/iter 1.00
Evaluator_Draft4_Ref_To_Single_Property 40.453291430573806 ns/iter 44.060518804420866 ns/iter 0.92
Evaluator_Draft4_Additional_Properties_Type 109.54199298299919 ns/iter 116.30106505118849 ns/iter 0.94
Evaluator_Draft4_Nested_Oneof 156.76916024677254 ns/iter 149.2339689750351 ns/iter 1.05
Evaluator_Draft4_Long_Enum 23.189975585013173 ns/iter 22.064884592229095 ns/iter 1.05
Evaluator_Draft4_Type_Object 13.867944803888966 ns/iter 13.176250557834372 ns/iter 1.05
Compiler_2019_09_OMC_JSON_V2 11411194610.000051 ns/iter 11590858915.000013 ns/iter 0.98
Compiler_Draft6_AdaptiveCard 65844507720.00002 ns/iter 66205035075.000015 ns/iter 0.99

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

Please sign in to comment.