blog post about how you can make your gc code faster

Adam D Ruppe destructionator at gmail.com
Wed Nov 9 12:29:51 UTC 2022


On Wednesday, 9 November 2022 at 11:55:28 UTC, Guillaume Piolat 
wrote:
> I've been avoiding void[] for this reason (I mean, void[] 
> _could_ contain pointers), but I think I'm cargo-culting this?

Yeah, it actually doesn't really matter. It is the allocation 
type that sets the flag. So

> If I do:
>     ubyte[] arr = new ubyte[100_000_000];

Since you `new ubyte`, it set NO_SCAN at the allocation. That's 
attached to the memory block now.

>     void[] arr2 = cast(void[]) arr; // will this still avoid 
> scanning?

Meaning this will not scan the block.


Where you get in trouble with void is if it was allocated that 
way to begin with. Then it will be scanned... but I'm pretty sure 
`new void[]` doesn't work anyway.

Other potential trouble with this would be if you allocate as 
ubyte, then cast to a pointer type and store something in there. 
That might be freed prematurely since the block flags is set at 
allocation time, and not changed when you cast the slice.

Even the precise collector doesn't really care since the type in 
a struct being ubyte[] or void[] are both themselves a pointer - 
the pointer to the array - so it'd count. But the array itself is 
stored elsewhere so the GC will look that up in its separate 
metadata.


So you are OK using void[] here to hold things wrt gc scans. Just 
make sure it is allocated as a concrete type.


More information about the Digitalmars-d-announce mailing list