8 ms·
EDIT: See kevincox's reply. Rust will bitwise copy the containing type, which is typically very cheap. For example, it you move a String, it will copy the Strin
by justinpombrio 6y ago
EDIT: See kevincox's reply. Rust will bitwise copy the containing type, which is typically very cheap. For example, it you move a String, it will copy the String struct, which contains a couple pointers and a length (or something along those lines). Importantly, it will not copy the underlying char array.
I was thinking of the following code, where I believe the assignment to y is actually free. Though apparently this isn't called a "move".
let x = <<large owned type like [char; 1000]>>;
let y = x;
More info: https://doc.rust-lang.org/rust-by-example/scope/move.html https://doc.rust-lang.org/rust-by-example/scope/move.html
- kevincox 6y agoSemantically it is a bitwise copy. In the example from the article it is probably actually a copy because the value was originally on the parent thread's stack, which will be reused after the function returns, so the value will need to be copied to the new thread's stack. However it is important to not that it isn't a deep/recursive bitwise copy. It just needs to copy the HashMap itself (which is probably a handful of words). So yes, it is doing a bitwise copy, but this is also very cheap. It will be much, much cheaper than spawning the thread.
- ahupp 6y agoThat's not exactly true. In C++ terms, the example code is moving a value: std::map<...> foo; someFunction(std::move(foo)); And not moving a pointer like: std::unique_ptr<std::map<...>> foo = ...; someFunction(std::move(foo)); So it copies sizeof(std::map<...>), not a pointer.
- pjmlp 6y agoNot necessary, std::map::map(&&) is free to move just the internal pointers and bookkeeping data, only dumb implementations would move the memory blocks where the map data is actually stored, as per ISO C++ requirements.
- ahupp 6y agoThe comment originally said it was the cost of a pointer copy, and I was pointing out it's not a pointer. eg., sizeof(std::map<std::string,std::string>) is 48, which is the "pointers and bookeeping" you mention.
- pjmlp 6y agoAh sorry for the misinterpretation. Should have payed more attention.