Delegate Memory Usage & Optimization
Walter Bright
newshound2 at digitalmars.com
Wed Dec 11 19:30:24 PST 2013
On 12/11/2013 6:27 PM, Maxime Chevalier-Boisvert wrote:
> I have a use case where I will need to generate millions (possibly hundreds of
> millions) of delegates with the same type signature. I'd like to know more about
> the memory usage characteristics. I have two questions:
>
> 1. Delegates have environment/context pointer. If the delegate does not
> capture/access any variables from the englobing function (if it behaves like a
> static nested function), will a context object be allocated anyways,
No.
> or will this pointer be null?
No. It'll be a pointer to the enclosing stack frame, even if it is never used.
> Does DMD optimize this case?
Yes. It only allocates a closure if (1) it thinks the delegate may escape the
scope and (2) uplevel references are used in the delegate.
> 2. Delegates are a function pointer and an environment pointer. Are they
> passed/stored by value, as a struct would be,
Yes.
> or heap allocated and passed by reference?
No.
I strongly suggest trying out a couple examples, and disassembling the result to
confirm.
More information about the Digitalmars-d
mailing list