Skip to content

perf: return ImmutableContext.EMPTY when merging two empty contexts#1973

Open
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-10-empty-context-merge
Open

perf: return ImmutableContext.EMPTY when merging two empty contexts#1973
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-10-empty-context-merge

Conversation

@tobias-ibounig-dt

@tobias-ibounig-dt tobias-ibounig-dt commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

This PR

  • ImmutableContext.merge() returns ImmutableContext.EMPTY when both this and the overriding context are empty

Related Issues

None

Notes

When merging two empty contexts the previous code still allocated a new ImmutableContext (via this.asUnmodifiableMap()). The result is always semantically equivalent to EMPTY, so we can return the existing singleton directly.

This path is not exercised by the current benchmark workload, so no allocation numbers are provided.

Follow-up Tasks

  • More PRs with memory improvements

Summary by CodeRabbit

  • Tests

    • Enhanced test coverage for ImmutableContext with new nested tests verifying constructor behavior and context merging scenarios, including edge cases with null and empty contexts.
  • Performance

    • Optimized memory usage by reusing singleton instances in context merge operations when appropriate.

Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
@tobias-ibounig-dt tobias-ibounig-dt requested review from a team as code owners June 19, 2026 11:41
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 637dcdaf-ebff-4e83-9944-b477cd1f87b7

📥 Commits

Reviewing files that changed from the base of the PR and between cc837b1 and c7ea4db.

📒 Files selected for processing (2)
  • src/main/java/dev/openfeature/sdk/ImmutableContext.java
  • src/test/java/dev/openfeature/sdk/ImmutableContextTest.java

📝 Walkthrough

Walkthrough

ImmutableContext.merge() is updated to return the EMPTY singleton instead of constructing a new instance when both the current context and the overriding context are empty. New tests cover constructor branch behavior for null/empty inputs and verify the EMPTY singleton short-circuit semantics.

Changes

ImmutableContext EMPTY singleton short-circuit

Layer / File(s) Summary
merge() short-circuit and test coverage
src/main/java/dev/openfeature/sdk/ImmutableContext.java, src/test/java/dev/openfeature/sdk/ImmutableContextTest.java
merge() now returns ImmutableContext.EMPTY when this.isEmpty() and the overriding context is null or empty, avoiding unnecessary allocation. New nested test classes assert EMPTY singleton identity for all empty-context merge combinations, verify non-empty contexts do not return EMPTY, and cover constructor branches for null/empty targeting key and attributes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: optimizing ImmutableContext.merge() to return the EMPTY singleton when merging two empty contexts, which is a performance improvement.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.17%. Comparing base (cc837b1) to head (c7ea4db).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1973      +/-   ##
============================================
+ Coverage     92.11%   93.17%   +1.05%     
- Complexity      660      665       +5     
============================================
  Files            59       59              
  Lines          1624     1626       +2     
  Branches        182      183       +1     
============================================
+ Hits           1496     1515      +19     
+ Misses           80       66      -14     
+ Partials         48       45       -3     
Flag Coverage Δ
unittests 93.17% <100.00%> (+1.05%) ⬆️

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.

2 participants