Skip to content
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

Initialization of large struct field increases binary size #22462

Open
rockorager opened this issue Jan 11, 2025 · 0 comments
Open

Initialization of large struct field increases binary size #22462

rockorager opened this issue Jan 11, 2025 · 0 comments
Milestone

Comments

@rockorager
Copy link
Contributor

rockorager commented Jan 11, 2025

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

Initializing a struct with a large field results in large binary sizes, unless you manually set these fields with memset.

const std = @import("std");

const Foo = struct {
    bar: [1_000_000]u64 = undefined,
};

fn a() void {
    const foo: Foo = .{};
    _ = foo.bar;
}

fn b() void {
    var foo: Foo = undefined;
    @memset(&foo.bar, 0);
    _ = foo.bar;
}

pub fn main() !void {
    //a(); // Binary size = 10_253_888
    b(); // Binary size = 2_254_096
}

Expected Behavior

Per Andrew on IRC:

you can go ahead and file a zig bug for this because the communication to the compiler is fine, arguably zig should lower large field inits with memset rather than a big memcpy

@rockorager rockorager added the bug Observed behavior contradicts documented or intended behavior label Jan 11, 2025
@andrewrk andrewrk added optimization and removed bug Observed behavior contradicts documented or intended behavior labels Jan 11, 2025
@andrewrk andrewrk added this to the 0.15.0 milestone Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants