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

Persistent datatypes #7212

Closed
Aluminium95 opened this issue Jun 18, 2013 · 1 comment
Closed

Persistent datatypes #7212

Aluminium95 opened this issue Jun 18, 2013 · 1 comment

Comments

@Aluminium95
Copy link

It would be great to be able to get some « persistent datatypes » the way clojure has because it allow the programmer to get some purely immutable variables without performances issues as wikipedia says.

Because Rust is able to handle unique boxes, and has a garbage collector, it will be « easy » to implement some of those persistent structures.

let myvec = ~[1, 8, 9];

let second = myvec.append (8); // creates a new vec (does not modify « myvec »)
let third = myvec.insert (8,1) // idem 

This would also allow to pass some immutable values between tasks, with an O(1) complexity and allow the task to « modify » the value with a near O(1) complexity ... This would be very great !

I found some articles about the implementation of such types in clojure :

Then if this is possible, it would also be great to be able to specify that a variable is mutable only in a small block (like a function) in order to produce by mutation, an immutable value. For exemple to fill a vector with a function application and return a new immutable vector (sorry, I don't realy know the syntax of Rust ...)

fn new_vector_fill (func : &(fn(int) -> int), size : int) -> ~[int] {
    let mut vec = vector::new (size); // creates a new vector 
    // iterates over the vector and fill with « vec[i] = f(i) »
    return ~vec; // convert into a immutable value ...
}

Is it possible ? Would it be usefull ?

@thestinger
Copy link
Contributor

Unique boxes are equivalent to a paired malloc/free in C, they don't use garbage collection and would have to copy.

A persistent data structure has to be implemented with @ (garbage collection) or Rc (reference counting). There's no global garbage collector so they won't be sendable between tasks, and Rc isn't sendable because it doesn't use atomic reference counting.

I have #4987 open about reimplementing the persistent tree to make it actually useful, and new bugs can be opened for other specific data structures. I don't think it really makes sense to have a metabug for this.

Realistically, concurrent access to persistent data structures would require implementing a global garbage collector for immutable data. There's extra::arc for sendable atomic reference counted types, but reference counting is only acceptable for persistent data structures with a low fanout, like 1 for a list, or 2 for a binary tree. For acceptable performance, most persistent data structures would require implementing a global garbage collector for immutable data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants