Skip to content

Commit

Permalink
unifying union checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuckame committed Jun 25, 2024
1 parent 386dccb commit c912a1b
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ internal inline fun <T : Any> AvroDecoder.findValueDecoder(
val schema = currentWriterSchema

val foundResolver =
if (schema.type == Schema.Type.UNION) {
if (schema.isUnion) {
if (this is UnionDecoder) {
decodeAndResolveUnion()
resolver(currentWriterSchema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public inline fun <T : Any> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<Schema> = if (this.type == Schema.Type.UNION) this.types.toList() else emptyList(),
types: List<Schema> = if (this.isUnion) this.types.toList() else emptyList(),
enumSymbols: List<String> = if (this.type == Schema.Type.ENUM) this.enumSymbols.toList() else emptyList(),
fields: List<Schema.Field>? = 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c912a1b

Please sign in to comment.