Skip to content

perf: eliminate merge allocation in setHooks by accepting hook sources directly#1956

Merged
chrfwow merged 5 commits into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-5-set-hooks-refactor
Jun 11, 2026
Merged

perf: eliminate merge allocation in setHooks by accepting hook sources directly#1956
chrfwow merged 5 commits into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-5-set-hooks-refactor

Conversation

@tobias-ibounig-dt

Copy link
Copy Markdown
Contributor

This PR

  • Eliminates the intermediate merged list in setHooks by accepting the four hook sources directly

Related Issues

None

Notes

Previously OpenFeatureClient called ObjectUtils.merge(providerHooks, optionHooks, clientHooks, apiHooks) to concatenate all hook sources into a single ArrayList before passing it to HookSupport.setHooks. This allocated one list per flag evaluation solely to iterate it once. The change accepts the four sources as separate Collection<Hook> parameters and iterates them directly via a private addFilteredHooks helper, removing the intermediate allocation entirely.

Metric main (baseline) PR 5 Delta
run:+totalAllocatedBytes 124,265,984 111,685,320 −12,580,664 (−10.1%)
run:+totalAllocatedInstances 2,723,316 2,444,048 −279,268 (−10.3%)

Follow-up Tasks

  • Update benchmark.txt after all are applied

@tobias-ibounig-dt tobias-ibounig-dt requested review from a team as code owners June 5, 2026 08:43

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors hook handling in HookSupport and OpenFeatureClient by passing separate hook collections (provider, option, client, and API) directly to setHooks instead of merging them beforehand. This eliminates the need for ObjectUtils.merge. The review feedback suggests adding null checks to prevent potential NullPointerExceptions and pre-sizing the ArrayList to optimize performance when collecting the filtered hooks.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +29 to 51
public void setHooks(
HookSupportData hookSupportData,
Collection<Hook> providerHooks,
Collection<Hook> optionHooks,
Collection<Hook> clientHooks,
Collection<Hook> apiHooks,
FlagValueType type) {
List<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>();
for (Hook hook : hooks) {
addFilteredHooks(hookContextPairs, providerHooks, type);
addFilteredHooks(hookContextPairs, optionHooks, type);
addFilteredHooks(hookContextPairs, clientHooks, type);
addFilteredHooks(hookContextPairs, apiHooks, type);
hookSupportData.hooks = hookContextPairs;
}

private static void addFilteredHooks(
List<Pair<Hook, HookContext>> dest, Collection<Hook> source, FlagValueType type) {
for (Hook hook : source) {
if (hook.supportsFlagValueType(type)) {
hookContextPairs.add(Pair.of(hook, null));
dest.add(Pair.of(hook, null));
}
}
hookSupportData.hooks = hookContextPairs;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential NullPointerExceptions and further optimize performance, we should:

  1. Add Null Checks: Ensure that we safely handle cases where any of the hook collections (such as providerHooks or optionHooks) or individual hooks within them are null.
  2. Pre-size the ArrayList: Since we are aiming to optimize performance and eliminate unnecessary allocations, pre-sizing the ArrayList with the sum of the sizes of the non-null collections avoids internal array resizing overhead during population.
    public void setHooks(
            HookSupportData hookSupportData,
            Collection<Hook> providerHooks,
            Collection<Hook> optionHooks,
            Collection<Hook> clientHooks,
            Collection<Hook> apiHooks,
            FlagValueType type) {
        int expectedSize = 0;
        if (providerHooks != null) {
            expectedSize += providerHooks.size();
        }
        if (optionHooks != null) {
            expectedSize += optionHooks.size();
        }
        if (clientHooks != null) {
            expectedSize += clientHooks.size();
        }
        if (apiHooks != null) {
            expectedSize += apiHooks.size();
        }

        List<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>(expectedSize);
        addFilteredHooks(hookContextPairs, providerHooks, type);
        addFilteredHooks(hookContextPairs, optionHooks, type);
        addFilteredHooks(hookContextPairs, clientHooks, type);
        addFilteredHooks(hookContextPairs, apiHooks, type);
        hookSupportData.hooks = hookContextPairs;
    }

    private static void addFilteredHooks(
            List<Pair<Hook, HookContext>> dest, Collection<Hook> source, FlagValueType type) {
        if (source == null) {
            return;
        }
        for (Hook hook : source) {
            if (hook != null && hook.supportsFlagValueType(type)) {
                dest.add(Pair.of(hook, null));
            }
        }
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't know which hooks will be added to the array list, so we would be overestimating the size of the list. I'm not sure if this is worth it, and if we can come up with a benchmark that represensts real life usage

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under 10 hooks, it looks like the runtime will allocate at least that many anyway: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/ArrayList.java#L119 - if I'm reading this correctly.

I don't think we'll often have more than 10, so I think doing more than what's here now is a micro-optimization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are null checks that Gemini mentions are concern?
As far as I can tell there were none in place previously, which is why I didn't consider adding them.

@codecov

codecov Bot commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.06%. Comparing base (75391aa) to head (4f6554d).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1956      +/-   ##
============================================
+ Coverage     92.28%   93.06%   +0.77%     
- Complexity      655      657       +2     
============================================
  Files            59       59              
  Lines          1594     1600       +6     
  Branches        181      180       -1     
============================================
+ Hits           1471     1489      +18     
+ Misses           76       66      -10     
+ Partials         47       45       -2     
Flag Coverage Δ
unittests 93.06% <100.00%> (+0.77%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chrfwow chrfwow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just left a comment for the tests

Comment on lines +29 to 51
public void setHooks(
HookSupportData hookSupportData,
Collection<Hook> providerHooks,
Collection<Hook> optionHooks,
Collection<Hook> clientHooks,
Collection<Hook> apiHooks,
FlagValueType type) {
List<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>();
for (Hook hook : hooks) {
addFilteredHooks(hookContextPairs, providerHooks, type);
addFilteredHooks(hookContextPairs, optionHooks, type);
addFilteredHooks(hookContextPairs, clientHooks, type);
addFilteredHooks(hookContextPairs, apiHooks, type);
hookSupportData.hooks = hookContextPairs;
}

private static void addFilteredHooks(
List<Pair<Hook, HookContext>> dest, Collection<Hook> source, FlagValueType type) {
for (Hook hook : source) {
if (hook.supportsFlagValueType(type)) {
hookContextPairs.add(Pair.of(hook, null));
dest.add(Pair.of(hook, null));
}
}
hookSupportData.hooks = hookContextPairs;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't know which hooks will be added to the array list, so we would be overestimating the size of the list. I'm not sure if this is worth it, and if we can come up with a benchmark that represensts real life usage

new LayeredEvaluationContext(evaluationContextWithValue("key", "value"), null, null, null);
hookSupportData.evaluationContext = layeredEvaluationContext;
hookSupport.setHooks(hookSupportData, List.of(recursiveHook, emptyHook), FlagValueType.STRING);
hookSupport.setHooks(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd pass the non empty list in different order for each of these tests, and I think we should also add a test to verify that the hooks are placed in the correct order in the resulting list

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adapted the tests as suggested and added an extra test to ensure order.

@tobias-ibounig-dt

Copy link
Copy Markdown
Contributor Author

I realized ObjectUtils#merge is now unused. You want me too remove it?

@chrfwow

chrfwow commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

I realized ObjectUtils#merge is now unused. You want me too remove it?

Yes, I think we can remove it

@toddbaert

Copy link
Copy Markdown
Member

I realized ObjectUtils#merge is now unused. You want me too remove it?

Yes, I think we can remove it

Just a call out - this was a public method, so this makes this change breaking - however - it was in the .internal package, so it think we can probably get away with it.

WDYT @chrfwow ? Would this surprise you if you were a user?

@chrfwow

chrfwow commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

I realized ObjectUtils#merge is now unused. You want me too remove it?

Yes, I think we can remove it

Just a call out - this was a public method, so this makes this change breaking - however - it was in the .internal package, so it think we can probably get away with it.

True, I did not see that.

WDYT @chrfwow ? Would this surprise you if you were a user?

I hope nobody uses an internal helper method from some library, but I guess we can never know. Not sure if we should remove it then. On the other hand, it's obviously not part of the functionality of OpenFeature, and can be easily reproduced by our users, so I think the impact on them should be minimal.

…s directly

Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
@tobias-ibounig-dt tobias-ibounig-dt force-pushed the perf/pr-5-set-hooks-refactor branch from c4c79b0 to 940651a Compare June 11, 2026 10:44
@tobias-ibounig-dt

Copy link
Copy Markdown
Contributor Author

Rebased, reintroduced ObjectUtils.merge, but marked it deprecated for removal.
Could be removed in a future release, to cleanup.

Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
@sonarqubecloud

Copy link
Copy Markdown

@toddbaert

Copy link
Copy Markdown
Member

I realized ObjectUtils#merge is now unused. You want me too remove it?

Yes, I think we can remove it

Just a call out - this was a public method, so this makes this change breaking - however - it was in the .internal package, so it think we can probably get away with it.

True, I did not see that.

WDYT @chrfwow ? Would this surprise you if you were a user?

I hope nobody uses an internal helper method from some library, but I guess we can never know. Not sure if we should remove it then. On the other hand, it's obviously not part of the functionality of OpenFeature, and can be easily reproduced by our users, so I think the impact on them should be minimal.

I think it's my least favorite part of Java (a language I mostly like) that it doesn't really have the concept of "jar private" (like .NET has with internal, for example). At some point (maybe v2) I would like to flatten the package structure so we could properly only export things we really want exported.

I guess we could do some jar-packaging stuff as well, but TBH I think that's overkill.

@toddbaert toddbaert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed one small comment and approved.

@chrfwow chrfwow merged commit a5ba3c6 into open-feature:main Jun 11, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants