std.experimental.allocator

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sun Aug 13 16:49:41 UTC 2023


On 14/08/2023 4:10 AM, ryuukk_ wrote:
> Also if you want people to use D for games, you want an allocator API 
> that doesn't use RAII, same for exceptions btw

After thinking about it a bit, this would suggest to me that you are 
trying to solve a problem that I would outright recommend against using 
an abstraction to solve.

Memory allocators that only deal with fixed sized memory blocks, that 
are specific to a thread are going to have the best performance. If this 
is the case you don't need an abstraction at all.

A rough pseudo code where no RCAllocator would be used:

```d
module particles;

private __gshared FreeList!(AllocatorList!(Region!(SystemMapper, 
__traits(classInstanceSize, Particle)))) allocator;

final class Particle {
	int lifeLeft;

	void free() {
		allocator.dispose(this);
	}

	static Particle create() {
		return allocator.make!Particle();
	}
}
```

Of course you'd only call free/create as part of a particle manager, but 
this should still demonstrate that an abstraction isn't required if you 
understand your data and memory lifetimes well enough to see any 
performance improvement by using a memory allocator library.


More information about the Digitalmars-d-learn mailing list