On Sunday, 4 March 2018 at 15:23:41 UTC, Nordlöw wrote:
When zeroing a slice of memory (either stack or heap) such as
enum n = 100;
ubyte[n] chunk;
should I use `memset` such as
memset(chunk.ptr, 0, n/2); // zero first half
or an array assignment such as
chunk[0 .. n/2] = 0; // zero first half
or are they equivalent in release mode?
Further, does it depend on whether the slice length is known at
compile-time or not?
I'd recommend not concerning yourself with such low-level
optimizations and let the compiler do that for you, and only jump
in yourself if profiling/benchmarking shows that there's a
bottleneck, and prefer nice readable code otherwise.
E.g., LDC will lower `chunk[0 .. n/2] = 0` to a memset() call if
the length is unknown at compile-time, and otherwise replace it
with good-looking assembly code (xor vector register and store it
consecutively to memory): https://run.dlang.io/is/I6Fq9G (click
on ASM and check the assembly for the two functions).