@@ -110,7 +110,6 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
110110 FlagEvaluationOptions flagOptions = ObjectUtils .defaultIfNull (options ,
111111 () -> FlagEvaluationOptions .builder ().build ());
112112 Map <String , Object > hints = Collections .unmodifiableMap (flagOptions .getHookHints ());
113- ctx = ObjectUtils .defaultIfNull (ctx , () -> new ImmutableContext ());
114113
115114 FlagEvaluationDetails <T > details = null ;
116115 List <Hook > mergedHooks = null ;
@@ -183,17 +182,29 @@ private static <T> void enrichDetailsWithErrorDefaults(T defaultValue, FlagEvalu
183182 * @return merged evaluation context
184183 */
185184 private EvaluationContext mergeEvaluationContext (EvaluationContext invocationContext ) {
186- final EvaluationContext apiContext = openfeatureApi .getEvaluationContext () != null
187- ? openfeatureApi .getEvaluationContext ()
188- : new ImmutableContext ();
189- final EvaluationContext clientContext = this .getEvaluationContext () != null
190- ? this .getEvaluationContext ()
191- : new ImmutableContext ();
192- final EvaluationContext transactionContext = openfeatureApi .getTransactionContext () != null
193- ? openfeatureApi .getTransactionContext ()
194- : new ImmutableContext ();
195-
196- return apiContext .merge (transactionContext .merge (clientContext .merge (invocationContext )));
185+ // avoid any unnecessary context instantiations and stream usage here; this is call with every evaluation.
186+ final EvaluationContext apiContext = openfeatureApi .getEvaluationContext ();
187+ final EvaluationContext clientContext = this .getEvaluationContext ();
188+ final EvaluationContext transactionContext = openfeatureApi .getTransactionContext ();
189+ final List <EvaluationContext > contextsToMerge = new ArrayList <>();
190+ if (apiContext != null ) {
191+ contextsToMerge .add (apiContext );
192+ }
193+ if (transactionContext != null ) {
194+ contextsToMerge .add (transactionContext );
195+ }
196+ if (clientContext != null ) {
197+ contextsToMerge .add (clientContext );
198+ }
199+ if (invocationContext != null ) {
200+ contextsToMerge .add (invocationContext );
201+ }
202+
203+ EvaluationContext merged = new ImmutableContext ();
204+ for (EvaluationContext evaluationContext : contextsToMerge ) {
205+ merged = merged .merge (evaluationContext );
206+ }
207+ return merged ;
197208 }
198209
199210 private <T > ProviderEvaluation <?> createProviderEvaluation (
0 commit comments