Re: Why are immutable array literals heap allocated?

2019-07-07 Thread Patrick Schluter via Digitalmars-d-learn
On Saturday, 6 July 2019 at 09:56:57 UTC, ag0aep6g wrote: On 06.07.19 01:12, Patrick Schluter wrote: On Friday, 5 July 2019 at 23:08:04 UTC, Patrick Schluter wrote: On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc {     return [1,2]; } [...] and it

Re: Why are immutable array literals heap allocated?

2019-07-06 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 5 July 2019 at 23:05:32 UTC, Jonathan M Davis wrote: Yes, I was wondering why the compiler doesn't statically allocate it automatically as an optimization. It would have to be set up to store the literal somewhere else. I was thinking the read-only data segment. Certainly, it

Re: Why are immutable array literals heap allocated?

2019-07-06 Thread ag0aep6g via Digitalmars-d-learn
On 06.07.19 01:12, Patrick Schluter wrote: On Friday, 5 July 2019 at 23:08:04 UTC, Patrick Schluter wrote: On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc {     return [1,2]; } [...] and it cannot optimize it away because it doesn't know what the

Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Era Scarecrow via Digitalmars-d-learn
On Friday, 5 July 2019 at 16:25:10 UTC, Nick Treleaven wrote: Yes, I was wondering why the compiler doesn't statically allocate it automatically as an optimization. Which i would think it could, but silently adds .dup to the end as it points to a unnamed memory block of N size. Or if it's

Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Patrick Schluter via Digitalmars-d-learn
On Friday, 5 July 2019 at 23:08:04 UTC, Patrick Schluter wrote: On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation This makes

Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Patrick Schluter via Digitalmars-d-learn
On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation This makes dynamic array literals unusable with @nogc, and adds to GC pressure

Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, July 5, 2019 10:25:10 AM MDT Nick Treleaven via Digitalmars-d- learn wrote: > On Thursday, 4 July 2019 at 11:06:36 UTC, Eugene Wissner wrote: > > static immutable arr = [1, 2]; > > > > You have to spell it out that the data is static. > > Yes, I was wondering why the compiler

Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Max Haughton via Digitalmars-d-learn
On Friday, 5 July 2019 at 16:25:10 UTC, Nick Treleaven wrote: On Thursday, 4 July 2019 at 11:06:36 UTC, Eugene Wissner wrote: static immutable arr = [1, 2]; You have to spell it out that the data is static. Yes, I was wondering why the compiler doesn't statically allocate it

Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Nick Treleaven via Digitalmars-d-learn
On Thursday, 4 July 2019 at 11:06:36 UTC, Eugene Wissner wrote: static immutable arr = [1, 2]; You have to spell it out that the data is static. Yes, I was wondering why the compiler doesn't statically allocate it automatically as an optimization.

Re: Why are immutable array literals heap allocated?

2019-07-04 Thread a11e99z via Digitalmars-d-learn
On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation specify the size of the static array: immutable(int[ 2 /*HERE*/ ]) f() @nogc {

Re: Why are immutable array literals heap allocated?

2019-07-04 Thread Eugene Wissner via Digitalmars-d-learn
On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote: immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation This makes dynamic array literals unusable with @nogc, and adds to GC pressure

Why are immutable array literals heap allocated?

2019-07-04 Thread Nick Treleaven via Digitalmars-d-learn
immutable(int[]) f() @nogc { return [1,2]; } onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation This makes dynamic array literals unusable with @nogc, and adds to GC pressure for no reason. What code would break if dmd used only static data