Skip to content

Commit 6b42592

Browse files
authored
perf: optimize EvaluatorWithHooks (#73)
**Requirements** - [x] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [x] I have validated my changes against all supported platform versions **Related issues** none **Describe the solution you've provided** - reduce calls to size() - presize collection - only wrap non-empty maps **Describe alternatives you've considered** non **Additional context** i was doing some flame graph analysis and found this tiny hotspot
1 parent c7677b8 commit 6b42592

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/EvaluatorWithHooks.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)