Returning variable-sized stack data
Quirin Schroll
qs.il.paperinik at gmail.com
Tue Aug 13 09:55:28 UTC 2024
On Monday, 12 August 2024 at 17:42:11 UTC, ryuukk_ wrote:
> On Monday, 12 August 2024 at 10:37:21 UTC, Quirin Schroll wrote:
>> On Wednesday, 17 July 2024 at 08:23:45 UTC, IchorDev wrote:
>>> On Monday, 15 July 2024 at 17:20:57 UTC, monkyyy wrote:
>>>> I think you should also preemptively handle runtime stack
>>>> allocated arrays inside the function
>>>>
>>>> ```d
>>>> int[$] foo(size_t i){
>>>> int[i] output;
>>>> foreach(...){
>>>> ...
>>>> }
>>>> return output;
>>>> }
>>>> ```
>>>
>>> Oh yes, that would be fantastic!
>>> It'd be lowered to something like this, right?
>>> ```d
>>> int[] output = (cast(int*)alloca(i * int.sizeof))[0..i];
>>> ```
>>
>> Why not this:
>> ```d
>> scope xs = new int[](n);
>> ```
>
>
> `new` = GC heap allocation
>
> So it doesn't make sense to read that line
Nonsense. Try this (use `-dip1000`):
```d
void f(scope Object) @safe @nogc { }
void main() @nogc @safe
{
scope o = new Object;
f(o);
}
```
Oddly, `new` rather means: Creates a reference type. It does not
mean: Allocates on the GC.
> A value is a value, no `new` keyword, please, besides, in my
> custom runtime, there is no `class`, no `new`, no `GC` and more
> importantly, no `TypeInfo`
What you’re basically saying is: You don’t use D; you use a
personal language based on D.
>> With the same guarantees of allocation elision as `scope xs =
>> [ 1, 2, 3 ];`?
>
>
> D's biggest mistake was to make this GC allocated without
> requiring `new`
>
> I hope this gets fixed with an Edition, because it is cringe
Funnily enough, C#12 added [collection
expressions](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#collection-expressions) which are basically what D had all along. In particular, no use of `new`.
Some people overly care about GC and allocations. D is a GC
language. It has facilities to statically ensure some parts of a
program are free of GC allocations, probably for the purpose of
optimizing hot spots. Most of Phobos is not written with `@nogc`
in mind – or `nothrow` for that matter. For example, I recently
fixed
[`lockstep`](https://dlang.org/library-prerelease/std/range/lockstep.html) so that it is `@safe` (and `pure`), but not `@nogc` or `nothrow`. That is not due to the task it accomplishes, but because it uses `enforce`.
More information about the dip.ideas
mailing list