Hacker News new | ask | show | jobs
by WalterBright 4069 days ago
> I've literally seen 20x or more speedups in multithreaded cases just by making sure I reuse every buffer rather than create new ones.

Reuse rather than free and reallocate is a core practice whenever you feel the need for speed, regardless of the memory allocate strategy used.

For some very fast D code:

https://github.com/facebook/warp

Minimizing the amount of heap memory allocated is a core strategy.

2 comments

Thanks for responding. I've seen this code used an an example before actually.

Not that buffer reuse/avoiding heap allocations was unknown to me, it was just surprising to see this in an application that spent most of it's time waiting on the network.

I will say as a positive point that the equivalent D code to a C++ implementation was much cleaner due to build in array slicing among other things.

There are other areas where heap allocations are not so avoidable however (some hashmaps, some std's). My main point is that like at all languages, heap allocations are slow, but here they bear an unnecessarily large contention factor.

I really like D so I hope this is helpful feedback.

"Reuse rather than free and reallocate is a core practice whenever you feel the need for speed"

The problem with this conventional wisdom is that it defeats other optimisations. If you free, use, and deallocate a block of memory within a compilation unit, a compiler can transparently allocate it on the stack, or even keep it in registers

If you use a free-list the objects are always escaped, and the same optimisations can't be applied to them.

This is particularly true for small temporary objects, like an intermediate vector (as in coordinates) object.