Re:Copying data is wasteful, mutating data is dangerous
by unlinked_dll on Thursday January 09, 2020 @01:17PM
(#22003815)
>results of previous caches persisting
You clear the buffer or capture it inside an optional type. When you're done with the buffer you can reset it to the default state to be used again.
Most of the issues are logical bugs though, not flaws with the pattern. You just need to reason about the computation that you're doing to preallocate exactly how much data you need outside your hot loops.
You can read some audio source code if you want, this is an extremely common pattern. Preallocate for your computation up front by exactly how much you need (and often with cache-friendly data structures) and don't get crafty reusing buffers where you don't need to.
You clear the buffer or capture it inside an optional type. When you're done with the buffer you can reset it to the default state to be used again.
Most of the issues are logical bugs though, not flaws with the pattern. You just need to reason about the computation that you're doing to preallocate exactly how much data you need outside your hot loops.
You can read some audio source code if you want, this is an extremely common pattern. Preallocate for your computation up front by exactly how much you need (and often with cache-friendly data structures) and don't get crafty reusing buffers where you don't need to.