From 8edc7242b0b72853bccb3f1c1c841e81ebb3c6b3 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 26 Oct 2026 19:45:21 +0100 Subject: decode sync messages lazily Sync messages are now being decoded only where they are applied: after membership has been checked, in the sync lane's blocking task. Debug now shows only the size instead of the whole message. --- src/transport/mod.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/transport/mod.rs') diff --git a/src/transport/mod.rs b/src/transport/mod.rs index a040993..0d761e0 100644 --- a/src/transport/mod.rs +++ b/src/transport/mod.rs @@ -326,13 +326,37 @@ pub struct SyncEnvelope { /// ignores an epoch older than the newest it has seen in the same session. pub epoch_seq: u32, #[serde(with = "crate::transport::wire::codec::sync_message")] - pub message: Message, + pub message: SyncPayload, /// The sender was ready when it generated the message: its document is the swarm's, /// not one it is still filling (joining, rejoining). Only such a peer can tell a /// catching-up node that it has everything; an empty document says nothing else. pub ready: bool, } +/// An Automerge sync message in Automerge's own encoding. Decoded only where it is +/// applied (`decode`), after the sender proved to be a member and off the consumer: a +/// large message from anyone else costs nothing but its bytes. +#[derive(Clone)] +pub struct SyncPayload(pub bytes::Bytes); + +impl SyncPayload { + pub fn encode(message: Message) -> Self { + Self(message.encode().into()) + } + + pub fn decode(&self) -> Result { + Message::decode(&self.0) + .map_err(|error| anyhow::anyhow!("Automerge decode failed: {error:?}")) + } +} + +/// Sizes instead of contents: logged messages stay short. +impl std::fmt::Debug for SyncPayload { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "SyncPayload({} bytes)", self.0.len()) + } +} + /// A message serialized for the wire. Cloning is cheap (shared buffer). #[derive(Clone, Debug)] pub struct EncodedMessage { -- cgit v1.3.1