Lifecycle doc clarification
I was reading https://docs.modular.com/mojo/manual/lifecycle/life#move-constructor, the following struc is given as an example:
what does
Then the lifetime of "existing" ends here, but Mojo does NOT call its destructor
mean? I understand that existing.data must not be free but what about existing.size? is it freed even though the destructor is not called? in theory a copy was made by doing self.size = existing.size
Life of a value | Modular Docs
An explanation of when and how Mojo creates values.
4 Replies
This means that the exisiting
HeapArray
can no longer be used when the move operator is used: ^
, but it also shouldn't free the data because now self
(the object it's being moved to) owns the data. Now the last use of self
will free the dataBut lines self.size = existing.size and self.data = existing.data are creating copies, are they not ? Shouldn't the move operator also be used in those lines?
Int
and Pointer
are register passable and "trivial". So they can only be copied. For simple things in register copying is more efficient than moving.Yeah exactly what @Michael K wrote, the expensive thing to copy is what's allocated by
self.data
, and that's not being copied just the Pointer to it. Passing 128bit of data through registers is trivial.