News for hackers. Stuff that matters.
Re:More Rust compared to C++ by jarrettc on Monday April 06, 2015 @02:35PM (#9329845)
The author mentions that Rust's "Box type is an owning type" and compares Box to C++'s std::unique_ptr. Worth noting: Rust's most basic variable syntax provides unique_ptr-like behavior, so you usually don't need the Box type for this behavior. For example:

https://gist.github.com/jarrett/fe3f24c301a586efd7c1

Re:More Rust compared to C++ by cwzwarich on Monday April 06, 2015 @03:07PM (#9330036)
The default mode in Rust is value semantics, similar to C++'s default. Rust does have the advantage of move-by-default on uses and checking the validity of moves, but if you want unique_ptr behavior then you need Box. C++ has the advantage (if you would call it that) of allowing arbitrary code in move constructors.
Re:More Rust compared to C++ by phaylon on Monday April 06, 2015 @03:13PM (#9330079)
I think he was more talking about the unique ownership part, especially that values themselves follow the same rules.

Which I find fitting, since in Rust, the Box is (at least to me) more about indirection. The single-owner semantics just come in because Box follows the same rules as anything else, while for unique_ptr they are part of the pointer concept.

Re:More Rust compared to C++ by SamReidHughes on Monday April 06, 2015 @03:03PM (#9330013)
Are you sure your comments are right? It seems to me like the value would be memcpyed, when you pass by value. There is not one unit of memory that is "taken over," or if there was, then Rust would have a serious problem.
Re:More Rust compared to C++ by phaylon on Monday April 06, 2015 @03:23PM (#9330149)
It might be a terminology thing. Assuming MyStruct only contains values, it will fully live on my_func's stack and nothing needs deallocating on drop. When take_ownership is called a copy of it's value would be passed in, and the original location marked unusable (assuming MyStruct doesn't implement Copy). So yeah, there's isn't any specific memory location being taken over, since there's two locations involved.
Re:More Rust compared to C++ by jarrettc on Tuesday April 07, 2015 @10:24AM (#9334230)
> It seems to me like the value would be memcpyed, when you pass by value.

As far as I know, the compiler should not copy in that instance. Rather, it should move.

> There is not one unit of memory that is "taken over," or if there was, then Rust would have a serious problem.

Could you elaborate on that? Rust does have an ownership model, and ownership can be transferred as in the example. What sort of problems would you expect that to cause? If you're worried that it will invalidate existing pointers, the compiler checks that for you. Unless you deliberately circumvent the check, the compiler guarantees that your pointers are valid.

Re:More Rust compared to C++ by steveklabnik on Tuesday April 07, 2015 @10:48AM (#9334415)
Semantically speaking, the only difference between a move and a copy is that you're allowed to use a copy type afterwards, and you're not allowed to use a move type afterwards. It's still a memcpy. Of course, these may be elided by optimization passes.
Re:More Rust compared to C++ by jarrettc on Wednesday April 08, 2015 @10:43AM (#9341070)
> Semantically speaking, the only difference between a move and a copy is that you're allowed to use a copy type afterwards

Are you speaking about Rust specifically, or move in general? I had always understood that move was no more expensive than passing by reference. That is, I had thought the memory was on the heap and didn't need to be copied each time someone new took ownership of that heap space.

Re:More Rust compared to C++ by steveklabnik on Wednesday April 08, 2015 @11:39AM (#9341452)
I mean in Rust.

> That is, I had thought the memory was on the heap

An example:

    let x = Box::new(5);
    let y = x;
While the 5 is allocated on the heap, when we move x to y, _the pointer itself_ is memcpy'd. That's why Box<T> isn't Copy; as you say, a simple memcpy won't actually duplicate the structure. Make sense?

(and in this case, I'd assume llvm's optimizations would realize the copy is superflous and just elide it, but semantically, that's what's up)

Re:More Rust compared to C++ by jarrettc on Wednesday April 08, 2015 @01:22PM (#9342347)
Oh, agreed. But how about this:

    let x = BigExpensiveStruct::new();
    some_function(x);
That won't trigger a big, expensive memcpy of the BigExpensiveStruct, will it? I'd thought that its memory was on the heap.
Re:More Rust compared to C++ by steveklabnik on Wednesday April 08, 2015 @03:52PM (#9343428)
If that's just a struct, it's stack allocated. So it's not ok the heap in the first place. IIRC, LLVM may optimize passing it to the function by reference though.
Re:More Rust compared to C++ by jarrettc on Wednesday April 08, 2015 @05:08PM (#9344012)
Good to know. Thanks! So I guess the moral is, if you have a big, expensive struct, make sure the expensive part is in a sub-structure you know is heap allocated, such as a Vec. E.g.:

    struct Expensive {
        cheap_value: u32,
        expensive_value: Vec<u32>
    }
Does that seem like a good maxim?
Re:More Rust compared to C++ by SamReidHughes on Wednesday April 08, 2015 @06:42PM (#9344613)
That is not a big expensive struct (to memcpy). (edit: I'm just bickering about what to call things, what you say is right.)

Your intuition about the performance of this sort of thing might be served by reading about how calling conventions work. You might not need to copy the struct from a local variable to where it belongs on the stack or in registers when calling a function, either because the calling convention says you put a pointer to the struct somewhere (depending on its size) or because you're (you being a compiler) clever enough to put it in the place that'll end up in the argument list later. The callee, however, has less flexibility, and if it needs to pass the value somewhere else, it'll probably have to copy the data. This is way better than allocating it on the heap -- stuff on the stack is in the L1 cache, you compute it's location statically instead of having to traverse data structures, but yeah if you found yourself copying around a 1000-byte struct you might want to box it or pass it by reference. I only know about C and C++ calling conventions though, so don't infer from my comment that Rust isn't doing anything "special" for big non-copyable structs -- I wouldn't know.

Re:More Rust compared to C++ by Jweb_Guru on Thursday April 09, 2015 @03:43PM (#9350468)
Rust (well, LLVM really) will automatically pass any "large" struct by reference, which in practice is going to be anything more than a few words (I think in the current implementation, it might actually be two). Unless the function call is inlined, of course, in which case LLVM can do pretty much whatever it wants.
Re:More Rust compared to C++ by steveklabnik on Wednesday April 08, 2015 @06:27PM (#9344538)
Well, it depends. I mean, generally, you want to give flexibility to your users. Maybe I _do_ want to stack allocate an ExpensiveStruct for some reason. You can always Box the struct to have it on the heap.
Re:More Rust compared to C++ by SamReidHughes on Wednesday April 08, 2015 @04:05AM (#9339289)
> Could you elaborate on that?

If the value isn't passed by (perhaps elidably) copying, that would mean it's getting allocated on the heap and deallocated.

Re:More Rust compared to C++ by jarrettc on Wednesday April 08, 2015 @10:46AM (#9341093)
Yes, I had understood the struct's memory to be on the heap. My thinking was, keeping the memory on the heap allows for inexpensive moves. Whereas, the pointer to the heap space may indeed be on the stack, as far as I know.

But I could be mistaken. This is all based on hearsay--just stuff I've read about the Rust compiler. I don't actually work on the compiler myself.

Re:More Rust compared to C++ by masklinn on Monday April 06, 2015 @03:11PM (#9330062)
His comment is right in that all of Rust's values do have move semantics by default (checked by the compiler), that's not specific to Box<T>.
Re:More Rust compared to C++ by cpeterso on Monday April 06, 2015 @03:17PM (#9330112)
The Rust documentation on ownership shows examples of function with reference parameters and Box parameters, but nothing like the OP's `fn take_ownership(obj: MyStruct)` example. Is that an oversight in this page in Rust docs?

http://doc.rust-lang.org/nightly/book/ownership.html

Re:More Rust compared to C++ by dbaupp on Monday April 06, 2015 @06:17PM (#9331018)
I think it is an oversight: Box isn't particularly special with how it handles ownership, but people do get the wrong impression that Box is the way to have uniquely owned data, whereas it is actually the default. This happens less now that Box doesn't have special syntax, but I agree with you that we may be able to improve those docs in this respect.

Unfortunately, it can be hard to have an example that's simple enough to understand, while still emphasising the right concepts. Hopefully we can find something.

Re:More Rust compared to C++ by masklinn on Monday April 06, 2015 @03:26PM (#9330162)
> nothing like the OP's `fn take_ownership(obj: MyStruct)` example.

It uses Box to demonstrate ownership in the first section, but that works the same way with any Rust type unless said type was opted into copy semantics, there's nothing special or magical about Box (at least in that respect), it's just a wrapper for a raw pointer.

Re:More Rust compared to C++ by SamReidHughes on Monday April 06, 2015 @03:14PM (#9330092)
I said comments.