java-sdk
java-sdk copied to clipboard
Cache merged context
Currently, the OpenFeatureClient.evaluateFlag() method performs a complete context merge on every flag evaluation by calling mergeEvaluationContext(), which merges all four context levels from scratch: API context (global), Transaction context, Client context, Invocation context (evaluation-specific)
Problem: This bottom-up approach is inefficient because:
- API, transaction, and client contexts change infrequently compared to evaluation frequency
- Each evaluation recreates the same merged context for the stable upper levels
- Unnecessary object allocation and computation occurs on every evaluation
Proposed Solution: Implement a top-down context propagation system:
- API level: When API context changes, propagate to transaction level
- Transaction level: When transaction context changes, propagate to client level
- Client level: When client context changes, create pre-merged context (API + Transaction + Client)
- Evaluation: Only merge the pre-computed context with the invocation context
Benefits:
- Reduces object churn and GC pressure and improves performance in high-evaluation scenarios
- Enables better performance scaling for applications with many flag evaluations
The documentation should recommend setting contexts before creating clients to maximize caching benefits.