www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Freelists and clear/emplace

reply d coder <dlang.coder gmail.com> writes:
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
Jun 15 2012
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
Jun 16 2012
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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
Jun 16 2012
prev sibling parent reply "F i L" <witte2008 gmail.com> writes:
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(); }
Jun 16 2012
next sibling parent "F i L" <witte2008 gmail.com> writes:
^^ Typo, should be:

   struct MyStruct {
     mixin Pool!MyStruct*;
   }
Jun 16 2012
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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; }
Jun 16 2012
parent reply "F i L" <witte2008 gmail.com> writes:
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.
Jun 16 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Jun 16 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Jun 16 2012