Skip to content

Return read-only JSON flag values with exact integers - #13484

Draft
leoromanovsky wants to merge 11 commits into
masterfrom
leo.romanovsky/ffe-json-frozen-values
Draft

leoromanovsky wants to merge 11 commits into
masterfrom
leo.romanovsky/ffe-json-frozen-values

Conversation

@leoromanovsky

@leoromanovsky leoromanovsky commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

  • A JSON flag evaluation returns a shared, read-only Value that is built once, when the configuration is parsed. Before, each evaluation converted the whole variant to a new Value, so its cost grew with the size of the variant.
  • Integers in JSON variant values are returned exactly: 5 is returned as 5, not 5.0, and integers up to Long.MAX_VALUE keep their value.
  • The plain-map form that asObjectMap() returns is built on its first call and then shared.

Motivation

Applications that serve large JSON flags pay for each evaluation in proportion to the variant size. On master, a 1 MB variant takes milliseconds and allocates megabytes on every call (see Results).

The JSON number handling is also a correctness bug. The parser reads every JSON number as a double, so integer fields come back as Double, and integer IDs above 2^53 change value.

Additional Notes

Results

ScaleSuiteBenchmark and ScaleSuiteLoadBenchmark from #13485, master (4075f1f) against this PR (d81545f), back to back. AWS workspace, Intel Xeon Platinum 8175M, 16 vCPUs, JDK 21.0.10, JMH AverageTime, 1 fork, 3 warm-up and 5 measurement iterations of 2 s, -prof gc. Each row is the mean over the cases of the file. The host load average was 12–14 during the run, so treat differences under about 10% as noise.

Case file master This PR Change
scale-json/json-sizes (100 KB, 1 MB, 4 MB) 4.30 ms, 6.9 MB 248 ns, 369 B 17,000× faster
scale-json/json-shapes (100,000 numbers, 1 MB string, escapes, nested arrays) 1.13 ms, 3.3 MB 209 ns, 369 B 5,400× faster
scale-json/json-values (719 nodes, 2,000 keys, 16 levels) 37.8 µs, 63,739 B 211 ns, 368 B 180× faster

The cost of a JSON evaluation no longer depends on the size of the value. The 368 B per call is the provider's result object. The 14 case files of the scale suite, which have no JSON values, allocate the same bytes per call on both builds, and their times match within noise.

Configuration load master This PR
scale-json (7.9 MB) parse time 65 ms (±6) 85 ms (±14)
scale-json allocated by one parse 65 MB 96 MB
scale-json retained heap about 38 MB about 44 MB
scale (7.9 MB, 10,030 small flags) parse time and retained heap 105 ms, about 24 MB 108 ms, about 24 MB

Freezing moves the conversion to load time: parsing the JSON-heavy configuration takes about 20 ms longer and keeps about 14% more heap. A configuration without large JSON values does not change.

Decisions

  • Freeze at parse time, build the plain map on first use. Each configuration pays the conversion once. asObjectMap() is built only when an application calls it, so a variant that is served but never read as a map does not keep a second copy.
  • Check the SDK's Long support by behavior. The provider declares OpenFeature SDK 1.23.0, but Maven can resolve an older SDK that an application declares. 1.22.0 added Value(Long) but its Structure.convertValue cannot convert it back, so the check converts a Long once, when the class loads. On 1.22.0 and older, integers outside the Integer range stay Double, as before.
  • MutableStructure over an unmodifiable map, not ImmutableStructure. ImmutableStructure.getValue deep-copies on each call. MutableStructure keeps the type and the equals behavior of today's result, and the frozen maps keep its key order.
  • Exact integers only in JSON variant values. The scope is the path flags.<flag>.variations.<key>.value, from the document root or the JSON:API data.attributes. Other UFC values (shard ranges, serial IDs, conditions) still expect Double.
  • Integer or Long, not BigInteger. Structure.convertValue cannot convert a BigInteger. Integers beyond the Long range stay Double.

Behavior changes

  • Changing a returned JSON structure, list or asObjectMap() map throws UnsupportedOperationException, with a message that says to copy the value first. asMap() still returns a new copy.
  • A hook that changes the value in after makes the evaluation return the default value with the GENERAL error code. The next evaluation still returns the configured value.
  • asObjectMap(), Value.asObject() and Value.equals see 5 (Integer) instead of 5.0 (Double) for JSON integers. This fixes the bug, but it can change what an application serializes or compares: value.equals(new Value(5.0)) is now false.
  • Value.asInteger() on an integer above 2^31-1 now wraps (Long.intValue()). Before, the Double saturated.
  • The README notes under "Migrating from earlier versions" describe these changes.

Testing

  • FlagEvaluatorJsonValueTest covers the shared instance, equality with an independently built expected value, exact integers (also through the JSON:API envelope and a flag keyed "variations"), scalar and array variants, the key order, the read-only error, and the Double values outside JSON variants. A ProviderTest case covers the after hook.
  • CanonicalFixtureTest expects exact integers in JSON results and defaults, against ffe-system-test-data main (cd0081d). That includes both cases of the JSON number precision fixture from Add JSON number precision fixture ffe-system-test-data#77.
  • Not covered: a CI run against OpenFeature SDK releases older than 1.23.0. The Long check was run by hand against 1.21.0, 1.22.0, 1.22.1 and 1.23.0.

Contributor Checklist

🤖 Generated with Claude Code

@leoromanovsky leoromanovsky added type: feature Enhancements and improvements tag: performance Performance related changes tag: ai generated Largely based on code generated by an AI or LLM comp: openfeature OpenFeature crucible: reviewing A Crucible review has started; this PR has reviewer attention. labels Nov 16, 2026
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 96.36%
• Overall Coverage: 60.66% (-0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 1a0184e | Docs | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.84 s 14.62 s [+0.5%; +2.4%] (maybe worse)
startup:insecure-bank:tracing:Agent 13.60 s 13.64 s [-1.0%; +0.4%] (no difference)
startup:petclinic:appsec:Agent 17.32 s 17.16 s [-0.1%; +2.0%] (no difference)
startup:petclinic:iast:Agent 16.61 s 17.18 s [-7.6%; +1.0%] (no difference)
startup:petclinic:profiling:Agent 16.59 s 16.89 s [-3.0%; -0.6%] (maybe better)
startup:petclinic:sca:Agent 17.25 s 16.99 s [+0.6%; +2.5%] (maybe worse)
startup:petclinic:tracing:Agent 16.19 s 16.22 s [-1.2%; +0.8%] (no difference)

Commit: 1a0184e3 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@leoromanovsky leoromanovsky removed the crucible: reviewing A Crucible review has started; this PR has reviewer attention. label Nov 16, 2026
@leoromanovsky leoromanovsky changed the title Share frozen JSON variant values and keep their integers exact Return read-only JSON flag values with exact integers Nov 16, 2026
leoromanovsky and others added 6 commits November 17, 2026 00:47
Add ScaleSuiteBenchmark, which evaluates each case of the opt-in scale suite of the shared FFE fixtures. The setup checks every expected value and reason first. Pin ffe-system-test-data to the scale suite PR head.
The scale benchmark sat on the JSON value change and checked JSON
integers by type, so it could not set a baseline on master. It now
compares JSON numbers by value, covers every case file of the scale and
scale-json suites, can isolate one case with the index parameter, and
prints each case's index. A load benchmark measures parsing each suite's
configuration and prints the heap it retains, so changes that move work
to load time show their cost.

The submodule points to the scale-suite fixture branch, which adds the
N x M dimensions and JSON values up to 10 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each evaluation of a JSON flag converted the whole variant tree to a new
OpenFeature Value. The cost grew with the variant size: about 21 us and
83 KB for a 712-node variant.

The parser now builds one Value for each JSON object or array variant.
Evaluations return that instance. Its structures are MutableStructure
over unmodifiable maps and its lists are unmodifiable, so callers cannot
change it, and equals is the same as Value.objectToValue. The plain-map
form is built once and shared.

Integers in JSON variant values now stay Integer or Long instead of
Double, so 5 is returned as 5 and integers above 2^53 stay exact.
Scalar flags keep Double values.
- Keep Long values only when the loaded OpenFeature SDK supports them.
  SDK 1.21.0 and older reject a Long with TypeMismatchError, which
  rejected the whole configuration.
- Freeze JSON variants in the parser, and store the frozen tree's plain
  form as Variant.value. The parsed tree is no longer kept as well.
- Make the parser scope follow positions, so a flag keyed "variations"
  keeps exact integers.
- Change nested integers of non-JSON flags back to Double, as before.
- Use one number model for JSON fixture defaults and results.
- Size maps exactly, and make FrozenValues package-private.
- Correct javadoc, and document the read-only values and exact integers.
- Add tests for the CDN envelope, the "variations" key, non-JSON flags
  and integers beyond the long range.
Handle non-JSON flags first in toVariant, use assertNull, and shorten the javadoc. No behavior change.
The submodule points to the scale suite plus the JSON number precision
fixture, so CanonicalFixtureTest checks exact JSON integers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leoromanovsky and others added 5 commits November 19, 2026 03:43
Master has the scale benchmarks of #13485 as one squashed commit, with
the fixtures moved to dd-openfeature/src/test/fixtures. Take master's
benchmarks, fixture path and fixture pin, and drop the old fixture
path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- OpenFeature SDK 1.22.0 has Value(Long) but cannot convert it back, so
  asObjectMap() on a JSON flag with a large integer threw there. Probe
  the conversion instead of the constructor; 1.22.0 now keeps doubles.
- A JSON object or array variant now holds only its frozen Value. The
  plain map form was built for every variant at load time and kept; it
  is now built on the first asObjectMap() call, which cuts the load time
  and the retained heap. The evaluator needs no special case: mapValue
  already returns a Value as is.
- Share the null and boolean Values, convert non-JSON variants to
  doubles in place, and use forEach to build the plain map.
- The README says when integers stay Double.
- Tests: compare against an independent expected value, cover scalar
  JSON variants, and check that an after hook that changes the value
  gets the default with GENERAL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Exact integers are read only at flags.<flag>.variations.<key>.value
  from the root of a UFC document or the data.attributes of the JSON:API
  envelope. A "flags" key nested elsewhere no longer starts the path.
- Frozen maps start at the default capacity of 16, as the maps that
  Value.objectToValue and asObjectMap() build, so keySet() and
  asObjectMap() list the keys in the same order as before.
- Changing a frozen structure throws an UnsupportedOperationException
  that says the value is read-only and should be copied.
- Pin ffe-system-test-data to the latest main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ffe-system-test-data main now has the JSON number precision fixture
(#77), so CanonicalFixtureTest runs its two cases again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The read-only test called only one of the eight add overloads, so the
coverage gate failed for FrozenStructure. It now calls each one and
checks the message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

comp: openfeature OpenFeature tag: ai generated Largely based on code generated by an AI or LLM tag: performance Performance related changes type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant