Skip to content

Commit

Permalink
Remove box indirection in Ropes (vercel/turborepo#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell authored Mar 2, 2023
1 parent 8d0b50c commit 9771a60
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions crates/turbo-tasks-fs/src/rope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<[RopeElem]>>);
#[derive(Clone, Debug)]
struct InnerRope(Arc<[RopeElem]>);

/// Differentiates the types of stored bytes in a rope.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<T: Into<Bytes>> From<T> for Rope {
} else {
Rope {
length: bytes.len(),
data: InnerRope::from(Box::from([Local(bytes)])),
data: InnerRope(Arc::from([Local(bytes)])),
}
}
}
Expand Down Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -465,8 +471,8 @@ impl DeterministicHash for InnerRope {
}
}

impl From<Box<[RopeElem]>> for InnerRope {
fn from(els: Box<[RopeElem]>) -> Self {
impl From<Vec<RopeElem>> for InnerRope {
fn from(els: Vec<RopeElem>) -> Self {
if cfg!(debug_assertions) {
// It's important that an InnerRope never contain an empty Bytes section.
for el in els.iter() {
Expand All @@ -482,12 +488,12 @@ impl From<Box<[RopeElem]>> for InnerRope {
}
}
}
InnerRope(Arc::new(els))
InnerRope(Arc::from(els))
}
}

impl Deref for InnerRope {
type Target = Arc<Box<[RopeElem]>>;
type Target = Arc<[RopeElem]>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down Expand Up @@ -699,7 +705,7 @@ mod test {
}
impl From<Vec<RopeElem>> for RopeElem {
fn from(value: Vec<RopeElem>) -> Self {
RopeElem::Shared(InnerRope::from(value.into_boxed_slice()))
RopeElem::Shared(InnerRope::from(value))
}
}
impl From<Rope> for RopeElem {
Expand All @@ -709,7 +715,7 @@ mod test {
}
impl Rope {
fn new(value: Vec<RopeElem>) -> Self {
let data = InnerRope::from(value.into_boxed_slice());
let data = InnerRope::from(value);
Rope {
length: data.len(),
data,
Expand Down

0 comments on commit 9771a60

Please sign in to comment.