News for hackers. Stuff that matters.
Re:Speedup from switch to += by thomasahle on Monday September 12, 2022 @12:11AM (#32806004)
In PyTorch `x = y + x` is actually semantically different from `x += y`, so you can't easily make the switch with a compiler.

The difference is that `x += y` modifies `x` inplace, where `x = x + y` creates a new object. In other words, if anybody had a reference to `x` before the update, the "optimized" code would break things.

Re:Speedup from switch to += by WirelessGigabit on Monday September 12, 2022 @12:15AM (#32806024)
Compiler could use a pointer to pointer.

I guess this is the kind of this stuff that drew me to Rust. This kind of behavior gives me the creeps. Just like Ruby’s conventions.

Re:Speedup from switch to += by fragmede on Monday September 12, 2022 @01:48AM (#32806523)
Re:Speedup from switch to += by codeflo on Monday September 12, 2022 @02:27AM (#32806717)
Not to turn this into an evangelism subthread, but I think GP is right. The performance optimization could apply, but the bug as described should be prevented by the borrow checker. In fact you can do neither x = … nor x += … when anything else still references x, because assignment requires an exclusive reference (&mut), which is statistically checked.
Re:Speedup from switch to += by _flux on Monday September 12, 2022 @02:39AM (#32806789)
I'm not sure what you mean by "same behavior". I don't think you can externally distinguish between "x = x + x" and "x += x" in Rust, because while in the first case a new temporary value will be created, it will overwrite the left side value: x will be the "same object" as it's in the same address in both cases. In Python objects are stored indirectly, so after an assignment x will then point to a new object instead of the old one.

But yes, not making copies is more efficient so the same optimization applies.

Nevertheless, in Rust (or C++) the parameter x would not have been passed as a mutable reference into the function, so the value provided as input would not have been mutated.