May 04, 2020
On Monday, 4 May 2020 at 09:20:06 UTC, Ali Çehreli wrote:
> On 4/30/20 10:04 AM, Ben Jones wrote:> On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote:
>
> > I think you want to use scope rather than auto which will put
> the class
> > on the stack and call its destructor:
> > https://dlang.org/spec/attribute.html#scope
>
> That is correct about calling the destructor but the object would still be allocated with 'new', hence be on the heap. There is also library feature 'scoped', which places the object on the stack:
>
>   https://dlang.org/phobos/std_typecons.html#scoped
>
> Ali

https://godbolt.org/z/SEVsp5

My ASM skills are pretty limited, but it seems to me that the call to _d_allocclass is omitted when using scope. At least with LDC and GDC.

Am I missing something ?
May 04, 2020
On 5/4/20 5:47 AM, Olivier Pisano wrote:
> On Monday, 4 May 2020 at 09:20:06 UTC, Ali Çehreli wrote:
>> On 4/30/20 10:04 AM, Ben Jones wrote:> On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote:
>>
>> > I think you want to use scope rather than auto which will put
>> the class
>> > on the stack and call its destructor:
>> > https://dlang.org/spec/attribute.html#scope
>>
>> That is correct about calling the destructor but the object would still be allocated with 'new', hence be on the heap. There is also library feature 'scoped', which places the object on the stack:
>>
>>   https://dlang.org/phobos/std_typecons.html#scoped
>>
> 
> https://godbolt.org/z/SEVsp5
> 
> My ASM skills are pretty limited, but it seems to me that the call to _d_allocclass is omitted when using scope. At least with LDC and GDC.
> 
> Am I missing something ?

I'm not sure if Ali is referring to this, but the usage of scope to allocate on the stack was at one time disfavored by the maintainers. This is why std.typecons.scoped was added (to hopefully remove that feature).

Though, if dip1000 ever becomes the default, allocating on the stack could be a valid optimization.

-Steve
May 04, 2020
On 5/4/20 2:47 AM, Olivier Pisano wrote:
> On Monday, 4 May 2020 at 09:20:06 UTC, Ali Çehreli wrote:
>> On 4/30/20 10:04 AM, Ben Jones wrote:> On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote:
>>
>> > I think you want to use scope rather than auto which will put
>> the class
>> > on the stack and call its destructor:
>> > https://dlang.org/spec/attribute.html#scope
>>
>> That is correct about calling the destructor but the object would still be allocated with 'new', hence be on the heap. There is also library feature 'scoped', which places the object on the stack:
>>
>>   https://dlang.org/phobos/std_typecons.html#scoped
>>
>> Ali
> 
> https://godbolt.org/z/SEVsp5
> 
> My ASM skills are pretty limited, but it seems to me that the call to _d_allocclass is omitted when using scope. At least with LDC and GDC.
> 
> Am I missing something ?

I stand corrected.

'scope's running the destructor was news to me, so I tested with a writeln example where the messages changed order depending on whether 'scope' was replaced with 'auto' or not:

import std.stdio;

class C {
  ~this() {
    writeln("~this");
  }
}

void foo() {
  scope c = new C();  // <-- here
}

void main() {
  foo();
  writeln("foo returned");
}

Now it's news to me that 'new' does not allocate on the heap when 'scope' is used. I'm not sure I'm comfortable with it but that's true. Is this unique? Otherwise, 'new' always allocates on the heap, no?

I added the following three lines to the end of foo() too see (without needing to look at assembly) that the 'scope' and 'auto' class objects are allocated on different memory regions (by taking object addresses as proof :) ):

  writeln(cast(void*)c);
  auto c2 = new C();
  writeln(cast(void*)c2);

Ali

May 04, 2020
On 5/4/20 12:33 PM, Ali Çehreli wrote:
> Now it's news to me that 'new' does not allocate on the heap when 'scope' is used. I'm not sure I'm comfortable with it but that's true. Is this unique? Otherwise, 'new' always allocates on the heap, no?

This feature was in D1 AFAIK.

So it's really old news ;)

-Steve
May 04, 2020
On Mon, May 04, 2020 at 09:33:27AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: [...]
> Now it's news to me that 'new' does not allocate on the heap when 'scope' is used. I'm not sure I'm comfortable with it but that's true. Is this unique?  Otherwise, 'new' always allocates on the heap, no?

IIRC this is by design.  The idea is that if an object is 'scope', then it will not escape the current scope and therefore it's safe to allocate it on the stack instead of the heap.  IIRC historically this was a semi-hack to allow people to specify that the object should be allocated on the stack instead of the heap, back when the semantics of 'scope' wasn't clearly defined yet.


T

-- 
Knowledge is that area of ignorance that we arrange and classify. -- Ambrose Bierce
May 04, 2020
On Monday, 4 May 2020 at 11:50:49 UTC, Steven Schveighoffer wrote:
> I'm not sure if Ali is referring to this, but the usage of scope to allocate on the stack was at one time disfavored by the maintainers. This is why std.typecons.scoped was added (to hopefully remove that feature).
>
> Though, if dip1000 ever becomes the default, allocating on the stack could be a valid optimization.

It's not an optimization, it's the status quo for years, although apparently not properly spec'd. And unlike the `scoped` library solution, `scope` is lightweight and works with -betterC too.
1 2
Next ›   Last »