@@ -37,24 +37,26 @@ class EvaluatorWithHooks implements EvaluatorInterface {
3737 public EvalResultAndFlag evalAndFlag (String method , String featureKey , LDContext context , LDValue defaultValue , LDValueType requireType , EvaluationOptions options ) {
3838 // Each hook will have an opportunity to provide series data to carry along to later stages. This list
3939 // is to track that data.
40- List <Map > seriesDataList = new ArrayList <>(hooks .size ());
40+ int size = hooks .size ();
41+ List <Map > seriesDataList = new ArrayList <>(size );
4142
4243 EvaluationSeriesContext seriesContext = new EvaluationSeriesContext (method , featureKey , context , defaultValue );
43- for (int i = 0 ; i < hooks .size (); i ++) {
44+ Map <String , Object > emptyMap = Collections .emptyMap ();
45+ for (int i = 0 ; i < size ; i ++) {
4446 Hook currentHook = hooks .get (i );
4547 try {
46- Map <String , Object > seriesData = currentHook .beforeEvaluation (seriesContext , Collections . emptyMap () );
47- seriesDataList .add (Collections .unmodifiableMap (seriesData )); // make data immutable
48+ Map <String , Object > seriesData = currentHook .beforeEvaluation (seriesContext , emptyMap );
49+ seriesDataList .add (seriesData . isEmpty () ? emptyMap : Collections .unmodifiableMap (seriesData )); // make data immutable
4850 } catch (Exception e ) {
49- seriesDataList .add (Collections . emptyMap () ); // since the provided hook failed to execute, we default the series data to an empty map in this case
51+ seriesDataList .add (emptyMap ); // since the provided hook failed to execute, we default the series data to an empty map in this case
5052 logger .error ("During evaluation of flag \" {}\" . Stage \" BeforeEvaluation\" of hook \" {}\" reported error: {}" , featureKey , currentHook .getMetadata ().getName (), e .toString ());
5153 }
5254 }
5355
5456 EvalResultAndFlag result = underlyingEvaluator .evalAndFlag (method , featureKey , context , defaultValue , requireType , options );
5557
5658 // Invoke hooks in reverse order and give them back the series data they gave us.
57- for (int i = hooks . size () - 1 ; i >= 0 ; i --) {
59+ for (int i = size - 1 ; i >= 0 ; i --) {
5860 Hook currentHook = hooks .get (i );
5961 try {
6062 currentHook .afterEvaluation (seriesContext , seriesDataList .get (i ), result .getResult ().getAnyType ());
0 commit comments