I think I have a use case which (for the foolish, but brave) could end
up in similar considerations about using unsafe and/or CGO.
I have an (almost) lock-free FIFO queue of many writers and one reader
in a hot path.
It's based on a datastructure like this:
---------------------------------------
type event struct {
seq uint64 // running sequence number of the value.
val int64
// val interface{}
}
type FifoQueue [queueSize]event
---------------------------------------
Simplified... the idea is that it's a ring-buffer and there's only used
locks when the writer catches up with the reader or the "seq" number is
about to wrap.
Anyway... The "val" field is supposed to be any *numerical* type. But if
I use an interface{}, I run into escape-analysis causing an extra
unnecessary allocation for boxing the numerical in an interface (and
some additional runtime cost).
The allocation unnecessary because I really don't need the full
interface{} generality. I only need numerical types. Something like:
type Value union {
int64
uint64
float64
}
type event struct {
seq uint64 // running sequence number of the value.
val Value
}
Now ... I could of course just duplicate all the code for the lock-free
FIFO queue in a copy for each of the needed types. :(
But not doing that would AFAICS involve fiddling with "unsafe".
/Peter
PS: I thing this discussion is related too:
http://webmail.dev411.com/p/gg/golang-nuts/156dvh40hs/go-nuts-re-reconsidering-union-types