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
sync: save 4 bytes memory allocation in WaitGroup struct #19149
Comments
/cc @dvyukov |
See #19057, which would make this idea (which is a good one) unnecessary. |
It won't save any memory because we don't have a 12-byte size class.
Allocating a 12-byte object will fall into the 16-byte size class.
|
@minux, what if the sync.WaitGroup were embedded in a larger struct? |
btw, the trick used in WaitGroup here does indicate that compilers should guarantee the alignment of 64-bit words must be 4N. But I can't find the guarantee in Go specification. Other compilers may not enforce the 4N alignment guarantee. So this trick may make Go programs not portable. |
The trick here is not valid. Something has to make the waitgroup 4-byte aligned, and that something is the uint32 field. |
Actually, it would be fine (and clearer) to change the [12]byte into [3]uint32. Then the finding of the sema field doesn't even need to use unsafe (except to determine the alignment). |
Change https://golang.org/cl/100515 mentions this issue: |
The WaitGroup struct uses a field, state1, a byte array which length is 12 to assure WaitGroup.state() returns a 64-bit aligned pointer. There are 4 bytes wasted by using this trick.
It may be a good idea to use the 4 wasted bytes for the sema field.
Now:
the saving memory version:
Maybe, merging the two functions as one two-returns function is better.
The text was updated successfully, but these errors were encountered: