Skip to content

perf: avoid Optional.ofNullable wrapping on hook.before() return value#1955

Open
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-4-optional-ofnullable
Open

perf: avoid Optional.ofNullable wrapping on hook.before() return value#1955
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-4-optional-ofnullable

Conversation

@tobias-ibounig-dt
Copy link
Copy Markdown

This PR

  • Replaces Optional.ofNullable(hook.before(...)).orElse(Optional.empty()) with a direct null check

Related Issues

None

Notes

The previous pattern wrapped the return value of hook.before() in Optional.ofNullable() to guard against null returns (which are non-standard but possible). This created up to two temporary Optional objects per hook call. The replacement assigns the return value directly and guards with an explicit != null check, which is semantically equivalent.

Allocation impact is negligible, but reduces on totalAllocatedInstances can be seen.

Metric main (baseline) PR 4 Delta
run:+totalAllocatedBytes 124,265,984 124,268,232 ~0 (noise)
run:+totalAllocatedInstances 2,723,316 2,700,508 −22,808 (−0.8%)

Follow-up Tasks

-More PRs with memory improvements

  • Update benchmark.txt after all are applied

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:38
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 5, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

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 simplifies the retrieval and validation of returnedEvalContext in HookSupport.java by directly assigning the result of hook.before and checking for null and presence. The reviewer suggested refactoring the code to use ifPresent instead of the isPresent() and get() pattern to make it more idiomatic and readable.

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.

.orElse(Optional.empty());
if (returnedEvalContext.isPresent()) {
Optional<EvaluationContext> returnedEvalContext = hook.before(hookContext, data.getHints());
if (returnedEvalContext != null && returnedEvalContext.isPresent()) {
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

While this condition is correct, you could make the code more idiomatic and avoid the isPresent()/get() pattern by using ifPresent.

For example:

if (returnedEvalContext != null) {
    returnedEvalContext.ifPresent(returnedContext -> {
        // existing logic from inside the if block
    });
}

This separates the null-check (to handle non-standard hook implementations) from the optional-handling, which can improve readability.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ifPresent would use new Lambda or Method reference

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