automate tuple creation
H. S. Teoh
hsteoh at quickfur.ath.cx
Wed Jan 19 23:21:58 UTC 2022
On Wed, Jan 19, 2022 at 02:33:02PM -0800, Ali Çehreli via Digitalmars-d-learn wrote:
[...]
> // Returning a dynamically allocated array looks expensive
> // here. Why not use a struct or std.typecons.Tuple instead?
Premature optimization. ;-) There's nothing wrong with allocating an
array.
If you're worried about memory efficiency, you could allocate the entire
matrix in a single block and just assemble slices of it in the outer
block, like this:
uint[][] createBoolMatrix(size_t count) {
auto buffer = new uint[count*count];
return iota(count).map!(i => buffer[count*i .. count*(i+1)])
.array;
}
This lets you do only 2 GC allocations instead of (1+count) GC
allocations. May help with memory fragmentation if `count` is large and
you create a lot of these things. But I honestly wouldn't bother with
this unless your memory profiler is reporting a problem in this aspect
of your program. It just adds complexity to your code (== poorer
long-term maintainability) for meager benefits.
T
--
What do you call optometrist jokes? Vitreous humor.
More information about the Digitalmars-d-learn
mailing list