repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

refactor(core): compress pass over session arena

6290472ae62ea581ac7a4291dd9f33e40f9d1d6c

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+16 / −29 text lines

Diff layout

smith-core/src/frame.rs

Modified

+3 / −3 text lines

9898 Self::Known { entry } => encode_frame(
9999 KIND_ENTRY,
100100 VERSION_ENTRY,
101 &entry.wire(),
101 entry,
102102 entry.content.len(),
103103 max_frame_bytes,
104104 ),
583583 assert_eq!(entry.kind, EntryKind::ToolResult { call_id, ok: true });
584584 assert_eq!(EntryContent::decode(&entry.content).unwrap(), content);
585585 // The content is embedded verbatim after a CBOR byte-string header.
586 let payload = Frame::encode_payload(&entry.wire(), DEFAULT_MAX_FRAME_BYTES).unwrap();
586 let payload = Frame::encode_payload(&entry, DEFAULT_MAX_FRAME_BYTES).unwrap();
587587 let at = payload
588588 .windows(entry.content.len())
589589 .position(|window| window == entry.content.as_slice())
714714 fn version_one_frames_without_body_prefix_are_rejected() {
715715 // Old layout: [len][CBOR payload] with no kind, version, or checksum.
716716 let entry = sample_entry(false);
717 let payload = Frame::encode_payload(&entry.wire(), DEFAULT_MAX_FRAME_BYTES).unwrap();
717 let payload = Frame::encode_payload(&entry, DEFAULT_MAX_FRAME_BYTES).unwrap();
718718 let mut bytes = u32::try_from(payload.len()).unwrap().to_le_bytes().to_vec();
719719 bytes.extend_from_slice(&payload);
720720 let recovery = read_all(&bytes, DEFAULT_MAX_FRAME_BYTES);

smith-core/src/session.rs

Modified

+13 / −26 text lines

151151/// Wire shape of one entry frame: the record plus its content as an opaque
152152/// canonical CBOR byte string.
153#[derive(Clone, PartialEq, Eq, Debug, Deserialize)]
153#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
154154pub struct EntryFrame {
155155 /// Entry ID.
156156 pub id: EntryId,
161161 /// What the entry recorded.
162162 pub kind: EntryKind,
163163 /// Canonical CBOR of the [`EntryContent`].
164 #[serde(deserialize_with = "byte_buf")]
164 #[serde(serialize_with = "cbor_bytes", deserialize_with = "byte_buf")]
165165 pub content: Vec<u8>,
182182 content: bytes,
183183 })
184184 }
186 pub(crate) fn wire(&self) -> EntryWire<'_> {
187 EntryWire {
188 id: self.id,
189 parent: self.parent,
190 timestamp_ms: self.timestamp_ms,
191 kind: self.kind,
192 content: CborBytes(&self.content),
193 }
194 }
197/// Borrowed serialization view of an entry frame, so arena bytes are
198/// framed without being copied out first.
187/// Borrowed [`EntryFrame`] serialization of a session record and its arena
188/// bytes, so arena content is framed without being copied out first.
199189#[derive(Serialize)]
200190pub(crate) struct EntryWire<'a> {
201191 id: EntryId,
202192 parent: Option<EntryId>,
203193 timestamp_ms: i64,
204194 kind: EntryKind,
205 content: CborBytes<'a>,
195 #[serde(serialize_with = "cbor_bytes")]
196 content: &'a [u8],
208199impl<'a> EntryWire<'a> {
212203 parent: entry.parent,
213204 timestamp_ms: entry.timestamp_ms,
214205 kind: entry.kind,
215 content: CborBytes(content),
206 content,
216207 }
217208 }
220/// Bytes serialized as one CBOR byte string rather than an integer array.
221struct CborBytes<'a>(&'a [u8]);
223impl Serialize for CborBytes<'_> {
224 fn serialize<S: serde::Serializer>(
225 &self,
226 serializer: S,
227 ) -> std::result::Result<S::Ok, S::Error> {
228 serializer.serialize_bytes(self.0)
229 }
211/// Serialize bytes as one CBOR byte string rather than an integer array.
212fn cbor_bytes<S: serde::Serializer>(
213 bytes: &impl AsRef<[u8]>,
214 serializer: S,
215) -> std::result::Result<S::Ok, S::Error> {
216 serializer.serialize_bytes(bytes.as_ref())
232219fn byte_buf<'de, D: serde::Deserializer<'de>>(