auto arr = [1, 2, 3] should be a static array, not a GC allocated array
Don Allen
donaldcallen at gmail.com
Sat Jul 23 17:37:04 UTC 2022
On Saturday, 23 July 2022 at 15:09:11 UTC, ryuukk_ wrote:
> On Saturday, 23 July 2022 at 14:56:48 UTC, Don Allen wrote:
>> 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.
>
> I would be fine if the distinction was made by the compiler,
> but in critical performance scenarios, i don't want to gamble
> wether the compiler will optimize something or not
>
> I want to make sure that my intent is perfectly understood by
> the compiler
>
> I don't want any ambiguity, when i write the code, and more
> importantly when i read it
>
> "Oh it is `auto arr = [1, 2, 3]` will it be stack allocated?
> hmm idk, let me decypher this whole algorithm again`"
If you don't want any ambiguity and want to be sure your array is
stack-allocated, then
say so explicitly
````
int arr[3]={1,2,3}
````
rather than using auto.
>
>> 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
>
> Not everyone develop for High End devices
>
> Some people are fine paying more $$$ for higher Cloud instance
> tier, some are not
>
> I personally stick to the cheapest to host my stuff, and my
> care made me save lot of money, and my programs are all fast,
> no compromise, and the language shouldn't cater to the lower
> common denominator, the developer should make that choice
And the developer *can* make that choice as the language stands
today.
As for the gc-allocated vs static optimization, that only applies
to dynamic arrays and, as I said, it is far from clear how
important that is in the grand scheme of things, even on cheap
hardware. But if it's important to you that your arrays be
stack-allocated, use static arrays, specified explicitly (not by
using 'auto). And if passing by value is a problem for you, pass
pointers. What you want is in the language now.
More information about the Digitalmars-d
mailing list