Re:Taming Go’s memory usage, or how we avoided rewriting our...
by slaymaker1907 on Tuesday September 21, 2021 @03:40PM
(#28608989)
I never thought about using a Vec for these, but that is a great idea for keeping the memory management sane for tree/linked lists.
One thing I would add that you need to be wary of destructors with large pointer data structures in Rust since it can easily stack overflow. When using Option<Box<T>> you need to be careful to call Option::take on the pointers in a loop to avoid stack overflow.
In general, if it's a datastructure where you have to use pointers, you'll have them Box'ed, but you would try to avoid that if you can. In your example of a heap, you'd want to use an array-based implementation, probably backed by a growable Vec, and use indexes internally. A peek function would still return a normal Rust reference to the data, and the borrow checker would make sure that you don't mutate the heap's backing array while that reference was still in use, etc.