4 ms·
"(Zig's) collections are not parametrized by an allocator, like in C++ or (future) Rust. Rather, an allocator is passed in explicitly to every method which actu
by Animats 4y ago
"(Zig's) collections are not parametrized by an allocator, like in C++ or (future) Rust. Rather, an allocator is passed in explicitly to every method which actually needs to allocate. This is Call Site Dependency Injection, and it is more flexible.'
I'd once considered the idea of having "space" as a type. Space is an array of bytes, and it cannot be read or written. Constructors take in "space" and turn it into an object. Constructors have the privilege of casting "space" to something else.
Destructors take an object and turn it back into "space". Destructors can cast their object to "space", and this destroys the data.
This separates construction, which is a typed thing, from allocation.
It can be used with malloc/free type allocation, garbage collection, or fixed allocation. You'd probably encapsulate this with a generic depending on what type of allocation you are using.
C++ probably would have been cleaner if it took that route, but C++ added both generics and allocators as afterthoughts.
- rrishi 4y agoSuper interesting idea.
- crop_rotation 4y agoI actually did find the allocation control very interesting and useful. It seemed like something that can be so useful in the context of request/response servers. Use an allocator per request and reset it at the end. However, it alone does not compensate for all the other missing niceties. e.g. Just dealing with strings is not straightforward compared to Rust/C++.
- ok_dad 4y agoGoddamn, this is a great way to consider this problem! I dabble in a lot of languages, Zig included, and I never had a good way to consider the allocation model that fit in my head in a reasonable way. See, I like to think of software as a transformation process that happens to physical objects that I actually visualize, and so thinking of an allocator as a "space" that can be transformed is super useful to me. I also love very-strictly-typed programming. I enjoy making everything a type, so thinking about a "space" type is something I can use in the future.
- spacechild1 4y ago> This separates construction, which is a typed thing, from allocation. But this is what C++ actually does! C++ ctors run after memory has been allocated (on the stack, on the heap, from a pool, etc.) Or maybe you mean something different?
- Animats 4y agoThe lifetime of the space can be different, but must enclose, the objects created in that space. C++ doesn't track lifetimes. Such a separation is only useful in specific situations, such as the one the original article mentions. It could be useful in avionics or industrial control, where you often avoid memory pools and have arrays of specific kinds of objects instead.
- pjmlp 4y agoDepends on what kind of lifetimes, yes C++ doesn't do Rust like lifetimes check, at least not without help from static analysis. However C++23 has introduced compiler aware functions to start specific lifetimes out of raw memory pools. https://en.cppreference.com/w/cpp/language/lifetime https://en.cppreference.com/w/cpp/language/lifetime
- spacechild1 4y agoIf you mean std::launder, this has been around since C++17. (Not that I ever had to use it :-)
- pjmlp 4y agohttps://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2590r2.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p25...
- spacechild1 4y agoThanks for the link! I got confused because your cppreference link only talks about std::launder. I did not know about std::start_lifetime_as. Looks very useful! It's crazy that it took so long. Considering that one of the primary design goals of C++ was backwards compatibility with C (too a large extent), I always found it crazy that malloc'ing objects would be undefined behavior. In practice, this has always worked, though. At least now we have a standard-blessed way to do these kind of things. As a side note, does this mean that you can finally cast an array of bytes to an object pointer (e.g. for deserialization) without violating strict-aliasing rules? The usage example in section 1.2 of the linked paper seems to imply this, but it does not mention aliasing anywhere...
- gpderetta 4y agoThis is exactly how C++ work. union { MyObject space; }; // just a bunch of bytes MyObject * myObject = new(&space) MyObject(); // invoke the constructor myObject->~MyObject(); // invoke the destructor If you want RAII use some wrapper like Optional.
- nextaccountic 4y agoIs this syntax new(&something) a new addition? I don't remember it, but then I used to program in C++03
- spacechild1 4y agoIt's been around since forever, precisely because you sometimes need to construct an object in pre-existing storage. Otherwise you couldn't implement std::vector, for example.
- BenFrantzDale 4y agoI assume it’s been around forever. https://en.cppreference.com/w/cpp/language/new https://en.cppreference.com/w/cpp/language/new (Placement-new is the thing you are talking about.) C++20 has `std::construct_at` which does the same thing but is usable at compile time (in a `constexpr` context) https://en.cppreference.com/w/cpp/memory/construct_at https://en.cppreference.com/w/cpp/memory/construct_at
- gpderetta 4y agoTIL about std::construct_at, thanks! edit: not sure why they didn't just bless placement new with constexpr powers though.
- pjmlp 4y agoPlacement new was already a thing in C++ARM.