Skip to content

AvroSerializer and AvroDeserializer run expand_schema and get_inline_tags on every message (slower since 2.14.2) #2458

Backlog

Description

@alexon1234

Description

Since 2.14.2, AvroDeserializer.__call__ runs fastavro.expand_schema(reader_schema) on every message. It runs whether or not the subject has rules, and its result is only used by field-level rule transforms. get_inline_tags(reader_schema) also walks the whole schema on every message. AvroSerializer does the same with use.latest.version. Both results depend only on the schema, so this work is repeated on every message.

Kafka consumption without decoding is not slower. Only Avro decoding is, and the cost grows with schema size. Local benchmarks, 2.12.2 vs 2.16.1:

Path 2.12.2 2.16.1
Avro decode, 7-field schema 6.9 µs/msg 10.2 µs/msg
Avro decode, 61-field schema 29.5 µs/msg 50.0 µs/msg

How to reproduce

MockSchemaRegistryClient, one 61-field schema, the same message deserialized 50,001 times:

"""Time AvroDeserializer per message and count fastavro.expand_schema calls."""
import io
import json
import struct
import time

import fastavro
import fastavro.schema

import confluent_kafka.schema_registry._sync.avro as avro_module
from confluent_kafka.schema_registry import Schema
from confluent_kafka.schema_registry._sync.mock_schema_registry_client import MockSchemaRegistryClient
from confluent_kafka.schema_registry.avro import AvroDeserializer
from confluent_kafka.serialization import MessageField, SerializationContext

MESSAGES = 50_000

fields = [{"name": f"f{i}", "type": ["null", "string"] if i % 2 else "long"} for i in range(50)]
nested_fields = [{"name": f"n{i}", "type": "string"} for i in range(10)]
fields.append({"name": "nested", "type": {"type": "record", "name": "Nested", "fields": nested_fields}})
schema = {"type": "record", "name": "Wide", "fields": fields}
record = {f"f{i}": (f"v{i}" if i % 2 else i) for i in range(50)}
record["nested"] = {f"n{i}": "x" for i in range(10)}

registry = MockSchemaRegistryClient({"url": "mock://"})
schema_id = registry.register_schema("topic-value", Schema(json.dumps(schema), "AVRO"))
buffer = io.BytesIO()
buffer.write(struct.pack(">bI", 0, schema_id))
fastavro.schemaless_writer(buffer, fastavro.parse_schema(schema), record)
message = buffer.getvalue()

expand_schema_calls = 0
if hasattr(avro_module, "expand_schema"):
    def counting_expand_schema(schema):
        global expand_schema_calls
        expand_schema_calls += 1
        return fastavro.schema.expand_schema(schema)

    avro_module.expand_schema = counting_expand_schema

deserializer = AvroDeserializer(registry)
ctx = SerializationContext("topic", MessageField.VALUE)
assert deserializer(message, ctx) == record

start = time.perf_counter()
for _ in range(MESSAGES):
    deserializer(message, ctx)
elapsed = time.perf_counter() - start
print(f"{elapsed / MESSAGES * 1e6:.1f} us/message, "
      f"{expand_schema_calls} expand_schema calls for {MESSAGES + 1} messages")

Python 3.13, fastavro 1.13.1, x86_64 Linux, confluent-kafka 2.16.1: 50.1 us/message, 50001 expand_schema calls for 50001 messages.

Checklist

  • confluent-kafka-python and librdkafka version: confluent-kafka 2.16.1 (PyPI wheel, bundled librdkafka); also reproduced on master
  • Apache Kafka broker version: not applicable, no broker is needed (MockSchemaRegistryClient)
  • Client configuration: AvroDeserializer(registry) with the default configuration
  • Operating system: x86_64 Linux and macOS arm64
  • Provide client logs: not applicable
  • Provide broker log excerpts: not applicable
  • Critical issue

Activity

  1. added
    component:schema-registry
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    component:schema-registry

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions