From 2072188b320e563e1f50a01bb69e2d8684dbb1c9 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Sat, 17 Aug 2024 10:04:16 +0200 Subject: [PATCH] more efficent rope serialization --- Cargo.lock | 1 + turbopack/crates/turbo-tasks-fs/Cargo.toml | 1 + turbopack/crates/turbo-tasks-fs/src/rope.rs | 8 ++++++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 642d01190516e..52f2872b81c97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8651,6 +8651,7 @@ dependencies = [ "parking_lot", "rstest", "serde", + "serde_bytes", "serde_json", "serde_path_to_error", "sha2", diff --git a/turbopack/crates/turbo-tasks-fs/Cargo.toml b/turbopack/crates/turbo-tasks-fs/Cargo.toml index 96c29bf75b03e..c2d63dca2bf6d 100644 --- a/turbopack/crates/turbo-tasks-fs/Cargo.toml +++ b/turbopack/crates/turbo-tasks-fs/Cargo.toml @@ -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 } diff --git a/turbopack/crates/turbo-tasks-fs/src/rope.rs b/turbopack/crates/turbo-tasks-fs/src/rope.rs index 24def58425c35..52dd8ed904755 100644 --- a/turbopack/crates/turbo-tasks-fs/src/rope.rs +++ b/turbopack/crates/turbo-tasks-fs/src/rope.rs @@ -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}; @@ -343,14 +344,17 @@ impl Serialize for Rope { fn serialize(&self, serializer: S) -> Result { 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>(deserializer: D) -> Result { - let bytes = >::deserialize(deserializer)?; + let bytes = ByteBuf::deserialize(deserializer)?.into_vec(); Ok(Rope::from(bytes)) } }