4 ms·
I looked at the Go implementation of this in "tagged pointers" [0] The amount of data that can be used for the tag is architecture-dependant, and the routine d
by marcus_holmes 3y ago
I looked at the Go implementation of this in "tagged pointers" [0]
The amount of data that can be used for the tag is architecture-dependant, and the routine discards any tag bits that don't fit into the tagged pointer without telling the caller.
To me, this seems ridiculous - why not just use a struct with a tag and a pointer, and not run the risk of your tag being destroyed without you knowing because the architecture can't fit that many bits?
But the Go folks are smart, and must be doing this for a reason. Can anyone explain the thinking here?
[0] https://github.com/golang/go/blob/master/src/runtime/tagptr_64bit.go https://github.com/golang/go/blob/master/src/runtime/tagptr_...
- yencabulator 3y agoIt's up to the low-level code to stay within taggedPointerBits, and the value is arch-dependent. For netpoll, it looks to be just a cyclic epoch counter, so the size doesn't really matter: https://github.com/golang/go/blob/4956c3437bd2f4448bcec51321f123d03731ddfc/src/runtime/netpoll.go#L299 https://github.com/golang/go/blob/4956c3437bd2f4448bcec51321... For use in malloc, it's assumed to be >=10 and verified at init: https://github.com/golang/go/blob/4956c3437bd2f4448bcec51321f123d03731ddfc/src/runtime/malloc.go#L449 https://github.com/golang/go/blob/4956c3437bd2f4448bcec51321...
- deleted 3y ago[deleted]
- Splizard 3y agoAlignment, if you add a uint8 tag for example, you'll end up effectively doubling your pointer size.
- saagarjha 3y agoAuxiliary data is not free; by stuffing your data into a pointer you don’t need to pay the cost of extra storage.
- none_to_remain 3y agoOne good use case is if you are doing atomic operations on pointers, you can atomically modify the tag as well.
- intelVISA 3y agoIt is not a Go thing, tagged pointers have been around forever!!
- marcus_holmes 3y agoI didn't say it was. I just followed one of the links in the article because I grok Go and it seemed counter-intuitive to how Go normally does things. But the other answers seem to make sense, so I guess this is a thing I accept now.
- wheels 3y agoThere are a lot of data structures where doubling the size of the struct would be a big deal. I've worked on big graphs, where I used similar tricks, because most of the storage of a graph is pointers, and limits of what can be quickly computed are bound by how much of the graph you can get in memory.