Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/compile: optimize CAS loops #29716
Note: I'm not 100% sure the transformation I'm proposing is correct, or profitable. If it's not feel free to close.
Consider the typical Go CAS loop:
On x64, this gets compiled to: (https://godbolt.org/z/7905Kp)
There are 3 things that jump out:
Rewritten, the loop could become:
This would be nice to fix. There's even a TODO in the code:
The restriction mentioned may still be a problem. Memory-using ops can now generate flags, but I think it would still be a problem for memory-generating ops.
I'm not particularly worried about either of these. The loop should only execute once in typical situations (and if it doesn't, you have bigger problems).