Optimized pydantic use

All protocol classes have now been duplicated in order to minimize the
use of pydantic validation

The "pure" classes are named what the previous validated classes were,
now each transport has its transport name prefixed on the class.

For example before we had Heartbeat, now we have Heartbeat and
JsonHeartbeat

This new architecture allows us to reduce the number of useless backend
validation steps created when copying and processing data, reducing CPU
stress by roughly 10-20% in real world testing
This commit is contained in:
h3cx committed 2026-09-17 22:22:42 +02:00
1 parent 207a91811e
commit 2e8155142e
11 files changed
+359 -88

No files matched your search

+13 -6
View File
@@ -13,6 +13,13 @@ from rich.logging import RichHandler
from dynalab_core.protocols.common import VersionDescriptor
from dynalab_core.protocols.constants import PROTOCOL_VERSION
from dynalab_core.protocols.json.wire import read_message, write_message
from dynalab_core.protocols.packets import (
JsonConnectorHello,
JsonDynaLabHello,
JsonHeartbeat,
JsonValueBatch,
JsonValueDescriptor,
)
from dynalab_core.protocols.packets.data import ValueBatch, ValueDescriptor
from dynalab_core.protocols.packets.handshake import (
ConnectorHello,
@@ -124,13 +131,13 @@ def value_sender_thread(
tau * SINE_FREQUENCY_HZ * (perf_counter() - sine_start_time)
)
timestamp = monotonic_ns()
message = ValueBatch(values=[])
value1 = ValueDescriptor(
message = JsonValueBatch(values=[])
value1 = JsonValueDescriptor(
signal_id=signal_id_1,
value=2.0 * sine_value,
timestamp=timestamp,
)
value2 = ValueDescriptor(
value2 = JsonValueDescriptor(
signal_id=signal_id_2,
value=sine_value,
timestamp=timestamp,
@@ -203,7 +210,7 @@ async def perform_handshake(
except TimeoutError as exc:
raise TimeoutError("Timed out waiting for server hello") from exc
if not isinstance(message, DynaLabHello):
if not isinstance(message, JsonDynaLabHello):
raise RuntimeError(f"Expected DynaLabHello, received {type(message).__name__}")
log.debug(
@@ -211,7 +218,7 @@ async def perform_handshake(
message.instance_id,
)
connector_hello = ConnectorHello(
connector_hello = JsonConnectorHello(
connector_uuid=uuid4(),
protocol_version=PROTOCOL_VERSION,
connector_name="Test connector",
@@ -252,7 +259,7 @@ async def receive_messages(
while True:
message = await read_message(reader)
if isinstance(message, Heartbeat):
if isinstance(message, JsonHeartbeat):
message.return_timestamp = monotonic_ms()
await send_message(