Freelists and clear/emplace

Timon Gehr timon.gehr at gmx.ch
Sat Jun 16 12:45:40 PDT 2012


On 06/16/2012 09:34 PM, F i L wrote:
> On Saturday, 16 June 2012 at 01:14:06 UTC, d coder wrote:
>> Greetings
>>
>> Is emplace/clear  mechanism mature enough to be used to create freelists?
>>
>> I looked but found very scanty documentation on emplace/clear on
>> dlang.org.
>>
>> Regards
>> - Puneet
>
> I did performance tests awhile ago and emplace() was virtually identical
> to raw assignment. I'm not sure how familiar you are with D, but
> personally I prefer D's mixin templates for freelists:
>
>    mixin template Pool(T) { // FreeList
>      static T poolHead;
>      public T poolNext;
>
>      static auto create() { ... }
>      static void delete() { ... }
>    }
>
>
>    class MyClass {
>      mixin Pool!MyClass;
>    }
>
>    struct MyStruct {
>      mixin Pool!MyStruct;
>    }
>
>
>    void main() {
>      auto mc = MyClass.create();
>      auto ms = MyStruct.create();
>
>      ...
>
>      mc.delete();
>      ms.delete();
>    }

'typeof(this)' can be used to avoid stuttering the type at the mixin 
location.

mixin template Pool() { mixin Pool!(typeof(this)); }

class MyOtherClass {
     mixin Pool;
}


More information about the Digitalmars-d mailing list