Skip to content

Commit 8868d69

Browse files
committed
doc: Postcard codec for ephemeral data
This should make ephemeral messaging faster, since less bytes are transferred using postcard instead of CBOR.
1 parent a60555e commit 8868d69

3 files changed

Lines changed: 7 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

reflection-doc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ hex = "0.4.3"
1515
indexmap = "2.13.0"
1616
loro = "1.13.9"
1717
p2panda-core = "0.7.1"
18+
postcard = { version = "1.1.3", features = ["alloc"] }
1819
rand = "0.10.1"
1920
reflection-node = { path = "../reflection-node" }
2021
serde = { version = "1.0.228", features = ["derive"] }

reflection-doc/src/document.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use glib::subclass::{Signal, prelude::*};
88
use glib::{Properties, clone};
99
pub use hex::FromHexError;
1010
use loro::{ExportMode, LoroDoc, LoroText, event::Diff};
11-
use p2panda_core::cbor::{decode_cbor, encode_cbor};
1211
use p2panda_core::{self, Topic};
1312
use reflection_node::{TopicStream, TopicSubscription, TopicSubscriptionError};
1413
use tracing::error;
@@ -98,14 +97,9 @@ impl DocumentId {
9897
}
9998

10099
#[derive(Debug, serde::Serialize, serde::Deserialize)]
101-
#[serde(tag = "t", content = "d")]
102100
enum EphemeralData {
103-
#[serde(rename = "cursor")]
104101
Cursor {
105-
#[serde(rename = "i")]
106102
insert_cursor: Option<loro::cursor::Cursor>,
107-
108-
#[serde(rename = "s")]
109103
selection_bound: Option<loro::cursor::Cursor>,
110104
},
111105
}
@@ -290,17 +284,17 @@ mod imp {
290284
*self.insert_cursor.write().unwrap() = insert_cursor;
291285

292286
if send {
293-
self.brodcast_ephemeral();
287+
self.broadcast_ephemeral();
294288
}
295289
}
296290

297-
pub fn brodcast_ephemeral(&self) {
291+
pub fn broadcast_ephemeral(&self) {
298292
let cursor_data = EphemeralData::Cursor {
299293
insert_cursor: self.insert_cursor.read().unwrap().clone(),
300294
selection_bound: self.selection_bound.read().unwrap().clone(),
301295
};
302296

303-
let cursor_bytes = match encode_cbor(&cursor_data) {
297+
let cursor_bytes = match postcard::to_allocvec(&cursor_data) {
304298
Ok(data) => data,
305299
Err(error) => {
306300
error!("Failed to serialize cursor: {}", error);
@@ -943,7 +937,7 @@ impl TopicSubscription for DocumentHandle {
943937
let author = document.authors().add(VerifyingKey(author));
944938
author.set_online(true);
945939
// When a new author joins we need to send ephemeral messages again
946-
document.imp().brodcast_ephemeral();
940+
document.imp().broadcast_ephemeral();
947941
});
948942
}
949943
}
@@ -967,7 +961,7 @@ impl TopicSubscription for DocumentHandle {
967961
) {
968962
if let Some(document) = self.0.upgrade() {
969963
document.main_context().invoke(move || {
970-
if let Ok(data) = decode_cbor(&data[..])
964+
if let Ok(data) = postcard::from_bytes(&data[..])
971965
&& let Some(author) = document.authors().author(&VerifyingKey(author))
972966
{
973967
document

0 commit comments

Comments
 (0)