Questions about GC

H. S. Teoh hsteoh at qfbox.info
Tue Jun 23 01:32:02 UTC 2026


On Tue, Jun 23, 2026 at 01:03:37AM +0000, Thomas via Digitalmars-d-learn wrote:
> thank you, do you know how to reuse an array? it seems to reset the capacity
> when you set the length to 0, so it reallocates when you append to it
[...]

You can do it two ways:

1) Either manually, by setting .length to the desired capacity, and
using a separate variable to keep track of its "actual" length; or

2) Use .assumeSafeAppend to tell the runtime to overwrite elements when
you append to it, if the allocated block still has capacity.


(2) is easier to write, but less efficient as it needs to invoke a
runtime function each time, whereas (1) is more efficient, but a bit
more fiddly for your code to handle.  I've used both approaches before
in my code -- it's one of the more common optimizations I use when
profiling reveals that the program is spending a lot of time inside the
GC.


T

-- 
I'm still trying to find a pun for "punishment"...


More information about the Digitalmars-d-learn mailing list