3 ms·
No, really, using arrays for internal storage of trees, graphs etc is much better from performance point of view. Among other things, allocations and finalizat
by fsloth 3y ago
No, really, using arrays for internal storage of trees, graphs etc is much better from performance point of view.
Among other things, allocations and finalizations are considerably faster.
When a non-trivial payload is construed mostly of large linked structures where each element is malloced, using platform memory management may results in performance profile where over quarter of the run time is spent on malloc and free. Am not kidding.
Of course you don't malloc anything. You do have "newNode" method or such that increments size. When size reaches capacity, you allocate a new larger array and copy data to it. When nodes "finalize", you store the index, and gc at the resize time (if you want to - there are lots of algorithms where you actually always only want to add data to the datastructure).
Deep copy everything, you say? Hell yes, it's way way faster to have few large memory allocate/copy operations than a datastructures worth of node pointers.
And if you don't care about performance, you probably shouldn't be using a systems programming language in the first place :D
The above of course only makes sense if you have really large business critical datastructures - it's not an endorsement to rewrite every dictionary, set or a linked list. But the message to which I replied to was about "when writing your own datastructure".