-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: elide write barriers for pointer swaps #16765
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
Comments
This is definitely unsafe if there's the possibility of preemption in the swap sequence. It might currently be safe if the sequence is unpreemptible (which is the case here), including no possibility of panics or synchronous signals. I'm not sure this is ever safe with ROC. /cc @RLH |
Another idea, if pointer swaps are common enough (and I suspect anecdotally they are, but have no evidence): Add a writebarrierswap runtime call that swaps two pointers. It'd eliminate one runtime call, at least. Presumably also a typedmemswap. (cc @bradfitz) |
Yet another idea: we could make the write barrier itself unpreemptible, which would let the compiler check runtime.writeBarrier just once per atomic sequence and coalesce some of the control flow. Would that help? We haven't looked too closely at this because, as far as we can tell, applications spend basically no time in the write barrier. But it's hard to quantify the secondary effects. |
Yes, I think it would. And there's a TODO in the compiler for it already. I think the secondary effects (code size, eliminated jumps, resulting new SSA backend optimization opportunities, etc.) are non-trivial. One recent data point is from CL 26666. A minor compiler change removed a single write barrier from the maps implementation and got a 5-8% perf boost on a few benchmarks that hit that code path hard. |
Does
need a write barrier? What's special about swapping pointers? Or is the observation related to s[i] and s[j] being slots in the same heap object? |
The special thing about swapping pointers is that it doesn't change the reachable set. |
Actually, it's not safe even if the sequence is unpreemptible. GC may be in the process of scanning this slice, and if the scan is between indexes i and j and there's no write barrier, you may fail to mark whatever is now at min(i, j). |
Ah. Oh well. Thanks, Austin. |
This might be hard to accomplish, but consider:
This generates two
writebarrierptr
calls. But since no actual pointers were harmed in the making of this swap, it seems to me that ought to be doable with no write barriers at all.That would transform this:
into this:
cc @mdempsky @randall77 @aclements @dr2chase
The text was updated successfully, but these errors were encountered: