Is there any way to get pointers to the actual value in slice? I have a tree of offers in the maps:
map[uint64]map[uint16][]offer
For fast access by id (which from 1 to n) I need a slice of pointers:
[]*offer
Actual offers lies in the first tree, in the slice should be pointers to the that actual.
I can't traverse tree and collect pointers of actual values because with range
of this slice gives me a copies, but I have more than billions of this structs in total and copies will lead to waste of my memory: 40 bytes for one struct lead to the 100+ gb and much more in the future. I also need to store slice of values in the tree to contiguous in order to fact lookup of offers.
May be there are some way to use unsafe
or reflect
package to get these pointers?
Index of pointers build only one time and both structs are immutable and only used to lookup offers.
Update: I was totally wrong. There is no problem to get pointer to element in the slice. My code was:
var offers []offer // actually there was about 50gb of offers
for i := range offers {
currentOffer := offers[i]
s.Relations[currentOffer.Id] = ¤tOffer
}
After this snippet overall amount of ram consumed by the app became 100+gb. I immediately thought that take element from slice of values give me a copy of that value, but it is wrong. I just copied original value to the currentOffer
variable by myself. Little mistake took 50gb of ram away.
Actually this snippet is work fine as expected:
for i := range offers {
s.Relations[offers[i].Id] = &offers[i]
}