digitalmars.D - Freelists and clear/emplace
- d coder (5/5) Jun 15 2012 Greetings
- Timon Gehr (5/11) Jun 16 2012 I create most of my objects with emplace and have not experienced any
- Dmitry Olshansky (5/21) Jun 16 2012 +1. And I used freelists with raw cast T* and had no trouble either.
- F i L (23/30) Jun 16 2012 I did performance tests awhile ago and emplace() was virtually
- F i L (4/4) Jun 16 2012 ^^ Typo, should be:
- Timon Gehr (7/39) Jun 16 2012 'typeof(this)' can be used to avoid stuttering the type at the mixin
- F i L (6/12) Jun 16 2012 For some reason I was under the impression that typeof(this)
- Timon Gehr (7/22) Jun 16 2012 It is indeed special cased.
- Timon Gehr (3/22) Jun 16 2012 That example actually compiles, but I'd consider that a bug. Removing
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
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 - PuneetI 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
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 - PuneetI 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
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 - PuneetI 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
^^ Typo, should be: struct MyStruct { mixin Pool!MyStruct*; }
Jun 16 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:'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; }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 - PuneetI 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
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
On 06/16/2012 09:55 PM, F i L wrote:Timon Gehr wrote: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 }'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
On 06/16/2012 10:02 PM, Timon Gehr wrote:On 06/16/2012 09:55 PM, F i L wrote:That example actually compiles, but I'd consider that a bug. Removing ".stringof" makes it fail as expected.Timon Gehr wrote: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 }'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),
Jun 16 2012