GC allocation
Alex via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Apr 21 15:39:03 PDT 2016
On Thursday, 21 April 2016 at 19:37:59 UTC, QAston wrote:
> On Thursday, 21 April 2016 at 17:27:09 UTC, Alex wrote:
>> Ok. So, does this mean, that they just allocate on
>> creation/binding them? If so, there is no problem and there
>> are no questions any more.
>
> Just like classes - when closure expression is executed.
>
>> I have an unusual caption... On creation I capture an
>> immutable id for my delegates, which are stored in an array.
>> Indeed, the id of the delegate is just the position of it in
>> its array.
>> Then, I call my delegate as a function with a parameter,
>> depending on it, the call is delegated to another objects
>> (with the stored id, of course :) )
>> Another possibility, which I could imagine is: not to store
>> the id and let the delegate calculate it by some pointer
>> arithmetic from the array wherein it is stored. Then, no
>> independent data would be stored at all. This would assume
>> however, that different function pointers won't be merged,
>> although the one and only distinction between them would be
>> the fact of storing them at different array indices.
>
> Instead of using a delegate you can use a Struct with opCall.
> Pass what you want to copy to the struct by using a constructor
> and put your function code in opCall method.
>
> An example here:
>
> https://github.com/QAston/transducers-dlang/blob/master/source/transduced/transducers.d#L797
>
> you can see there various variants of doing the same operation,
> using closure, function and struct.
Yes, I can. I even used a struct before. But then, all properties
of it were moved to other classes and I decided to not have a
struct just for storing an id. Well, storing an id alone would
maybe be ok for me, but I also have the knowledge, that the id
has to be an ordinal number. And that is why I thought a struct
would be not worth it.
But the solution with the opCall is cool. Thanks for the hint.
So my question was, if I messed up the speed of my code, by
removing the struct. And if the allocation is done like classes -
then it is no problem. I allocate them all once: at the beginning
of my prog, then only use them as functions, so the allocating is
just an initialization effort and nothing bad is happening.
More information about the Digitalmars-d-learn
mailing list