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

[DO NOT MERGE] syntax: try ref-counting the AST. #58482

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/libsyntax/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ impl HasAttrs for ThinVec<Attribute> {
}
}

impl<T: HasAttrs + 'static> HasAttrs for P<T> {
impl<T: Clone + HasAttrs + 'static> HasAttrs for P<T> {
fn attrs(&self) -> &[Attribute] {
(**self).attrs()
}
Expand Down
71 changes: 45 additions & 26 deletions src/libsyntax/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,42 @@ use std::{mem, ptr, slice, vec};

use serialize::{Encodable, Decodable, Encoder, Decoder};

use rustc_data_structures::sync::Lrc;
use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
HashStable};
/// An owned smart pointer.
#[derive(Hash, PartialEq, Eq)]
pub struct P<T: ?Sized> {
ptr: Box<T>
ptr: Lrc<T>
}

#[allow(non_snake_case)]
/// Construct a `P<T>` from a `T` value.
pub fn P<T: 'static>(value: T) -> P<T> {
P {
ptr: Box::new(value)
ptr: Lrc::new(value)
}
}

impl<T: 'static> P<T> {
impl<T: 'static + Clone> P<T> {
/// Move out of the pointer.
/// Intended for chaining transformations not covered by `map`.
pub fn and_then<U, F>(self, f: F) -> U where
F: FnOnce(T) -> U,
{
f(*self.ptr)
f(self.into_inner())
}
/// Equivalent to and_then(|x| x)
pub fn into_inner(self) -> T {
*self.ptr
Lrc::try_unwrap(self.ptr).unwrap_or_else(|ptr| (*ptr).clone())
}

/// Produce a new `P<T>` from `self` without reallocating.
pub fn map<F>(mut self, f: F) -> P<T> where
F: FnOnce(T) -> T,
{
let p: *mut T = &mut *self.ptr;
// FIXME(eddyb) How can we reuse the original if unchanged?
let p: *mut T = Lrc::make_mut(&mut self.ptr);

// Leak self in case of panic.
// FIXME(eddyb) Use some sort of "free guard" that
Expand All @@ -78,15 +80,16 @@ impl<T: 'static> P<T> {
ptr::write(p, f(ptr::read(p)));

// Recreate self from the raw pointer.
P { ptr: Box::from_raw(p) }
P { ptr: Lrc::from_raw(p) }
}
}

/// Optionally produce a new `P<T>` from `self` without reallocating.
pub fn filter_map<F>(mut self, f: F) -> Option<P<T>> where
F: FnOnce(T) -> Option<T>,
{
let p: *mut T = &mut *self.ptr;
// FIXME(eddyb) How can we reuse the original if unchanged?
let p: *mut T = Lrc::make_mut(&mut self.ptr);

// Leak self in case of panic.
// FIXME(eddyb) Use some sort of "free guard" that
Expand All @@ -99,9 +102,9 @@ impl<T: 'static> P<T> {
ptr::write(p, v);

// Recreate self from the raw pointer.
Some(P { ptr: Box::from_raw(p) })
Some(P { ptr: Lrc::from_raw(p) })
} else {
drop(Box::from_raw(p));
drop(Lrc::from_raw(p));
None
}
}
Expand All @@ -116,15 +119,17 @@ impl<T: ?Sized> Deref for P<T> {
}
}

impl<T: ?Sized> DerefMut for P<T> {
impl<T: Clone> DerefMut for P<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.ptr
Lrc::make_mut(&mut self.ptr)
}
}

impl<T: 'static + Clone> Clone for P<T> {
impl<T: ?Sized> Clone for P<T> {
fn clone(&self) -> P<T> {
P((**self).clone())
P {
ptr: self.ptr.clone(),
}
}
}

Expand Down Expand Up @@ -160,17 +165,21 @@ impl<T: Encodable> Encodable for P<T> {

impl<T> P<[T]> {
pub fn new() -> P<[T]> {
P { ptr: Default::default() }
P { ptr: Lrc::new([]) }
}

// FIXME(eddyb) this is inefficient because it needs to
// move all the elements to accomodate `Lrc`'s allocation.
#[inline(never)]
pub fn from_vec(v: Vec<T>) -> P<[T]> {
P { ptr: v.into_boxed_slice() }
P { ptr: v.into() }
}

// FIXME(eddyb) this is inefficient because it needs to
// clone all the elements out of the `Lrc<[T]>`.
#[inline(never)]
pub fn into_vec(self) -> Vec<T> {
self.ptr.into_vec()
pub fn into_vec(self) -> Vec<T> where T: Clone {
self.ptr.to_vec()
}
}

Expand All @@ -181,31 +190,41 @@ impl<T> Default for P<[T]> {
}
}

impl<T: Clone> Clone for P<[T]> {
fn clone(&self) -> P<[T]> {
P::from_vec(self.to_vec())
}
}

impl<T> From<Vec<T>> for P<[T]> {
fn from(v: Vec<T>) -> Self {
P::from_vec(v)
}
}

impl<T> Into<Vec<T>> for P<[T]> {
// FIXME(eddyb) this is inefficient because it needs to
// clone all the elements out of the `Lrc<[T]>`.
impl<T: Clone> Into<Vec<T>> for P<[T]> {
fn into(self) -> Vec<T> {
self.into_vec()
}
}

// FIXME(eddyb) this is inefficient because it needs to
// clone all the elements out of the `Lrc<[T]>`.
impl<T: Clone> DerefMut for P<[T]> {
fn deref_mut(&mut self) -> &mut [T] {
// HACK(eddyb) this emulates `make_mut` for `Lrc<[T]>`.
if Lrc::get_mut(&mut self.ptr).is_none() {
self.ptr = self.ptr.to_vec().into();
}
Lrc::get_mut(&mut self.ptr).unwrap()
}
}

impl<T> FromIterator<T> for P<[T]> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> {
P::from_vec(iter.into_iter().collect())
}
}

impl<T> IntoIterator for P<[T]> {
// FIXME(eddyb) this is inefficient because it needs to
// clone all the elements out of the `Lrc<[T]>`.
impl<T: Clone> IntoIterator for P<[T]> {
type Item = T;
type IntoIter = vec::IntoIter<T>;

Expand Down