Repository navigation
Description
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 |
git bisectbetween v2.14.0 (good) and v2.14.2 (bad) points to 3fd7408 (Fix Encryption fails when referencing self defined types (#2254) #2255, fix for [AVRO] Encryption fails when referencing self defined types #2254). Removing only the per-messageexpand_schemacall brings the timing back to the v2.14.0 level.- With py-spy (native frames) on 2.16.1,
expand_schematakes 25% of the samples and the Avro decode itself takes 30%. - Master still makes the per-message call.
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