Skip to content

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

Open
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-5-set-hooks-refactor
Open

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

Conversation

@tobias-ibounig-dt
Copy link
Copy Markdown

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

…s directly

Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
@tobias-ibounig-dt tobias-ibounig-dt requested review from a team as code owners June 5, 2026 08:43
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

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

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));
            }
        }
    }

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.05%. Comparing base (58091f3) to head (73aa53a).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1956      +/-   ##
============================================
+ Coverage     92.27%   93.05%   +0.77%     
- Complexity      653      655       +2     
============================================
  Files            59       59              
  Lines          1592     1598       +6     
  Branches        179      178       -1     
============================================
+ Hits           1469     1487      +18     
+ Misses           76       66      -10     
+ Partials         47       45       -2     
Flag Coverage Δ
unittests 93.05% <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.

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.

1 participant