by ironhaven · 4y ago · 0 comments
z = y + x
x = z
Basically, creating an object `z` just to throw it away.`x += y` just adds y to x directly without any intermediary.
You could write this in any language pretty easily. For example, in Rust:
let x = "abc".to_string();
let y = "123".to_string();
let x = x + &y;
as opposed to the more efficient: let mut x = "abc".to_string();
let y = "123".to_string();
x.push_str(&y);
It's just using an operation to mutate in place vs an immutable operation.It’s the confusion / idea that this is trivial change which is the overload thing.
The problem is thinking `+' and `+=' are the same, they are not and `+' should not be used when `+=' can be used.
Btw there's ongoing work to automatically optimize expressions like this. See the XLA compiler for example. Right now deep learning has a ton of seemingly obvious compute/memory optimisations that are not done automatically.