growthbook / growthbook/growthbook-sdk-java
Feature Request: Optimized Batch Feature Evaluation with Shared UserContext for Memory Efficiency
- Dominant language
- Java
- Stars
- 17
- Forks
- 18
- Avg merge
- 16m
- Merged PRs (30d)
- 1
Description
Requesting a method to evaluate multiple features with a single shared `UserContext` to reduce memory usage when evaluating many features for the same user.
## Problem Statement
When evaluating multiple features for the same user, we create a `UserContext` once and reuse it across evaluations. However, each call to `evalFeature()` or `getFeatureValue()` may create internal copies or duplicate data structures, leading to:
- Higher memory usage when evaluating 50-100+ features per request
- Unnecessary object allocations
- Potential performance overhead from repeated context processing
## Use Case
We have a high-throughput service that evaluates 50-100 features per user request. Currently, we:
```java
// Create UserContext once
UserContext userContext = new UserContext.UserContextBuilder()
.build()
.witAttributesJson(attributesJson);
// Evaluate multiple features sequentially or in parallel
for (String featureName : featureNames) {
FeatureResult result = client.evalFeature(
featureName,
Object.class,
userContext // Same context reused
);
// Process result...
}
```
While `UserContext` is immutable and thread-safe, the SDK may still create internal copies or process the context multiple times.
## Current Behavior
- Each `evalFeature()` call processes the `UserContext` independently
- No explicit optimization for batch evaluation scenarios
- Memory usage scales linearly with the number of features evaluated
## Proposed Solution
Add a batch evaluation method that:
1. Accepts a list of feature keys and a single `UserContext`
2. Internally optimizes context processing (parse attributes once, reuse parsed data)
3. Returns a map of feature keys to their evaluated results
4. Minimizes memory allocations by sharing internal data structures
**Proposed API:**
```java
// Option 1: New method on GrowthBookClient
Map> evalFeatures(
List featureKeys,
Class valueTypeClass,
UserContext userContext
);
// Option 2: Builder pattern for batch operations
BatchEvaluator batchEvaluator = client.createBatchEvaluator(userContext);
Map> results = batchEvaluator
.evalFeatures(featureKeys, Object.class);
```
## Benefits
1. Memory efficiency: parse and process `UserContext` once for multiple features
2. Performance: reduce redundant context processing
3. API clarity: explicit batch evaluation method
4. Backward compatible: existing single-feature methods remain unchanged
## Example Implementation (Pseudo-code)
```java
public Map> evalFeatures(
List featureKeys,
Class valueTypeClass,
UserContext userContext
) {
// Parse attributes once
Map parsedAttributes = parseAttributes(userContext.getAttributesJson());
// Pre-compute common context data
EvaluationContext evalContext = buildEvaluationContext(userContext, parsedAttributes);
// Evaluate all features using shared context
Map> results = new HashMap<>();
for (String featureKey : featureKeys) {
results.put(featureKey, evaluateFeature(featureKey, valueTypeClass, evalContext));
}
return results;
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.