digitalmars.D.learn - delegates & GC allocations
- Etienne (15/15) Aug 20 2014 I've been hearing that delegates get a context pointer which will be
- ketmar via Digitalmars-d-learn (4/5) Aug 20 2014 On Wed, 20 Aug 2014 10:44:38 -0400
- ketmar via Digitalmars-d-learn (4/4) Aug 20 2014 On Wed, 20 Aug 2014 10:44:38 -0400
- Dicebot (7/12) Aug 20 2014 non-static nested functions are effectively delegates as it needs
- Ola Fosheim Gr (1/3) Aug 20 2014 Only if it is recursive.
- Chris Nicholson-Sauls (3/6) Aug 20 2014 Or if it refers to any state of the parent function.
- Ola Fosheim Gr (5/12) Aug 20 2014 As long as the compiler knows where it will be called from it
- Ola Fosheim Gr (5/15) Aug 20 2014 Well, I guess simple recursion could be solved easily too by
- Etienne (2/5) Aug 20 2014 So, my question inspired a new optimization? :-p
- Ola Fosheim Gr (4/5) Aug 20 2014 A decent optimizing compiler would detect that the function is
- hane (19/35) Aug 20 2014 int getInt1() @nogc
I've been hearing that delegates get a context pointer which will be allocated on the GC. Is this also true for delegates which stay in scope? e.g. void addThree() { int val; void addOne() { val++; } addOne(); addOne(); addOne(); return val; } Will the above function allocate on the GC?
Aug 20 2014
On Wed, 20 Aug 2014 10:44:38 -0400 Etienne via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Will the above function allocate on the GC?no.
Aug 20 2014
On Wed, 20 Aug 2014 10:44:38 -0400 Etienne via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: and this is not delegate, this is just nested function.
Aug 20 2014
On Wednesday, 20 August 2014 at 14:54:31 UTC, ketmar via Digitalmars-d-learn wrote:On Wed, 20 Aug 2014 10:44:38 -0400 Etienne via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: and this is not delegate, this is just nested function.non-static nested functions are effectively delegates as it needs a context pointer to parent stack frame. However, not every delegate is a closure - heap allocation of the context happens only if delegate leaves the scope it refers to (via return value or by being passed as an argument to external function)
Aug 20 2014
non-static nested functions are effectively delegates as it needs a context pointer to parent stack frame.Only if it is recursive.
Aug 20 2014
On Wednesday, 20 August 2014 at 15:17:52 UTC, Ola Fosheim Gr wrote:Or if it refers to any state of the parent function.non-static nested functions are effectively delegates as it needs a context pointer to parent stack frame.Only if it is recursive.
Aug 20 2014
On Wednesday, 20 August 2014 at 20:48:38 UTC, Chris Nicholson-Sauls wrote:On Wednesday, 20 August 2014 at 15:17:52 UTC, Ola Fosheim Gr wrote:As long as the compiler knows where it will be called from it should be able use a stack pointer offset (unless alloca gets in the way) without the frame pointer of the parent.Or if it refers to any state of the parent function.non-static nested functions are effectively delegates as it needs a context pointer to parent stack frame.Only if it is recursive.
Aug 20 2014
On Wednesday, 20 August 2014 at 21:19:18 UTC, Ola Fosheim Gr wrote:On Wednesday, 20 August 2014 at 20:48:38 UTC, Chris Nicholson-Sauls wrote:Well, I guess simple recursion could be solved easily too by having a wrapper function that puts the frame pointer in a free callee save register...On Wednesday, 20 August 2014 at 15:17:52 UTC, Ola Fosheim Gr wrote:As long as the compiler knows where it will be called from it should be able use a stack pointer offset (unless alloca gets in the way) without the frame pointer of the parent.Only if it is recursive.Or if it refers to any state of the parent function.
Aug 20 2014
On 2014-08-20 5:25 PM, Ola Fosheim Gr wrote:Well, I guess simple recursion could be solved easily too by having a wrapper function that puts the frame pointer in a free callee save register...So, my question inspired a new optimization? :-p
Aug 20 2014
On Wednesday, 20 August 2014 at 21:30:41 UTC, Etienne wrote:So, my question inspired a new optimization? :-pA decent optimizing compiler would detect that the function is calling itself and save stack space by using register where possible...
Aug 20 2014
On Wednesday, 20 August 2014 at 14:44:39 UTC, Etienne wrote:I've been hearing that delegates get a context pointer which will be allocated on the GC. Is this also true for delegates which stay in scope? e.g. void addThree() { int val; void addOne() { val++; } addOne(); addOne(); addOne(); return val; } Will the above function allocate on the GC?int getInt1() nogc { int val; int func() nogc { return val; } return func(); } int delegate() getInt2() // nogc - thie one allocates! { int val; int func() nogc { return val; } return &func; } int delegate() getInt3() nogc { int val; int func() nogc { return 0; } return &func; }
Aug 20 2014