Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove box indirection in Ropes #4040

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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