Repository navigation
Optimize Row construction, RowWithGetters field caching, and Row utilities #40791
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
sjvanrossum
wants to merge
13
commits into
apache:master
Choose a base branch
from
sjvanrossum:row-coder-opt-1
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.
+811
−626
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
441fb29
Add reproducer tests for Row.Equals.deepHashCode on logical types and…
sjvanrossum b6af710
Fix Row.Equals.deepHashCode for logical types and RowWithGetters equa…
sjvanrossum 340f9ca
Add reproducer tests for nested Row schema validation in RowUtils
sjvanrossum 57941fa
Validate nested Row schema when setting ROW fields in RowUtils
sjvanrossum 9c373a7
Add reproducer test for sliced and read-only ByteBuffers in RowUtils
sjvanrossum d542376
Preserve ByteBuffer position and limit when extracting bytes in RowUtils
sjvanrossum 2a4a062
Add reproducer test for DATETIME-based logical types in SchemaCoderHe…
sjvanrossum 33b4bdd
Handle null and structural values for DATETIME-based logical types in…
sjvanrossum ad142a8
Apply spotless formatting
sjvanrossum 03bb446
Fix nested Row schema in RowStringInterpolatorTest
sjvanrossum 1af8a39
Add Row and RowCoder benchmark
sjvanrossum 2af3ce6
Optimize Row construction, RowWithGetters field caching, and Row util…
sjvanrossum 0b10478
Fix CheckerFramework nullness errors in SchemaVerification and RowWit…
sjvanrossum 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
342 changes: 342 additions & 0 deletions
342
sdks/java/core/jmh/src/main/java/org/apache/beam/sdk/jmh/schemas/RowAndCoderBenchmark.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,342 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.sdk.jmh.schemas; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.apache.beam.sdk.coders.RowCoder; | ||
| import org.apache.beam.sdk.schemas.JavaFieldSchema; | ||
| import org.apache.beam.sdk.schemas.NoSuchSchemaException; | ||
| import org.apache.beam.sdk.schemas.Schema; | ||
| import org.apache.beam.sdk.schemas.Schema.FieldType; | ||
| import org.apache.beam.sdk.schemas.SchemaCoder; | ||
| import org.apache.beam.sdk.schemas.SchemaRegistry; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.SqlTypes; | ||
| import org.apache.beam.sdk.util.ByteStringOutputStream; | ||
| import org.apache.beam.sdk.values.Row; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
| import org.joda.time.DateTime; | ||
| import org.joda.time.DateTimeZone; | ||
| import org.joda.time.Instant; | ||
| 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.Warmup; | ||
|
|
||
| /** Benchmarks for {@link Row}, {@link RowCoder}, and {@link SchemaCoder}. */ | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| @Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @Fork(1) | ||
| public class RowAndCoderBenchmark { | ||
|
|
||
| public static class NestedPojo { | ||
| public String nestedStr = "inner"; | ||
| public long nestedLong = 999L; | ||
| public @Nullable String nestedNullable = null; | ||
|
|
||
| @Override | ||
| public boolean equals(@Nullable Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof NestedPojo)) { | ||
| return false; | ||
| } | ||
| NestedPojo that = (NestedPojo) o; | ||
| return nestedLong == that.nestedLong | ||
| && Objects.equals(nestedStr, that.nestedStr) | ||
| && Objects.equals(nestedNullable, that.nestedNullable); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(nestedStr, nestedLong, nestedNullable); | ||
| } | ||
| } | ||
|
|
||
| public static class ComplexPojo { | ||
| public String id = "user-12345"; | ||
| public int count = 42; | ||
| public double score = 98.6d; | ||
| public boolean active = true; | ||
| public @Nullable String optionalTag = null; | ||
| public List<String> tags = Arrays.asList("alpha", "beta", "gamma", "delta"); | ||
| public Map<String, Long> metrics = ImmutableMap.of("k1", 10L, "k2", 20L, "k3", 30L); | ||
| public NestedPojo nested = new NestedPojo(); | ||
|
|
||
| @Override | ||
| public boolean equals(@Nullable Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof ComplexPojo)) { | ||
| return false; | ||
| } | ||
| ComplexPojo that = (ComplexPojo) o; | ||
| return count == that.count | ||
| && Double.compare(that.score, score) == 0 | ||
| && active == that.active | ||
| && Objects.equals(id, that.id) | ||
| && Objects.equals(optionalTag, that.optionalTag) | ||
| && Objects.equals(tags, that.tags) | ||
| && Objects.equals(metrics, that.metrics) | ||
| && Objects.equals(nested, that.nested); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(id, count, score, active, optionalTag, tags, metrics, nested); | ||
| } | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class BenchmarkState { | ||
| Schema flatSchema; | ||
| Instant timestamp; | ||
| BigDecimal decimal; | ||
| Row flatRow; | ||
| Row flatRowCopy; | ||
| RowCoder flatRowCoder; | ||
| byte[] encodedFlatRow; | ||
|
|
||
| Schema nestedSchema; | ||
| Schema complexSchema; | ||
| LocalDate localDate; | ||
| List<String> tags; | ||
| Map<String, Long> metrics; | ||
| Row nestedRow; | ||
| Row complexRow; | ||
| Row complexRowCopy; | ||
| RowCoder complexRowCoder; | ||
| byte[] encodedComplexRow; | ||
|
|
||
| ComplexPojo complexPojo; | ||
| SchemaCoder<ComplexPojo> pojoSchemaCoder; | ||
| Row rowWithGetters; | ||
| byte[] encodedPojo; | ||
|
|
||
| ByteStringOutputStream outStream; | ||
|
|
||
| @Setup | ||
| public void setup() throws IOException, NoSuchSchemaException { | ||
| timestamp = new DateTime(2021, 11, 18, 8, 45, 38, DateTimeZone.UTC).toInstant(); | ||
| decimal = new BigDecimal("123.12"); | ||
|
|
||
| flatSchema = | ||
| Schema.builder() | ||
| .addByteField("f_byte") | ||
| .addInt16Field("f_int16") | ||
| .addInt32Field("f_int32") | ||
| .addInt64Field("f_int64") | ||
| .addFloatField("f_float") | ||
| .addDoubleField("f_double") | ||
| .addBooleanField("f_boolean") | ||
| .addStringField("f_string") | ||
| .addDecimalField("f_decimal") | ||
| .addDateTimeField("f_timestamp") | ||
| .build(); | ||
|
|
||
| flatRow = | ||
| Row.withSchema(flatSchema) | ||
| .addValues( | ||
| (byte) 12, | ||
| (short) 22, | ||
| 32, | ||
| 42L, | ||
| 1.02E5f, | ||
| 62.2d, | ||
| true, | ||
| "hello", | ||
| decimal, | ||
| timestamp) | ||
| .build(); | ||
| flatRowCopy = | ||
| Row.withSchema(flatSchema) | ||
| .addValues( | ||
| (byte) 12, | ||
| (short) 22, | ||
| 32, | ||
| 42L, | ||
| 1.02E5f, | ||
| 62.2d, | ||
| true, | ||
| "hello", | ||
| decimal, | ||
| timestamp) | ||
| .build(); | ||
|
|
||
| flatRowCoder = RowCoder.of(flatSchema); | ||
| outStream = new ByteStringOutputStream(256); | ||
| flatRowCoder.encode(flatRow, outStream); | ||
| encodedFlatRow = outStream.toByteStringAndReset().toByteArray(); | ||
|
|
||
| nestedSchema = | ||
| Schema.builder() | ||
| .addStringField("nested_str") | ||
| .addInt64Field("nested_long") | ||
| .addNullableField("nested_nullable", FieldType.STRING) | ||
| .build(); | ||
|
|
||
| complexSchema = | ||
| Schema.builder() | ||
| .addStringField("id") | ||
| .addInt32Field("count") | ||
| .addDoubleField("score") | ||
| .addBooleanField("active") | ||
| .addNullableField("optional_tag", FieldType.STRING) | ||
| .addLogicalTypeField("date", SqlTypes.DATE) | ||
| .addArrayField("tags", FieldType.STRING) | ||
| .addMapField("metrics", FieldType.STRING, FieldType.INT64) | ||
| .addRowField("nested", nestedSchema) | ||
| .build(); | ||
|
|
||
| localDate = LocalDate.of(2025, 10, 24); | ||
| tags = Arrays.asList("alpha", "beta", "gamma", "delta"); | ||
| metrics = ImmutableMap.of("k1", 10L, "k2", 20L, "k3", 30L); | ||
| nestedRow = Row.withSchema(nestedSchema).addValues("inner", 999L, null).build(); | ||
|
|
||
| complexRow = | ||
| Row.withSchema(complexSchema) | ||
| .addValues( | ||
| "user-12345", 42, 98.6d, true, null, localDate, tags, metrics, nestedRow) | ||
| .build(); | ||
| complexRowCopy = | ||
| Row.withSchema(complexSchema) | ||
| .addValues( | ||
| "user-12345", 42, 98.6d, true, null, localDate, tags, metrics, nestedRow) | ||
| .build(); | ||
|
|
||
| complexRowCoder = RowCoder.of(complexSchema); | ||
| complexRowCoder.encode(complexRow, outStream); | ||
| encodedComplexRow = outStream.toByteStringAndReset().toByteArray(); | ||
|
|
||
| SchemaRegistry registry = SchemaRegistry.createDefault(); | ||
| registry.registerSchemaProvider(ComplexPojo.class, new JavaFieldSchema()); | ||
| registry.registerSchemaProvider(NestedPojo.class, new JavaFieldSchema()); | ||
| complexPojo = new ComplexPojo(); | ||
| pojoSchemaCoder = registry.getSchemaCoder(ComplexPojo.class); | ||
| rowWithGetters = pojoSchemaCoder.getToRowFunction().apply(complexPojo); | ||
| pojoSchemaCoder.encode(complexPojo, outStream); | ||
| encodedPojo = outStream.toByteStringAndReset().toByteArray(); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Row buildFlatRow(BenchmarkState state) { | ||
| return Row.withSchema(state.flatSchema) | ||
| .addValues( | ||
| (byte) 12, | ||
| (short) 22, | ||
| 32, | ||
| 42L, | ||
| 1.02E5f, | ||
| 62.2d, | ||
| true, | ||
| "hello", | ||
| state.decimal, | ||
| state.timestamp) | ||
| .build(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Row buildComplexRow(BenchmarkState state) { | ||
| Row nested = Row.withSchema(state.nestedSchema).addValues("inner", 999L, null).build(); | ||
| return Row.withSchema(state.complexSchema) | ||
| .addValues( | ||
| "user-12345", | ||
| 42, | ||
| 98.6d, | ||
| true, | ||
| null, | ||
| state.localDate, | ||
| state.tags, | ||
| state.metrics, | ||
| nested) | ||
| .build(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int complexRowEqualsAndHashCode(BenchmarkState state) { | ||
| boolean eq = state.complexRow.equals(state.complexRowCopy); | ||
| return eq ? state.complexRow.hashCode() : 0; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int readRowWithGettersRepeatedly(BenchmarkState state) { | ||
| Row r = state.rowWithGetters; | ||
| // Read nested row and collection/map fields multiple times on the same RowWithGetters | ||
| Row nested1 = r.getRow(7); | ||
| Row nested2 = r.getRow(7); | ||
| return r.getArray(5).size() | ||
| + r.getMap(6).size() | ||
| + nested1.getString(0).length() | ||
| + nested2.getString(0).length(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int encodeFlatRow(BenchmarkState state) throws IOException { | ||
| state.outStream.reset(); | ||
| state.flatRowCoder.encode(state.flatRow, state.outStream); | ||
| return state.outStream.size(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Row decodeFlatRow(BenchmarkState state) throws IOException { | ||
| return state.flatRowCoder.decode(new ByteArrayInputStream(state.encodedFlatRow)); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int encodeComplexRow(BenchmarkState state) throws IOException { | ||
| state.outStream.reset(); | ||
| state.complexRowCoder.encode(state.complexRow, state.outStream); | ||
| return state.outStream.size(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Row decodeComplexRow(BenchmarkState state) throws IOException { | ||
| return state.complexRowCoder.decode(new ByteArrayInputStream(state.encodedComplexRow)); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public int encodePojoViaSchemaCoder(BenchmarkState state) throws IOException { | ||
| state.outStream.reset(); | ||
| state.pojoSchemaCoder.encode(state.complexPojo, state.outStream); | ||
| return state.outStream.size(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public ComplexPojo decodePojoViaSchemaCoder(BenchmarkState state) throws IOException { | ||
| return state.pojoSchemaCoder.decode(new ByteArrayInputStream(state.encodedPojo)); | ||
| } | ||
| } | ||
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.
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.
field indices are not defined when using Pojo schemas - Java doesn't guarantee iteration order. Get the fields by name instead.