-
Notifications
You must be signed in to change notification settings - Fork 274
defer CBOR/string pre-encoding in SoftReferencedFieldMap until needed #2461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thjaeckle
wants to merge
1
commit into
eclipse-ditto:master
Choose a base branch
from
beyonnex-io:refactor/lazy-cbor-encoding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+326
−66
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
json/src/test/java/org/eclipse/ditto/json/ImmutableJsonObjectBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /* | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0 | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| */ | ||
| package org.eclipse.ditto.json; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
| import org.openjdk.jmh.runner.Runner; | ||
| import org.openjdk.jmh.runner.RunnerException; | ||
| import org.openjdk.jmh.runner.options.Options; | ||
| import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
|
||
| /** | ||
| * JMH micro-benchmark for {@link ImmutableJsonObject} construction, used to validate the | ||
| * lazy-encoding refactor in {@code SoftReferencedFieldMap}. | ||
| * | ||
| * <h2>Scenarios</h2> | ||
| * <p>Each {@code @Benchmark} exercises the programmatic-build path | ||
| * ({@link JsonObjectBuilder}) that is the hot site in production for | ||
| * {@code Thing.toJson}, {@code Feature.toJson}, {@code AbstractCommandResponse.toJson} | ||
| * and similar emit-side flows.</p> | ||
| * | ||
| * <ul> | ||
| * <li><b>buildSmallNoSerialize</b> — build a 5-field object, discard. Measures | ||
| * pure construction cost; this is the maximum-saving case once eager pre-encoding is | ||
| * deferred (short-lived JsonObjects that never get serialised).</li> | ||
| * <li><b>buildLargeNoSerialize</b> — same, but 50 fields. Larger field map = more | ||
| * eager work being eliminated.</li> | ||
| * <li><b>buildSmallThenToString</b> — build then call {@code toString()}. After | ||
| * the refactor this should match the baseline (string is materialised lazily on | ||
| * first call) — serves as a no-regression check for the case where the cache | ||
| * <em>is</em> consumed.</li> | ||
| * <li><b>buildLargeThenToString</b> — same, 50 fields.</li> | ||
| * <li><b>buildSmallAccessFields</b> — build then call {@code getField} 5 times. | ||
| * Should be substantially faster after the refactor since no encoding happens.</li> | ||
| * </ul> | ||
| * | ||
| * <h2>How to run</h2> | ||
| * <pre> | ||
| * mvn test-compile -pl json -am -Djapicmp.skip=true | ||
| * java -cp "$(mvn -pl json dependency:build-classpath -Dmdep.outputFile=/dev/stdout -q):json/target/classes:json/target/test-classes" \ | ||
| * org.eclipse.ditto.json.ImmutableJsonObjectBenchmark | ||
| * </pre> | ||
| */ | ||
| @State(Scope.Benchmark) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| @Warmup(iterations = 3, time = 1) | ||
| @Measurement(iterations = 5, time = 2) | ||
| @Fork(1) | ||
| @Threads(1) | ||
| public class ImmutableJsonObjectBenchmark { | ||
|
|
||
| private static final int LARGE_FIELD_COUNT = 50; | ||
|
|
||
| private String[] smallKeys; | ||
| private JsonValue[] smallValues; | ||
| private String[] largeKeys; | ||
| private JsonValue[] largeValues; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| smallKeys = new String[]{"thingId", "policyId", "_revision", "definition", "_modified"}; | ||
| smallValues = new JsonValue[]{ | ||
| JsonValue.of("ns:thing-1"), | ||
| JsonValue.of("ns:policy-1"), | ||
| JsonValue.of(42L), | ||
| JsonValue.of("ditto:robot:1.0.0"), | ||
| JsonValue.of("2026-05-26T07:11:46Z"), | ||
| }; | ||
|
|
||
| largeKeys = new String[LARGE_FIELD_COUNT]; | ||
| largeValues = new JsonValue[LARGE_FIELD_COUNT]; | ||
| for (int i = 0; i < LARGE_FIELD_COUNT; i++) { | ||
| largeKeys[i] = "feature_" + i; | ||
| largeValues[i] = JsonValue.of("value_" + i); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void buildSmallNoSerialize(final Blackhole bh) { | ||
| final JsonObjectBuilder builder = JsonFactory.newObjectBuilder(); | ||
| for (int i = 0; i < smallKeys.length; i++) { | ||
| builder.set(smallKeys[i], smallValues[i]); | ||
| } | ||
| bh.consume(builder.build()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void buildLargeNoSerialize(final Blackhole bh) { | ||
| final JsonObjectBuilder builder = JsonFactory.newObjectBuilder(); | ||
| for (int i = 0; i < largeKeys.length; i++) { | ||
| builder.set(largeKeys[i], largeValues[i]); | ||
| } | ||
| bh.consume(builder.build()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void buildSmallThenToString(final Blackhole bh) { | ||
| final JsonObjectBuilder builder = JsonFactory.newObjectBuilder(); | ||
| for (int i = 0; i < smallKeys.length; i++) { | ||
| builder.set(smallKeys[i], smallValues[i]); | ||
| } | ||
| bh.consume(builder.build().toString()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void buildLargeThenToString(final Blackhole bh) { | ||
| final JsonObjectBuilder builder = JsonFactory.newObjectBuilder(); | ||
| for (int i = 0; i < largeKeys.length; i++) { | ||
| builder.set(largeKeys[i], largeValues[i]); | ||
| } | ||
| bh.consume(builder.build().toString()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void buildSmallAccessFields(final Blackhole bh) { | ||
| final JsonObjectBuilder builder = JsonFactory.newObjectBuilder(); | ||
| for (int i = 0; i < smallKeys.length; i++) { | ||
| builder.set(smallKeys[i], smallValues[i]); | ||
| } | ||
| final JsonObject obj = builder.build(); | ||
| for (int i = 0; i < smallKeys.length; i++) { | ||
| bh.consume(obj.getValue(smallKeys[i])); | ||
| } | ||
| } | ||
|
|
||
| public static void main(final String[] args) throws RunnerException { | ||
| final Options opt = new OptionsBuilder() | ||
| .include(ImmutableJsonObjectBenchmark.class.getSimpleName()) | ||
| .build(); | ||
| new Runner(opt).run(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.