DEV Community

Discussion on: No More Tears, No More Knots: Arena-Allocated Trees in Rust

Collapse
 
frolovdev profile image
Andrey Frolov

Hi there, thanks for the post. What about if I want to have a different trees/subtrees builded from the common "pool" of nodes? Like a two separate heads of the list pointed to the same node.

Does this data structure fits well for such case? (of course if we wrap all nodes in Rc)

Collapse
 
deciduously profile image
Ben Lovy

Hi - because all nodes only refer to each other via a Copy index, instead of directly pointing to each other, you should be able to set up such structures without even needing to wrap in Rc. The nodes are only accessed via the arena as needed, the only reference to them is just a number that can be passed around without issue.

Collapse
 
frolovdev profile image
Andrey Frolov

Thank you for the clarification

Thread Thread
 
deciduously profile image
Ben Lovy

The caveat would be simultaneous access - you would need to employ a lock of some sort if you expect multiple threads or tasks to mutably borrow the data in a given node.