Checking if a vector of structs is contiguous in memory gives some tips for how to check, but would like to know the general pattern.
My specific use case is a sort of nested dictionary. I have an iterator of key-value pairs, and I’d like to keep track of the (ordered) list of values associated with every key. I expect that most of the time most keys will only have a single value, but they could have an unbounded number (up to the length of the iterator).
I’d like to use a Dict{K, Tuple{V, Vector{V}}}
, but d.vals
uses indirection, so each initial assignment results in an allocation. For a 64 bit V, I’d like d.vals
to be an array of 128 bit structs, for the first occurrence of a key I’d like to set the first half to the value and the second to a null pointer, for the second occurrence, I’d like to allocate a vector with an element, each subsequent occurrence push!
into the vector, but I don’t know how to achieve this.
(I have benchmarked & profiled and this is the bottleneck)