Skip to content

perf: return Collections.emptyMap() from asUnmodifiableMap() when attributes is empty#1974

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

perf: return Collections.emptyMap() from asUnmodifiableMap() when attributes is empty#1974
tobias-ibounig-dt wants to merge 1 commit into
open-feature:mainfrom
tobias-ibounig-dt:perf/pr-11-asUnmodifiableMap

Conversation

@tobias-ibounig-dt

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

Copy link
Copy Markdown
Contributor

This PR

  • AbstractStructure.asUnmodifiableMap() returns a shared static EMPTY_UNMODIFIABLE_MAP constant when attributes is empty, instead of allocating a new UnmodifiableMap wrapper on every call

Related Issues

None

Notes

Collections.unmodifiableMap(m) always allocates a new wrapper object, even when m is empty. For empty structures the result is always semantically identical, so the wrapper can be replaced with a single static instance. The constant is typed as Map<String, Value> and backed by Collections.unmodifiableMap(Collections.emptyMap()), so callers see no behavioral difference.

This path is not exercised by the current benchmark workload.

Follow-up Tasks

  • Update benchmark.txt after all PRs are applied

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved unmodifiable map handling in structures so empty or null-initialized attributes now reliably return an empty map.
  • Tests

    • Added coverage for unmodifiable map behavior on empty and null-backed structures, including verification that returned maps cannot be modified.

@tobias-ibounig-dt tobias-ibounig-dt requested review from a team as code owners June 19, 2026 11:42
@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: 728807a8-d62e-4272-87af-4eee7b88920d

📥 Commits

Reviewing files that changed from the base of the PR and between c812b44 and 9202413.

📒 Files selected for processing (2)
  • src/main/java/dev/openfeature/sdk/AbstractStructure.java
  • src/test/java/dev/openfeature/sdk/MutableStructureTest.java
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/java/dev/openfeature/sdk/AbstractStructure.java
  • src/test/java/dev/openfeature/sdk/MutableStructureTest.java

📝 Walkthrough

Walkthrough

AbstractStructure.asUnmodifiableMap() gains a null/empty guard that returns Collections.emptyMap() when attributes is null or empty. Two new tests in MutableStructureTest verify that both an empty structure and a null-backed structure return an empty, unmodifiable map.

Changes

Null/empty guard in asUnmodifiableMap

Layer / File(s) Summary
asUnmodifiableMap guard + tests
src/main/java/dev/openfeature/sdk/AbstractStructure.java, src/test/java/dev/openfeature/sdk/MutableStructureTest.java
asUnmodifiableMap() now returns Collections.emptyMap() when attributes is null or empty. Two new test methods assert the returned map is empty and throws UnsupportedOperationException on mutation, covering both the empty-structure and null-backed-structure scenarios. Import statements in the test class are updated to add AssertJ's assertThat and switch from a wildcard JUnit import to a named import.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% 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 directly and accurately describes the main change: optimizing asUnmodifiableMap() to return Collections.emptyMap() when attributes are empty, which is the core improvement in this performance-focused pull request.
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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/dev/openfeature/sdk/AbstractStructure.java`:
- Around line 32-34: The early return of Collections.emptyMap() when attributes
is empty but non-null breaks consistency with the live-view semantics of
asUnmodifiableMap() that is applied in the non-empty branch. Instead of
returning Collections.emptyMap() in the condition checking if attributes is null
or empty, remove the isEmpty() check from the conditional at line 32 so that
both empty and non-empty attributes maps are processed through the same
asUnmodifiableMap() logic used at line 35, ensuring consistent behavior
regardless of map size.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4cfc6f27-ef9b-48e4-a981-9a7b8dda9b5c

📥 Commits

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

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

Comment thread src/main/java/dev/openfeature/sdk/AbstractStructure.java
@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.11%. Comparing base (cc837b1) to head (9202413).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1974      +/-   ##
============================================
+ Coverage     92.11%   93.11%   +0.99%     
- Complexity      660      665       +5     
============================================
  Files            59       59              
  Lines          1624     1626       +2     
  Branches        182      182              
============================================
+ Hits           1496     1514      +18     
+ Misses           80       66      -14     
+ Partials         48       46       -2     
Flag Coverage Δ
unittests 93.11% <100.00%> (+0.99%) ⬆️

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.

…ributes is empty

Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
@tobias-ibounig-dt tobias-ibounig-dt force-pushed the perf/pr-11-asUnmodifiableMap branch from c812b44 to 9202413 Compare June 19, 2026 11:47
@tobias-ibounig-dt tobias-ibounig-dt changed the title perf: return ImmutableContext.EMPTY when merging two empty contexts perf: return Collections.emptyMap() from asUnmodifiableMap() when attributes is empty Jun 19, 2026
@sonarqubecloud

Copy link
Copy Markdown

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