Skip to content

perf: avoid copying the evaluation context - #78

Merged
kinyoklion merged 2 commits into
launchdarkly:mainfrom
TSFenwick:perf/avoid-context-copy
Dec 2, 2026
Merged

kinyoklion merged 2 commits into
launchdarkly:mainfrom
TSFenwick:perf/avoid-context-copy

Conversation

@TSFenwick

@TSFenwick TSFenwick commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

toLdContext called asMap() twice on the evaluation context, and once more on each nested structure of a multi-kind context. Each call returns a new copy of the map, on every flag evaluation.

The converter only reads these maps. It now reads them through asUnmodifiableMap(), which OpenFeature SDK 1.16.0 already has, and it reuses the top-level map.

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

None.

Describe the solution you've provided

The converter reads the evaluation context and each nested structure through asUnmodifiableMap(), and passes the top-level map to BuildSingleContext and BuildMultiContext. The converter only reads these maps, so the copies are not necessary.

Behavior does not change. Before and after return the same LDContext for single-kind, multi-kind, nested-kind and invalid contexts.

Describe alternatives you've considered

  • Reuse the first asMap() result only. That removes one copy but keeps the others. asUnmodifiableMap() removes all of them and is in the lowest supported OpenFeature SDK.
  • Also change ValueConverter, which calls asMap() on attributes whose value is a structure. I left it out to keep this change to the path that every evaluation takes.

Additional context

This change adds no tests, because behavior does not change. The existing ContextConverterTest covers single-kind, multi-kind, nested-kind and invalid contexts. It passes on JDK 17 with OpenFeature SDK 1.23.0, and with OpenFeature SDK 1.16.0 forced. I did not run it on Java 11 or 19, which CI covers.

I used AI in investigating this issue and the perf improvement


Note

Overview
This PR removes redundant map copies when converting OpenFeature evaluation contexts to LaunchDarkly LDContext on the flag-evaluation path.

EvaluationContextConverter now calls asUnmodifiableMap() once on the top-level context and reuses that map for single- and multi-kind builds, instead of repeated asMap() calls that allocate copies. BuildMultiContext takes the attribute map directly, and nested multi-kind structures also use asUnmodifiableMap().

ValueConverter uses asUnmodifiableMap() when iterating structure attributes during conversion, for the same reason.

Behavior is unchanged (read-only access). A new test asserts structure conversion goes through asUnmodifiableMap().

Reviewed by Cursor Bugbot for commit d4a806e. Bugbot is set up for automated code reviews on this repo. Configure here.

toLdContext called asMap() twice on the evaluation context, and once
more on each nested structure of a multi-kind context. Each call
returns a new copy of the map, on every flag evaluation.

The converter only reads these maps. It now reads them through
asUnmodifiableMap(), which OpenFeature SDK 1.16.0 already has, and
it reuses the top-level map.
@TSFenwick
TSFenwick requested a review from a team as a code owner November 25, 2026 22:50
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@kinyoklion

Copy link
Copy Markdown
Member

Hello @TSFenwick,

Thank you for the contribution!

I'm going to make a couple minor comment changes and also look to see if the optimization can be extended to the ValueConverter.

@kinyoklion
kinyoklion merged commit d51aad4 into launchdarkly:main Dec 2, 2026
7 checks passed
kinyoklion pushed a commit that referenced this pull request Dec 3, 2026
🤖 I have created a release *beep* *boop*
---


##
[2.0.2](2.0.1...2.0.2)
(2026-12-02)


### Performance Improvements

* avoid copying the evaluation context
([#78](#78))
([d51aad4](d51aad4))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> Bumps the library to **2.0.2** via Release Please:
`gradle.properties`, `.release-please-manifest.json`, and **CHANGELOG**
now record the **2.0.2** release (2026-12-02).
> 
> The changelog entry documents a **performance improvement** from
[#78](https://github.com/launchdarkly/openfeature-java-server/issues/78)—avoiding
unnecessary copying of the OpenFeature **evaluation context** during
flag evaluation. No application code changes appear in this PR; it only
packages and documents that already-merged fix for release.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
24079d9. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

3 participants