From c912a1bbd52206bff9f7b9d30e64fdf14efdf6e1 Mon Sep 17 00:00:00 2001 From: Chuckame Date: Tue, 25 Jun 2024 19:22:29 +0200 Subject: [PATCH] unifying union checks --- benchmark/README.md | 16 ++++++++-------- .../com/github/avrokotlin/avro4k/AvroDecoder.kt | 2 +- .../com/github/avrokotlin/avro4k/AvroEncoder.kt | 2 +- .../encoder/direct/AbstractAvroDirectEncoder.kt | 4 ++-- .../generic/AbstractAvroGenericEncoder.kt | 4 ++-- .../github/avrokotlin/avro4k/internal/helpers.kt | 2 +- .../avro4k/internal/schema/ValueVisitor.kt | 2 +- .../avrokotlin/avro4k/RecordBuilderForTest.kt | 2 +- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/benchmark/README.md b/benchmark/README.md index 15a32391..677b8604 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -25,14 +25,14 @@ Computer: Macbook air M2 ``` Benchmark Mode Cnt Score Error Units Relative Difference (%) -ApacheAvroReflectBenchmark.read thrpt 5 20073.322 ± 878.268 ops/s +1.70% -Avro4kBenchmark.read thrpt 5 19738.061 ± 930.026 ops/s 0.00% -Avro4kGenericWithApacheAvroBenchmark.read thrpt 5 7538.287 ± 112.690 ops/s -61.78% - -Avro4kBenchmark.write thrpt 5 40780.118 ± 4136.921 ops/s 0.00% -ApacheAvroReflectBenchmark.write thrpt 5 37632.786 ± 117.940 ops/s -7.72% -JacksonAvroBenchmark.write thrpt 5 30544.663 ± 1004.357 ops/s -25.08% -Avro4kGenericWithApacheAvroBenchmark.write thrpt 5 21088.555 ± 1280.548 ops/s -48.31% +Avro4kBenchmark.read thrpt 5 20537.185 ± 135.318 ops/s 0.00% +ApacheAvroReflectBenchmark.read thrpt 5 20059.982 ± 241.854 ops/s -2.32% +Avro4kGenericWithApacheAvroBenchmark.read thrpt 5 7591.527 ± 172.173 ops/s -63.03% + +Avro4kBenchmark.write thrpt 5 41215.703 ± 1274.692 ops/s 0.00% +ApacheAvroReflectBenchmark.write thrpt 5 37188.260 ± 115.447 ops/s -9.74% +JacksonAvroBenchmark.write thrpt 5 30757.363 ± 1557.034 ops/s -25.39% +Avro4kGenericWithApacheAvroBenchmark.write thrpt 5 21305.149 ± 830.640 ops/s -48.33% ``` > [!WARNING] diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroDecoder.kt index f03a4da5..6e130ad7 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroDecoder.kt @@ -296,7 +296,7 @@ internal inline fun AvroDecoder.findValueDecoder( val schema = currentWriterSchema val foundResolver = - if (schema.type == Schema.Type.UNION) { + if (schema.isUnion) { if (this is UnionDecoder) { decodeAndResolveUnion() resolver(currentWriterSchema) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroEncoder.kt index 08e3a299..c3b478da 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroEncoder.kt @@ -71,7 +71,7 @@ public inline fun AvroEncoder.encodeResolving( resolver: (Schema) -> (() -> T)?, ): T { val schema = currentWriterSchema - return if (schema.type == Schema.Type.UNION) { + return if (schema.isUnion) { resolveUnion(schema, error, resolver) } else { resolver(schema)?.invoke() ?: throw error() diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/direct/AbstractAvroDirectEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/direct/AbstractAvroDirectEncoder.kt index 1b49b445..6bef64d0 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/direct/AbstractAvroDirectEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/direct/AbstractAvroDirectEncoder.kt @@ -52,7 +52,7 @@ internal sealed class AbstractAvroDirectEncoder( private fun Schema.isTypeOfBytes() = type == Schema.Type.BYTES || - type == Schema.Type.UNION && types.any { it.type == Schema.Type.BYTES } + isUnion && types.any { it.type == Schema.Type.BYTES } override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder { return when (descriptor.kind) { @@ -124,7 +124,7 @@ internal sealed class AbstractAvroDirectEncoder( if (selectedUnionIndex > -1) { throw SerializationException("Already selected union index: $selectedUnionIndex, got $index, for selected schema $currentWriterSchema") } - if (currentWriterSchema.type == Schema.Type.UNION) { + if (currentWriterSchema.isUnion) { binaryEncoder.writeIndex(index) selectedUnionIndex = index currentWriterSchema = currentWriterSchema.types[index] diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/generic/AbstractAvroGenericEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/generic/AbstractAvroGenericEncoder.kt index eee38721..f3f33679 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/generic/AbstractAvroGenericEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/encoder/generic/AbstractAvroGenericEncoder.kt @@ -44,7 +44,7 @@ internal abstract class AbstractAvroGenericEncoder : AbstractEncoder(), AvroEnco if (selectedUnionIndex > -1) { throw SerializationException("Already selected union index: $selectedUnionIndex, got $index, for selected schema $currentWriterSchema") } - if (currentWriterSchema.type == Schema.Type.UNION) { + if (currentWriterSchema.isUnion) { selectedUnionIndex = index currentWriterSchema = currentWriterSchema.types[index] } else { @@ -60,7 +60,7 @@ internal abstract class AbstractAvroGenericEncoder : AbstractEncoder(), AvroEnco value: T, ) { if (currentWriterSchema.type == Schema.Type.BYTES || - currentWriterSchema.type == Schema.Type.UNION && currentWriterSchema.types.any { it.type == Schema.Type.BYTES } + currentWriterSchema.isUnion && currentWriterSchema.types.any { it.type == Schema.Type.BYTES } ) { when (value) { is ByteArray -> encodeBytes(value) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/helpers.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/helpers.kt index f6ace386..f298ef62 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/helpers.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/helpers.kt @@ -81,7 +81,7 @@ internal fun Schema.copy( namespace: String? = if (this.type == Schema.Type.RECORD || this.type == Schema.Type.ENUM || this.type == Schema.Type.FIXED) this.namespace else null, aliases: Set = if (this.type == Schema.Type.RECORD || this.type == Schema.Type.ENUM || this.type == Schema.Type.FIXED) this.aliases else emptySet(), isError: Boolean = if (this.type == Schema.Type.RECORD) this.isError else false, - types: List = if (this.type == Schema.Type.UNION) this.types.toList() else emptyList(), + types: List = if (this.isUnion) this.types.toList() else emptyList(), enumSymbols: List = if (this.type == Schema.Type.ENUM) this.enumSymbols.toList() else emptyList(), fields: List? = if (this.type == Schema.Type.RECORD && this.hasFields()) this.fields.map { it.copy() } else null, enumDefault: String? = if (this.type == Schema.Type.ENUM) this.enumDefault else null, diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/schema/ValueVisitor.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/schema/ValueVisitor.kt index db224618..6318b9ff 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/internal/schema/ValueVisitor.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/internal/schema/ValueVisitor.kt @@ -124,7 +124,7 @@ internal class ValueVisitor internal constructor( } private fun Schema.toNullableSchema(): Schema { - return if (this.type == Schema.Type.UNION) { + return if (this.isUnion) { Schema.createUnion(listOf(Schema.create(Schema.Type.NULL)) + this.types) } else { Schema.createUnion(Schema.create(Schema.Type.NULL), this) diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/RecordBuilderForTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/RecordBuilderForTest.kt index ae87951f..f4f7243e 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/RecordBuilderForTest.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/RecordBuilderForTest.kt @@ -94,6 +94,6 @@ internal fun recordWithSchema( private val Schema.nonNull: Schema get() = when { - type == Schema.Type.UNION && isNullable -> this.types.filter { it.type != Schema.Type.NULL }.let { if (it.size > 1) Schema.createUnion(it) else it[0] } + isUnion && isNullable -> this.types.filter { it.type != Schema.Type.NULL }.let { if (it.size > 1) Schema.createUnion(it) else it[0] } else -> this } \ No newline at end of file