Jump to page: 1 2 3
Thread overview
Create class on stack
Aug 05, 2017
Johnson Jones
Aug 05, 2017
angel
Aug 05, 2017
Moritz Maxeiner
Aug 06, 2017
Johnson Jones
Aug 06, 2017
Moritz Maxeiner
Aug 06, 2017
FoxyBrown
Aug 06, 2017
Adam D. Ruppe
Aug 06, 2017
FoxyBrown
Aug 06, 2017
Moritz Maxeiner
Aug 06, 2017
Jacob Carlborg
Aug 06, 2017
Moritz Maxeiner
Aug 07, 2017
Jacob Carlborg
Aug 07, 2017
Moritz Maxeiner
Aug 07, 2017
Mike
Aug 07, 2017
Moritz Maxeiner
Aug 07, 2017
Moritz Maxeiner
Aug 07, 2017
Mike
Aug 07, 2017
Moritz Maxeiner
Aug 08, 2017
Johan Engelen
Aug 08, 2017
ANtlord
Aug 08, 2017
Moritz Maxeiner
August 05, 2017
using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it.

Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct?
August 05, 2017
On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote:
> using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it.
>
> Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct?

Emplace ?
https://dlang.org/phobos/std_conv.html#emplace

August 05, 2017
On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote:
> using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it.
>
> Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct?

The easy way is through std.typecons.scoped [1].
Here be dragons, though, because classes are reference types.

[1] https://dlang.org/phobos/std_typecons.html#.scoped
August 06, 2017
On Saturday, 5 August 2017 at 23:09:09 UTC, Moritz Maxeiner wrote:
> On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote:
>> using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it.
>>
>> Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct?
>
> The easy way is through std.typecons.scoped [1].
> Here be dragons, though, because classes are reference types.
>
> [1] https://dlang.org/phobos/std_typecons.html#.scoped

Thanks, I didn't think it created on the stack but it makes sense to do so. The only issue is that it escaping the reference?

August 06, 2017
On Sunday, 6 August 2017 at 01:18:50 UTC, Johnson Jones wrote:
> On Saturday, 5 August 2017 at 23:09:09 UTC, Moritz Maxeiner wrote:
>> On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote:
>>> using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it.
>>>
>>> Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct?
>>
>> The easy way is through std.typecons.scoped [1].
>> Here be dragons, though, because classes are reference types.
>>
>> [1] https://dlang.org/phobos/std_typecons.html#.scoped
>
> Thanks, I didn't think it created on the stack but it makes sense to do so.

See the source [1] as to why: typeof(scoped!T) is a (non-copyable) struct that holds the memory for the T object inside it.

> The only issue is that it escaping the reference?

Yes, don't escape references, that's the reason for my comment:
>> Here be dragons, though, because classes are reference types.

[1] https://github.com/dlang/phobos/blob/v2.075.0/std/typecons.d#L6613
August 06, 2017
On Sunday, 6 August 2017 at 02:10:31 UTC, Moritz Maxeiner wrote:
> On Sunday, 6 August 2017 at 01:18:50 UTC, Johnson Jones wrote:
>> On Saturday, 5 August 2017 at 23:09:09 UTC, Moritz Maxeiner wrote:
>>> On Saturday, 5 August 2017 at 17:08:32 UTC, Johnson Jones wrote:
>>>> using gtk, it has a type called value. One has to use it to get the value of stuff but it is a class. Once it is used, one doesn't need it.
>>>>
>>>> Ideally I'd like to treat it as a struct since I'm using it in a delegate I would like to minimize unnecessary allocations. Is there any way to get D to allocate a class on the stack like a local struct?
>>>
>>> The easy way is through std.typecons.scoped [1].
>>> Here be dragons, though, because classes are reference types.
>>>
>>> [1] https://dlang.org/phobos/std_typecons.html#.scoped
>>
>> Thanks, I didn't think it created on the stack but it makes sense to do so.
>
> See the source [1] as to why: typeof(scoped!T) is a (non-copyable) struct that holds the memory for the T object inside it.
>
>> The only issue is that it escaping the reference?
>
> Yes, don't escape references, that's the reason for my comment:
>>> Here be dragons, though, because classes are reference types.
>
> [1] https://github.com/dlang/phobos/blob/v2.075.0/std/typecons.d#L6613

I don't think you understand what I'm saying.

If I use this method to create a "reference" type on the stack rather than the heap, is the only issue worrying about not having that variable be used outside that scope(i.e., have it "escape")?

Obviously since it's on the stack it will be invalid after the function call, but I'm talking about other pitfalls. I don't see any but I want to be sure. Also, does it do the allocation at compile time(reserve space on the stack for the variable along with all the others or does it "allocate" space on the stack at runtime?... which is slightly slower).




August 06, 2017
On Sunday, 6 August 2017 at 02:19:19 UTC, FoxyBrown wrote:
> Also, does it do the allocation at compile time(reserve space on the stack for the variable along with all the others or does it "allocate" space on the stack at runtime?... which is slightly slower).

compile time. It works like a static array of the appropriate size.

though the cost if ti was at runtime is small regardless. I think it is just a register subtract.
August 06, 2017
On Sunday, 6 August 2017 at 02:32:05 UTC, Adam D. Ruppe wrote:
> On Sunday, 6 August 2017 at 02:19:19 UTC, FoxyBrown wrote:
>> Also, does it do the allocation at compile time(reserve space on the stack for the variable along with all the others or does it "allocate" space on the stack at runtime?... which is slightly slower).
>
> compile time. It works like a static array of the appropriate size.
>
> though the cost if ti was at runtime is small regardless. I think it is just a register subtract.

yeah, I know, but no need for it ;) Still better than the heap but was just curious ;) No need to waste cycles if it's not necessary.
August 06, 2017
On Sunday, 6 August 2017 at 02:19:19 UTC, FoxyBrown wrote:
> [...]
>
> I don't think you understand what I'm saying.
>
> If I use this method to create a "reference" type on the stack rather than the heap, is the only issue worrying about not having that variable be used outside that scope(i.e., have it "escape")?

It's the only one I'm aware of OTTOMH. If you encounter others, a bug report would be appreciated.
August 06, 2017
On 2017-08-05 19:08, Johnson Jones wrote:
> using gtk, it has a type called value. One has to use it to get the
> value of stuff but it is a class. Once it is used, one doesn't need it.
>
> Ideally I'd like to treat it as a struct since I'm using it in a
> delegate I would like to minimize unnecessary allocations. Is there any
> way to get D to allocate a class on the stack like a local struct?

Prefix the variable declaration with "scope":

scope foo = new Object;

-- 
/Jacob Carlborg
« First   ‹ Prev
1 2 3