From 9771a60a2aca6613628855e91aa8b012483b106c Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Thu, 2 Mar 2023 13:41:25 -0500 Subject: [PATCH] Remove box indirection in Ropes (vercel/turbo#4040) --- crates/turbo-tasks-fs/src/rope.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/turbo-tasks-fs/src/rope.rs b/crates/turbo-tasks-fs/src/rope.rs index e9ab62aa9ef36..83c5be299ce0b 100644 --- a/crates/turbo-tasks-fs/src/rope.rs +++ b/crates/turbo-tasks-fs/src/rope.rs @@ -38,8 +38,8 @@ pub struct Rope { /// An Arc container for ropes. This indirection allows for easily sharing the /// contents between Ropes (and also RopeBuilders/RopeReaders). -#[derive(Clone, Debug, Default)] -struct InnerRope(Arc>); +#[derive(Clone, Debug)] +struct InnerRope(Arc<[RopeElem]>); /// Differentiates the types of stored bytes in a rope. #[derive(Clone, Debug)] @@ -120,7 +120,7 @@ impl> From for Rope { } else { Rope { length: bytes.len(), - data: InnerRope::from(Box::from([Local(bytes)])), + data: InnerRope(Arc::from([Local(bytes)])), } } } @@ -202,7 +202,7 @@ impl RopeBuilder { self.finish(); Rope { length: self.length, - data: InnerRope::from(self.committed.into_boxed_slice()), + data: InnerRope::from(self.committed), } } } @@ -453,6 +453,12 @@ impl InnerRope { } } +impl Default for InnerRope { + fn default() -> Self { + InnerRope(Arc::from([])) + } +} + impl DeterministicHash for InnerRope { /// Ropes with similar contents hash the same, regardless of their /// structure. Notice the InnerRope does not contain a length (and any @@ -465,8 +471,8 @@ impl DeterministicHash for InnerRope { } } -impl From> for InnerRope { - fn from(els: Box<[RopeElem]>) -> Self { +impl From> for InnerRope { + fn from(els: Vec) -> Self { if cfg!(debug_assertions) { // It's important that an InnerRope never contain an empty Bytes section. for el in els.iter() { @@ -482,12 +488,12 @@ impl From> for InnerRope { } } } - InnerRope(Arc::new(els)) + InnerRope(Arc::from(els)) } } impl Deref for InnerRope { - type Target = Arc>; + type Target = Arc<[RopeElem]>; fn deref(&self) -> &Self::Target { &self.0 @@ -699,7 +705,7 @@ mod test { } impl From> for RopeElem { fn from(value: Vec) -> Self { - RopeElem::Shared(InnerRope::from(value.into_boxed_slice())) + RopeElem::Shared(InnerRope::from(value)) } } impl From for RopeElem { @@ -709,7 +715,7 @@ mod test { } impl Rope { fn new(value: Vec) -> Self { - let data = InnerRope::from(value.into_boxed_slice()); + let data = InnerRope::from(value); Rope { length: data.len(), data,