-
-
Notifications
You must be signed in to change notification settings - Fork 3k
comptime allocator #25198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
comptime allocator #25198
Conversation
to runtime function pointer when comptime-known.
The implementation you propose in #1291 is not valid:
This PR is also not valid. In order to do what you're trying to do, you would need to check that this coerced function never becomes runtime-known. This is a task we have so far designed the language rules to avoid needing, because it's a lot of work: the problem is aggregate values, which can become runtime-known and then require implementing a recursive check through all sub-values. This is because this language change is just a hack: inline functions are not normal functions. Luckily for you, it's not even useful for #1291. We know this because Comptime allocators work in Zig today---no language changes needed(*). Here is an implementation with usage: https://zigbin.io/2e8af7 The tradeoff is that, for correctness reasons, you cannot access the memory at runtime (everyone's favourite "runtime value contains reference to comptime var" error). This tradeoff is one we agreed on around a year ago when I rewrote the comptime memory access logic; the alternatives are much more complex, so we chose this approach. If the use case of using Either way, this diff is both incorrect (a lot more complex logic is required to make this change sound) and pointless (it makes no difference to #1291); I strongly believe this should be closed. (*): there are currently some caveats related to how Zig handles comptime memory. The biggest one is that Zig does not currently allow reinterpreting a |
this might not be the right place for me to share this, so let me know if I should put it elsewhere. I think @andrewrk started work on this PR after a conversation with me on IRC. I am here to share the use case I had and the experience I had trying different approaches, in case it helps inform the discussion. if not, feel free to disregard. I've been messing around with task scheduling graphs in comptime for fun. ultimately I need some kind of growable comptime list, and nodes would need to store function pointers. I just tried to use your allocator, @mlugg, and I quickly ran into the caveat you outlined at the end of your comment, which I now fully comprehend after experiencing it first hand. the ultimately I just implemented my own dumb |
Yeah, it's an unfortunate problem which really limits where you can use Luckily, this is absolutely solvable. It just requires some language and compiler design work, which I unfortunately won't be able to get to for a while; sorry about that.
If you need it, a possible workaround would be storing them as
Hmm, I'm certainly not aware of any such bug off the top of my head. So yeah, if you would be able to repro it and file an issue, that would be appreciated.
Yeah, that's a good idea for the time being. To be honest, the main use case for a comptime allocator is to reuse existing code which uses allocators, e.g. parsing file formats at comptime by just calling into a normal runtime parsing library. If you're writing comptime-only code in the first place, then you can easily do what you did here and just use comptime-specific abstractions. FWIW, when I'm doing hacky comptime stuff (that never sees the light of day!), here's my go-to implementation---it's basically just a slightly more featureful version of yours:
|
The comptime allocator allocates into .data / .cdata and relies on linker garbage collection by using global (array) variables. This is better than using
std.heap.FixedBufferAllocator
because it does not cause unused data within the fixed buffer to be part of the program's address space.First commit: allow coercion of generic function pointer to runtime function pointer when comptime-known.
Intended to close #1291 but does not accomplish it yet:
Next step is to figure out why this compile error incorrectly thinks the alloc function is not comptime known.
Merge Checklist