6 ms·
Allocgate: Restructuring how allocators work in Zig
- celeritascelery 5y agoI am not fully understanding how fat-pointers allow LLVM to devirtualize the function calls. If the allocators are polymorphic then a particular piece of code doesn't know which vtable it will get at run time correct?
- ayende 5y agoYou aren't modifying the same object When you have a fat pointer, llvm can tell you are modifying the ptr, bot the vtable You can then cache the function call When your vtable is in the object you are mutating, it needs to read each time
- celeritascelery 5y agoDoes LLVM do the inline caching itself? Or does it just enable zig to do this optimization?
- ayende 5y agoWhat happens is likely that the code gen can do the virtual lookup once, instead on each loop iteration This is llvm, bit zig
- Spex_guy 5y agoIt depends a lot, but in practice in Zig devirtualization is effectively constant propagation. The compiler needs to see the place where the vtable is created, and follow that to the place where virtual functions are called, ensuring along the way that nothing modifies the vtable. This is not possible for all uses of interfaces, but it is possible for many of them, especially ones where the interface is sort of "temporary" and you are usually passing around the implementation. These are the cases targeted by this change. The difference in results has to do with pointer provenance tracking and aliasing. With both approaches, the first call to an interface function will almost definitely be devirtualized. The problem is that that first call will also modify implementation state. If the implementation function is not inlined (which is common), this is tracked as a modification to the memory region containing the implementation state. But with the fieldParentPtr model, that's the same memory region containing the vtable! So this breaks constant propagation on the vtable and any later calls must always be fully virtual, even if the optimizer can see the whole way from vtable creation to virtual call.
- celeritascelery 5y agoSo does this optimization only work when the declaration of the allocator and its uses are in the same codegen unit? Otherwise the vtable of the allocator can’t be known at compile time.
- Spex_guy 5y agoZig's build model is mostly to combine everything into one compilation unit (from multiple files). Since Zig doesn't have a preprocessor, the compiler can reason about incremental compilation and avoid rebuilding everything for every change. Incremental optimization is an open question that we will need to tackle at some point, but the focus for now is on getting the language to a point where it can be stable first. You can introduce intentional graph cuts if you want, and build multiple objects, but you are limited to the C ABI at these boundaries.
- levzettelin 5y agoDoes anyone know if RAII types will ever be a thing in Zig?
- asddubs 5y agomaybe since Zig still is making breaking changes, they can call it IIRA
- kristoff_it 5y ago`defer` and `errdefer` give you 90% of what destructors & co give you for 0% of the complexity. For now the the closest thing to RAII is this proposal, but there is no guarantee that it will be accepted. https://github.com/ziglang/zig/issues/782 https://github.com/ziglang/zig/issues/782
- initplus 5y agoI find in C++ at least reasoning about RAII is always surprisingly complex. The second you have to write a custom destructor you get into the weeds of reasoning about copy/move/copy-assignment etc. https://en.cppreference.com/w/cpp/language/rule_of_three https://en.cppreference.com/w/cpp/language/rule_of_three C++ RAII also rubs up painfully against handle based APIs (looking at you Windows) in my experience. There is a lack of standardized RAII wrappers for handle types like there is for pointers. Yes you can pull in a custom handle RAII wrapper but at that point it's simpler to just manage manually. Defer on the other hand is simple. Anyone can understand it in five minutes.
- jcelerier 5y ago> There is a lack of standardized RAII wrappers for handle types like there is for pointers. .. what's wrong with #include <memory> template<typename T, auto Free> using safe_handle_t = std::unique_ptr<T, decltype([] (auto p) { Free(p); })>; using file_handle = safe_handle_t<FILE, fclose>; void file_example() { file_handle f{fopen("foo", "r")}; }
- 5y ago
- khiner 5y agoThis was a fantastic, thorough explanation, thank you for putting in the effort!
- Shadonototra 5y agowhy not just an an interface type instead of this giant mess?
- kristoff_it 5y ago"allocgate" is actually a meme name that we purposely gave to this API change. In reality it's not a big deal and that's the superpower that a language v0 has: you can make breaking changes. As for the builtin interface type, why get locked into one particular implementation when you can have all of them by leaving the choice to the programmer.
- Shadonototra 5y ago> As for the builtin interface type, why get locked into one particular implementation when you can have all of them by leaving the choice to the programmer. that's a very good point!
- williamstein 5y agoI agree that it's not a big deal. I am not a Zig developer, but I've written a few thousand lines of Zig code in the last few months that extensively uses allocators all over the place, and it only took me a few minutes to update my code to work with this API change. Also, having seriously played around in Zig for a few months writing high performance pure mathematics and number theory code for fun, I *really, really* like it. Zig is a fantastic language for certain application domains.
- twmb 5y agoAre there performance worries about passing around two pointers for anything that needs to allocate, as well as storing these pointers in a struct? AFAICT this basically means two registers are eaten, and a lot of types have effectively 16 bytes of overhead. It seems like this could quickly change the calculus on what fits within cache lines and what doesn't, which people often care about for very high performance code. I wonder if it's possible to change the compiler to detect that, if what is being used in arguments is the global default allocator, the first argument can be stripped and all references inside the function can be replaced with the global pointer. Potentially the same concept could apply to allocators that use thread local storage. (perhaps these optimizations already exist?)
- jasonhansel 5y agoI believe that "devirtualization"--the optimization mentioned in the OP--will do exactly what you're describing, by rewriting the virtual function call as a static call to the allocator when the vtable can be determined from the callsite at compile-time.
- Spex_guy 5y agoThis is a worry, but it's not as bad as you might initially think. The first thing to notice is that even though the "interface pointer" got fatter, the implementation got much leaner, as it no longer contains the vtable. Vtables are now shared between instances of the same type, so total memory use has gone down, and implementations can be packed more densely. If you're worried about overall cache usage, this is a net positive. The first load, from the fat pointer, is very likely to be in cache. The second load, from the vtable, will be in cache if you have used the same type recently. Which is likely if you have thousands of objects, you probably do not have thousands of implementations. There is some additional latency because the virtual function load is now two pointer dereferences instead of one. However, C++ and Go both use double-dereference models like this, and it seems to be working fine for them. Additionally, if virtual calls like this are on your critical fast path, you have bigger problems :P
- pcwalton 5y agoThis is an example of how certain optimizations (specifically, making vtables immutable) are hard in LLVM, because of how low-level it is. Language-specific high-level IRs, such as Swift SIL, can allow compilers to perform these kinds of optimizations more easily. Of course, they're a lot of work to implement.
- Spex_guy 5y agoIt's true that language specific IRs are more powerful, but that isn't the problem in this example. Interfaces aren't part of Zig at the language level, nor are object lifetimes over which to make the vtables immutable. Having a memory model where memory is not innately typed is what makes this problem difficult to optimize, the IR has very little to do with it.
- dnautics 5y ago> a memory model where memory is not innately typed This is intriguing. Do you have examples of systems where memory is typed? Or some ideas?
- brandmeyer 5y agoBitC used a strictly typed memory model.
- Spex_guy 5y agoMemory is typed in C++. You must use placement new to imbue memory with a non-POD type, and that memory continues to have that type until its destructor is invoked. This is what allows the optimizer to assume that vtable pointers don't change while an object is alive. Effects of the typedness of memory can also be seen in the strict aliasing rules. Once a piece of memory has taken on a type, it may no longer be aliased by pointers of different types, until the memory is relinquished back into typelessness by a destructor.
- dnautics 5y ago