8

Take a look at the following simple example:

use std::rc::Rc;

struct MyStruct {
    a: i8,
}

fn main() {
    let mut my_struct = MyStruct { a: 0 };
    my_struct.a = 5;
    let my_struct_rc = Rc::new(my_struct);

    println!("my_struct_rc.a = {}", my_struct_rc.a);
}

The official documentation of Rc says:

The type Rc<T> provides shared ownership of a value of type T, allocated in the heap.

Theoretically it is clear. But, firstly my_struct is not immediately wrapped into Rc, and secondly MyStruct is a very simple type. I can see 2 scenarios here.

  1. When my_struct is moved into the Rc the memory content is literally copied from the stack to the heap.
  2. The compiler is able to resolve that my_struct will be moved into the Rc, so it puts it on the heap from the beginning.

If number 1 is true, then there might be a hidden performance bottleneck as when reading the code one does not explicitly see memory being copied (I am assuming MyStruct being much more complex).

If number 2 is true, I wonder whether the compiler is always able to resolve such things. The provided example is very simple, but I can imagine that my_struct is much more complex and is mutated several times by different functions before being moved to the Rc.

2
  • This may help reddit.com/r/rust/comments/hemjx0/… and also search for placement new in rust. Commented Aug 19, 2021 at 7:16
  • 2
    The function call to Rc::new() moves the value into the function, and whenever you do this, the value might actually be copied – there's nothing hidden here. The conpiler also might be able to optimise this, but you'll never get a guarantee it will. I recommend not to worry. Copying a value is cheap even for more complex values, and in almost all cases the cost for the allocation will dwarf the cost of the move. There may be some extreme cases where it matters, but they should be exceedingly rare. Commented Aug 19, 2021 at 9:00

2 Answers 2

7

Tl;dr It could be either scenario, but for the most part, you should just write code in the most obvious way and let the compiler worry about it.

According to the semantics of the abstract machine, that is, the theoretical model of computation that defines Rust's behavior, there is always a copy. In fact, there are at least two: my_struct is first created in the stack frame of main, but then has to be moved into the stack frame of Rc::new. Then Rc::new has to create an allocation and move my_struct a second time, from its own stack frame into the newly allocated memory*. Each of these moves is conceptually a copy.

However, this analysis isn't particularly useful for predicting the performance of code in practice, for three reasons:

  1. Copies are actually pretty darn cheap. Moving my_struct from one place to another may actually be much cheaper, in the long run, than referencing it with a pointer. Copying a chunk of bytes is easy to optimize on modern processors; following a pointer to some arbitrary location is not. (Bear in mind also that the complexity of the structure is irrelevant because all moves are bytewise copies; for instance, moving any Vec is just copying three usizes regardless of the contents.)

    If you haven't measured the performance and shown that excessive copying is a problem, you must not assume that it is without evidence: you may accidentally pessimize instead of optimizing your code. Measure first.

  2. The semantics of the abstract machine is not the semantics of your real machine. The whole point of an optimizing compiler is to figure out the best way to transform one to the other. Under reasonable assumptions, it's very unlikely that the code here would result in 2 copies with optimizations turned on. But how the compiler eliminates one or both copies may be dependent on the rest of the code: not just on the snippet that contains them but on how the data is initialized and so forth. Real machine performance is complicated and generally requires analysis of more than just a few lines at a time. Again, this is the whole point of an optimizing compiler: it can do a much more comprehensive analysis, much faster than you or I can.

    Even if the compiler leaves a copy "on the table", you shouldn't assume without evidence that removing the copy would make things better simply because it is a copy. Measure first.

  3. It probably doesn't matter anyway, in this case. Requesting a new allocation from the heap is likely† more expensive than copying a bunch of bytes from one place to another, so fiddling around with 1 fast copy vs. no copies while ignoring a (plausible) big bottleneck is probably a waste of time. Don't try to optimize things before you've profiled your application or library to see where the most performance is being lost. Measure first.

See also

Questions about overflowing the stack by accidentally putting large data on it (to which the solution is usually to use Vec instead of an array):


* Rc, although part of the standard library, is written in plain Rust code, which is how I analyze it here. Rc could theoretically be subject to guaranteed optimizations that aren't available to ordinary code, but that doesn't happen to be relevant to this case.

† Depending at least on the allocator and on whether new memory must be acquired from the OS or if a recently freed allocation can be re-used.

5
  • Is this Rust abstract machine defined anywhere? Can I read about its architecture? Do you know?
    – Al Bundy
    Commented Aug 28, 2021 at 11:07
  • Regarding point number 3. This is why I have actually asked this question. In C, the allocation is always explicit, in Rust it might be implicit. I do not understand why Rc needs data to be placed in the heap.
    – Al Bundy
    Commented Aug 28, 2021 at 11:32
  • There are no implicit allocations in Rust either so I don't know what you mean by that. Rc puts data in the heap because that's what Rc is: a heap allocated shared pointer. If you don't want allocation, you don't want Rc. Simple as that.
    – trent
    Commented Aug 28, 2021 at 14:41
  • As for the exact semantics, they're a work in progress, and not formalized, so I unfortunately can't really source my first claim. There are papers you can read about defining a formal semantics for Rust but I don't know where would be a good place to start. I would stress that formal semantics are only an abstraction, and do not correspond to real world ABIs or performance characteristics. Abstract machines don't exist; they don't have performance. So if you're concerned about performance, that's probably not the right tree to be barking up.
    – trent
    Commented Aug 28, 2021 at 14:53
  • 1
    Ok, I guess I have been missing one point. Having value in stack and in Rc simply does not make sense, because when it is in stack you (or compiler) simply know the scope and lifetime.
    – Al Bundy
    Commented Aug 28, 2021 at 15:36
0

You can just test what happens:

Try to use my_struct after creating an Rc out of it. The value has been moved, so you can't use it.

use std::rc::Rc;

struct MyStruct {
    a: i8,
}

fn main() {
    let mut my_struct = MyStruct { a: 0 };
    my_struct.a = 5;
    let my_struct_rc = Rc::new(my_struct);

    println!("my_struct_rc.a = {}", my_struct_rc.a);
    // Add this line. Compilation error "borrow of moved value"
    println!("my_struct.a = {}", my_struct.a);
}

Make your struct implement the Copy trait, and it will be automatically copied into the Rc::new function. Now the code above works, because the my_struct variable is not moved anywhere, just copied.

#[derive(Clone, Copy)]
struct MyStruct {
    a: i8,
}

The compiler is able to resolve that my_struct will be moved into the Rc, so it puts it on the heap from the beginning.

Take a look at Rc::new source code (removed the comment which is irrelevant).

struct RcBox<T: ?Sized> {
    strong: Cell<usize>,
    weak: Cell<usize>,
    value: T,
}

// ...

    pub fn new(value: T) -> Rc<T> {
        Self::from_inner(Box::into_raw_non_null(box RcBox {
            strong: Cell::new(1),
            weak: Cell::new(1),
            value,
        }))
    }

It takes the value you pass to it, and creates a Box, so it's always put on the heap. This is plain Rust and I don't think it performs too many sophisticated optimizations, but that may change.

Note that "move" in Rust may also copy data implicitly, and this may depend on the current compiler's behavior. In that case, if you are concerned about performance you can try to make the struct as small as possible, and store some information on the heap. For example when a Vec<T> is moved, as far as I know it only copies the capacity, length and pointer to the heap, but the actual array which is on the heap is not copied element by element, so only a few bytes are copied when moving a vector (assuming the data is copied, because that's also subject to compiler optimizations in case copying is not actually needed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.