auto arr = [1, 2, 3] should be a static array, not a GC allocated array

Don Allen donaldcallen at gmail.com
Sat Jul 23 14:56:48 UTC 2022


Personally, I hope Walter ignores this conversation. I think the 
current syntactic distinction (presence or absence of a constant 
length) between static and dynamic arrays is fine and whether a 
dynamic array is gc-allocated or stack-allocated seems to me to 
be a compiler optimization issue. I would think that if the 
compiler can prove that the array doesn't need to be re-allocated 
due to expansion and if allocation on the stack provides a 
sufficient lifetime, then it can stack allocate. And we should 
only care whether dmd does this optimization if it becomes clear 
that it is important in a lot of use cases, which I doubt.

And regarding that optimization, as hardware has gotten faster 
and faster (remember the Cray 1 "supercomputer"? It had an 80 mhz 
clock frequency), we have become more and more guilty of 
premature optimization. The flip side of that is that is the 
amount of software we all use daily that is written in languages 
like Python, Javascript or PHP that are perhaps 2 orders of 
magnitude slower than D and still provide adequate performance 
even on our phones.

My opinion.

/Don



More information about the Digitalmars-d mailing list