View mode: basic / threaded / horizontal-split · Log in · Help
June 16, 2012
Freelists and clear/emplace
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
June 16, 2012
Re: Freelists and clear/emplace
On 06/16/2012 03:07 AM, 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 <http://dlang.org>.
>
> Regards
> - Puneet
>

I create most of my objects with emplace and have not experienced any 
trouble.

Make sure to keep your allocations aligned, though (16 bytes are sufficient)

http://d.puremagic.com/issues/show_bug.cgi?id=6635
June 16, 2012
Re: Freelists and clear/emplace
On 16.06.2012 15:51, Timon Gehr wrote:
> On 06/16/2012 03:07 AM, 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 <http://dlang.org>.
>>
>> Regards
>> - Puneet
>>
>

> I create most of my objects with emplace and have not experienced any
> trouble.

+1. And I used freelists with raw cast T* and had no trouble either.
(these Ts  were PODs, of course)

> Make sure to keep your allocations aligned, though (16 bytes are
> sufficient)
>
> http://d.puremagic.com/issues/show_bug.cgi?id=6635


-- 
Dmitry Olshansky
June 16, 2012
Re: Freelists and clear/emplace
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();
  }
June 16, 2012
Re: Freelists and clear/emplace
^^ Typo, should be:

  struct MyStruct {
    mixin Pool!MyStruct*;
  }
June 16, 2012
Re: Freelists and clear/emplace
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;
}
June 16, 2012
Re: Freelists and clear/emplace
Timon Gehr wrote:
> '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;
> }

For some reason I was under the impression that typeof(this) 
couldn't be resolved in this situation (no 'this' value), but I'm 
sure you're correct here. Which of course means you'd need to 
"static if(isValueType!typeof(this)) { ... } else { ... }" to 
make it work with structs.
June 16, 2012
Re: Freelists and clear/emplace
On 06/16/2012 09:55 PM, F i L wrote:
> Timon Gehr wrote:
>> '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;
>> }
>
> For some reason I was under the impression that typeof(this) couldn't be
> resolved in this situation (no 'this' value),

It is indeed special cased.

class S{
    pragma(msg, typeof(this)); // ok
    pragma(msg, this.stringof);
 // Error: 'this' is only defined in non-static member functions, not S
}

> but I'm sure you're
> correct here. Which of course means you'd need to "static
> if(isValueType!typeof(this)) { ... } else { ... }" to make it work with
> structs.
>
>
June 16, 2012
Re: Freelists and clear/emplace
On 06/16/2012 10:02 PM, Timon Gehr wrote:
> On 06/16/2012 09:55 PM, F i L wrote:
>> Timon Gehr wrote:
>>> '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;
>>> }
>>
>> For some reason I was under the impression that typeof(this) couldn't be
>> resolved in this situation (no 'this' value),
>
> It is indeed special cased.
>
> class S{
>      pragma(msg, typeof(this)); // ok
>      pragma(msg, this.stringof);
>   // Error: 'this' is only defined in non-static member functions, not S
> }
>

That example actually compiles, but I'd consider that a bug. Removing 
".stringof" makes it fail as expected.
Top | Discussion index | About this forum | D home