Skip to content

perf: avoid copying the evaluation context - #78

Open
TSFenwick wants to merge 1 commit into
launchdarkly:mainfrom
TSFenwick:perf/avoid-context-copy
Open

TSFenwick wants to merge 1 commit into
launchdarkly:mainfrom
TSFenwick:perf/avoid-context-copy

Conversation

@TSFenwick

@TSFenwick TSFenwick commented Nov 25, 2026 •

Copy link
Copy Markdown

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
Stops copying evaluation context maps on every flag evaluation by switching EvaluationContextConverter from asMap() to asUnmodifiableMap() for the top-level context and nested structures in multi-kind contexts.

The converter reuses one top-level attribute map end-to-end: BuildMultiContext and BuildSingleContext now take Map<String, Value> instead of re-reading evaluationContext.asMap(). Output LDContext behavior is unchanged; only read-only views are used where copies were unnecessary.

Reviewed by Cursor Bugbot for commit 8f8aeac. 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

This branch has not been deployed

No deployments
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