Skip to content

No dynamic memory allocation at comptime #5881

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

Closed
ghost opened this issue Jul 16, 2020 · 4 comments
Closed

No dynamic memory allocation at comptime #5881

ghost opened this issue Jul 16, 2020 · 4 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@ghost
Copy link

ghost commented Jul 16, 2020

Goes hand in hand with #5718. I hate hate HATE the current comptime memory management strategy. It flies in the face of everything Zig stands for. It breaks rules just for the sake of it. It requires fundamentally different logic at comptime vs runtime. It's ill-suited for applications that do heavy computations at comptime (regex, interfaces, perfect hashing etc.). #5873 proposes a better solution, and this proposal makes another thing explicit: a pointer should not be able to manage the memory it points to. So code like this should not be allowed. It requires complex logic in the compiler to detect lifetimes (a motherfreaking GARBAGE COLLECTOR for Kristoff's sake), as well as built-in reallocation logic. We invented better ways to do these things, let's not rely on unreliable solutions. Let's use the same programmer's model at comptime that we do at runtime, and only break it when we have to, not just for the sake of it.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jul 16, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Jul 16, 2020
@andrewrk andrewrk changed the title Proposal: NO DYNAMIC MEMORY ALLOCATION AT COMPTIME No dynamic memory allocation at comptime Jul 16, 2020
@andrewrk
Copy link
Member

Can you give an example of something that this proposal would disallow, that is not already covered by other proposals / bug reports?

@ghost
Copy link
Author

ghost commented Jul 16, 2020

There's one linked in the OP. err_handlers is a slice, but also the source and primary handle of the memory it points to. The linked line does not resize err_handlers, but it does resize the memory pointed to by it, which could require an implicit relocation -- even though, from looking at the code, no heap allocation was attempted. This is a fundamentally different operating principle than runtime code, and violates the "one language" principle we've worked to achieve in other areas. This proposal would disallow it. I haven't seen this issue covered elsewhere.

@andrewrk
Copy link
Member

Here's an even simpler example of dynamic memory allocation at comptime:

fn initArray(comptime len: usize) [len]u8 {
    var array: [len]u8 = undefined;
    for (array) |*elem, i| {
        elem.* = i * 10;
    }
    return array;
}

var array = initArray(100);

This, along with the linked code, is planned to be part of the language specification.

Note that garbage collection at compile time is quite normal, and already happens at the link stage of all ahead-of-time compilers, when it finds out what code/data can be omitted from the final binary. (This is what the linker flag --gc-sections does.) Many optimization passes of compilers could be modeled as garbage collectors.

@ghost
Copy link
Author

ghost commented Jul 16, 2020

That's not quite the same thing. The data in that case is managed directly, rather than through a pointer, and not resized once it's created. That's consistent with runtime semantics.

Regarding GC within the compiler, ok, sure, but we shouldn't expose that directly to userspace comptime code. That locks users in to one memory management strategy, which is not well suited to all cases, as well as encouraging a style for comptime code which is invalid for runtime code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

1 participant