Skip to content

Commit

Permalink
more efficent rope serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 4, 2024
1 parent f3fff62 commit 2072188
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mime = { workspace = true }
notify = { workspace = true }
parking_lot = { workspace = true }
serde = { workspace = true, features = ["rc"] }
serde_bytes = { workspace = true }
serde_json = { workspace = true }
serde_path_to_error = { workspace = true }
tokio = { workspace = true }
Expand Down
8 changes: 6 additions & 2 deletions turbopack/crates/turbo-tasks-fs/src/rope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use anyhow::{Context, Result};
use bytes::{Buf, Bytes};
use futures::Stream;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_bytes::ByteBuf;
use tokio::io::{AsyncRead, ReadBuf};
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};
use RopeElem::{Local, Shared};
Expand Down Expand Up @@ -343,14 +344,17 @@ impl Serialize for Rope {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::Error;
let bytes = self.to_bytes().map_err(Error::custom)?;
bytes.serialize(serializer)
match bytes {
Cow::Borrowed(b) => serde_bytes::Bytes::new(b).serialize(serializer),
Cow::Owned(b) => ByteBuf::from(b).serialize(serializer),
}
}
}

impl<'de> Deserialize<'de> for Rope {
/// Deserializes strings into a contiguous, immutable Rope.
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let bytes = <Vec<u8>>::deserialize(deserializer)?;
let bytes = ByteBuf::deserialize(deserializer)?.into_vec();
Ok(Rope::from(bytes))
}
}
Expand Down

0 comments on commit 2072188

Please sign in to comment.