-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: avoid copying if copied value isn't modified #19817
Comments
Issues similar to #18625 could be resolved with this optimization |
A copy is required for the
We want this code to always return 0, even in the presence of data races. The internal assignments, like |
@valyala, you can optimize it man 8000 ually:
or, even simpler
|
@go101 , programmers frequently forget about implicit copying and stack usage, which results to issues like #18625 . See also this tweet. So it would be great if compiler could assist the programmer in such cases. @randall77 , probably the optimization could be applied only to arrays and structs? |
That would help the mutex case. But I still don't think it is advisable.
It would be very surprising if it printed different values, even in the presence of data races. |
@bradfitz , could you add "performance" label to this issue? |
Go 1.8 emits unnesessary value copyings in the following code (didn't test go tip yet, but it is likely it has the same behaviour):
All these copyings could be substituted by pointers, which would result in direct memory access pointed by
p
. This looks like safe optimization, since botha
andb
values are read-only in this code.This optimization also could save stack space used for storing
a
andb
values.Someone could argue this optimization can't be applied since the memory pointed by
p
could be modified by concurrently running goroutines, which will result in data races. But this code is already vulnerable to the same data races when compiled without the optimization, since value copying isn't atomic.The text was updated successfully, but these errors were encountered: