Skip to content

perf: cache clientMetadata lambda to avoid per-call allocation#1952

Open
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-1-client-metadata
Open

perf: cache clientMetadata lambda to avoid per-call allocation#1952
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-1-client-metadata

Conversation

@tobias-ibounig-dt
Copy link
Copy Markdown

This PR

  • Caches the ClientMetadata lambda in a final field to avoid re-allocating it on every getMetadata() call

Related Issues

None

Notes

getMetadata() is called once per flag evaluation to build the SharedHookContext. Previously it returned this::getDomain — a new lambda instance each time. The field is initialized once in the constructor.

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

Metric main (baseline) This PR Delta
run:+totalAllocatedBytes 124,265,984 124,265,984 0
run:+totalAllocatedInstances 2,723,316 2,674,636 −48,680 (−1.8%)

Follow-up Tasks

  • More PRs with memory improvements
  • 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:22
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 OpenFeatureClient to store a clientMetadata field initialized with a lambda reference in the constructor, returning it in getMetadata(). The reviewer suggests optimizing this further by having OpenFeatureClient directly implement the ClientMetadata interface, which would allow getMetadata() to return this and completely eliminate the extra field and lambda allocation.

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.

@Override
public ClientMetadata getMetadata() {
return this::getDomain;
return clientMetadata;
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

Instead of introducing a new clientMetadata field and allocating a lambda instance in the constructor, OpenFeatureClient can directly implement the ClientMetadata interface.

Since OpenFeatureClient already has a public getDomain() method (generated by Lombok's @Getter on the domain field), it already satisfies the ClientMetadata interface requirements.

To apply this optimization:

  1. Update the class signature to:
    public class OpenFeatureClient implements Client, ClientMetadata {
  2. Remove the clientMetadata field and its initialization in the constructor.
  3. Update getMetadata() to return this as suggested below.

This completely avoids any extra field overhead and lambda allocation (reducing it to zero allocation).

Suggested change
return clientMetadata;
return this;

Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
@tobias-ibounig-dt tobias-ibounig-dt force-pushed the perf/pr-1-client-metadata branch from 8e1cd13 to d66651c Compare June 5, 2026 08:35
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 5, 2026

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