-
Notifications
You must be signed in to change notification settings - Fork 126
[IonJava] Optimize text read hot paths (+18.1% aggregate JMH) #1151
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
ganschen
wants to merge
2
commits into
amazon-ion:master
Choose a base branch
from
ganschen:perf/text-hot-paths
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.
+325
−20
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package com.amazon.ion; | ||
|
|
||
| import com.amazon.ion.system.IonReaderBuilder; | ||
| import com.amazon.ion.system.IonSystemBuilder; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| 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.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * Benchmarks targeting the text reader hot paths: | ||
| * - {@code IonReaderTextRawTokensX.load_double_quoted_string} / {@code load_symbol_identifier} | ||
| * / {@code skip_over_blob} ASCII fast paths | ||
| * - {@code IonReaderTextUserX.isIonVersionMarker} (regex -> character-by-character) | ||
| * | ||
| * Results are comparable between the baseline and CR HEAD | ||
| * by running {@code ./gradlew :jmh -PjmhIncludes=TextHotPathBenchmark}. | ||
| */ | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| @Fork(1) | ||
| @Warmup(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 5, time = 3, timeUnit = TimeUnit.SECONDS) | ||
| @State(Scope.Benchmark) | ||
| public class TextHotPathBenchmark { | ||
|
|
||
| private static final IonSystem SYSTEM = IonSystemBuilder.standard().build(); | ||
|
|
||
| /** Text with many double-quoted ASCII strings; exercises load_double_quoted_string fast path. */ | ||
| private byte[] doubleQuotedTextBytes; | ||
|
|
||
| /** Text with many ASCII symbol identifiers; exercises load_symbol_identifier fast path. */ | ||
| private byte[] symbolIdentifierTextBytes; | ||
|
|
||
| /** Text with a large base64 blob; exercises skip_over_blob inline whitespace fast path. */ | ||
| private byte[] blobTextBytes; | ||
|
|
||
| /** Text beginning with $ion_1_0 IVM many times over; exercises isIonVersionMarker. */ | ||
| private byte[] ivmTextBytes; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| doubleQuotedTextBytes = buildDoubleQuotedText(); | ||
| symbolIdentifierTextBytes = buildSymbolIdentifierText(); | ||
| blobTextBytes = buildBlobText(); | ||
| ivmTextBytes = buildIvmText(); | ||
| } | ||
|
|
||
| // ============ Benchmarks ============ | ||
|
|
||
| /** Read lots of double-quoted ASCII strings. */ | ||
| @Benchmark | ||
| public void readDoubleQuotedStrings(Blackhole bh) throws IOException { | ||
| try (IonReader r = IonReaderBuilder.standard().build(doubleQuotedTextBytes)) { | ||
| while (r.next() != null) { | ||
| bh.consume(r.stringValue()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Read many ASCII symbol identifiers. */ | ||
| @Benchmark | ||
| public void readSymbolIdentifiers(Blackhole bh) throws IOException { | ||
| try (IonReader r = IonReaderBuilder.standard().build(symbolIdentifierTextBytes)) { | ||
| while (r.next() != null) { | ||
| bh.consume(r.stringValue()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Read a big blob; exercises skip_over_blob inline whitespace path. */ | ||
| @Benchmark | ||
| public void readBlob(Blackhole bh) throws IOException { | ||
| try (IonReader r = IonReaderBuilder.standard().build(blobTextBytes)) { | ||
| while (r.next() != null) { | ||
| bh.consume(r.newBytes()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Read text starting with repeated IVMs; exercises isIonVersionMarker. */ | ||
| @Benchmark | ||
| public void readIvmHeavyText(Blackhole bh) throws IOException { | ||
| try (IonReader r = IonReaderBuilder.standard().build(ivmTextBytes)) { | ||
| while (r.next() != null) { | ||
| bh.consume(r.getType()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ============ Fixture builders ============ | ||
|
|
||
| private static byte[] buildDoubleQuotedText() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| Random r = new Random(42); | ||
| for (int i = 0; i < 2000; i++) { | ||
| sb.append('"'); | ||
| int len = 20 + r.nextInt(60); | ||
| for (int j = 0; j < len; j++) { | ||
| char c = (char) ('!' + r.nextInt(93)); | ||
| if (c == '"' || c == '\\') c = 'a'; | ||
| sb.append(c); | ||
| } | ||
| sb.append("\" "); | ||
| } | ||
| return sb.toString().getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| private static byte[] buildSymbolIdentifierText() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < 5000; i++) { | ||
| sb.append("symbol_identifier_").append(i).append(' '); | ||
| } | ||
| return sb.toString().getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| private static byte[] buildBlobText() { | ||
| Random r = new Random(7); | ||
| byte[] raw = new byte[32 * 1024]; | ||
| r.nextBytes(raw); | ||
| String b64 = java.util.Base64.getEncoder().encodeToString(raw); | ||
| StringBuilder sb = new StringBuilder(b64.length() + 16); | ||
| sb.append("{{ "); | ||
| for (int i = 0; i < b64.length(); i += 76) { | ||
| sb.append(b64, i, Math.min(i + 76, b64.length())).append('\n'); | ||
| } | ||
| sb.append(" }}"); | ||
| return sb.toString().getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| private static byte[] buildIvmText() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < 500; i++) { | ||
| sb.append("$ion_1_0 value_").append(i).append(' '); | ||
| } | ||
| return sb.toString().getBytes(StandardCharsets.UTF_8); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a switch statement on
c, which generates a jump table instead of multiple branch checks. Have you checked the performance difference between the two approaches?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converted to a switch.