Skip to content

abnormal size in freestanding/ReleaseSmall mode when accessing a slice #20245

Description

@pierrec

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

The following pieces of code generates much different sizes (build.zig at the end (for some reason I cannot attach the files)):

var font: *Font = undefined;
var glyphs: []u8 = undefined;

const Font = struct {
    glyphs: u8 = 0,
};

export fn _start() callconv(.C) void {
    glyphs = @as([*]u8, @ptrCast(&font.glyphs))[0..10];
    //_ = glyphs[1];
}
var font: *Font = undefined;
var glyphs: []u8 = undefined;

const Font = struct {
    glyphs: u8 = 0,
};

export fn _start() callconv(.C) void {
    glyphs = @as([*]u8, @ptrCast(&font.glyphs))[0..10];
    _ = glyphs[1];
}

As seen here:

 % zig build
 % ls -l zig-out/bin
total 16
-rwxr-xr-x  1 pierre  staff  7976 Jun  9 21:17 build
 % zig build
 % ls -l zig-out/bin
total 1760
-rwxr-xr-x  1 pierre  staff  897984 Jun  9 21:18 build

Build file:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{ .default_target = .{
        .os_tag = .freestanding,
    } });
    const optimize = b.standardOptimizeOption(.{
        .preferred_optimize_mode = .ReleaseSmall,
    });
    const exe = b.addExecutable(.{
        .name = "build",
        .root_source_file = b.path("main.zig"),
        .target = target,
        .optimize = optimize,
    });
    b.installArtifact(exe);
}

Expected Behavior

A slight increase in size when accessing the glyphs slice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions