On Thursday, 15 March 2018 at 19:36:10 UTC, jmh530 wrote:
I recall some talk Andrei did where he said it was a bad idea to make the allocator part of the type. However, the container library in dlang-community(says it is backed with std.experimental.allocator) contains allocator as part of the type. Automem does too. Honestly, I would think you would really be hobbled if you didn't. For instance, if you want to expand a DynamicArray using the built-in ~= operator, then you really need to know what the allocator is. Without ~= you'd have to rely on functions (maybe member functions, maybe not).

So I suppose I'm wondering why is it a bad thing to include the allocator as part of the type and why is it that it seems like in practice that's how it is done anyway.

I think it is done in D, because it was always done in C++ this way, so it is known to be a working solution.

I see two reasons to make the allocators part of the type
1. Virtual function calls are slow, so let us do all allocators to structs and then we can pass them as a part of the type without using stuff like IAllocator, allocatorObject etc. 2. Used allocator is actually known at compile-time, so it can be decided at compile-time what allocator to use.

Now there are Bloomberg Labs allocators for C++ [1] (or BDE allocators), that are polymorphic allocators, so the allocators just implement an interface. The main reasoning behind it was as far as I remember to reduce compile time (and Bloomberg has tons of C++ code), so they developed these allocators and containers that accept the allocator as constructor argument and save it as a container member.

The main problem with allocators as part of the type isn't that you can't compare or assign types with different allocators - with a bit metaprogramming it is easy. The problem for me is that all you code should be templated then. You can't have a function

"void myFunc(Array!int)"

because it would work only with one allocator if the allocator is part of the type.

So I'm using polymorphic allocators for a long time [2].

I'm surprised to see that std.experimental.allocator documentation includes now an example how to save "the IAllocator Reference For Later Use".

[1] https://github.com/bloomberg/bde/wiki/BDE-Allocator-model
[2] https://github.com/caraus-ecms/tanya/blob/80a177179d271b6d023f51aa3abb69376415b36e/source/tanya/container/array.d#L205

Reply via email to