ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.14k stars 2.56k forks source link

zero'd global constants consume @sizeOf(T) disk space #21988

Open Jarred-Sumner opened 1 week ago

Jarred-Sumner commented 1 week ago

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

  1. Build this:
    
    const massive: [1024 * 1024 * 128]u8 = .{0} ** (1024 * 1024 * 128);
    const std = @import("std");

pub fn main() void { std.mem.doNotOptimizeAway(&massive); }

2. `zig build-exe -O ReleaseFast huge.zig`
3. `ls -lSh huge`

Resulting size: 129 MB

Compare that with this:
```zig
var massive: [1024 * 1024 * 128]u8 = .{0} ** (1024 * 1024 * 128);
const std = @import("std");

pub fn main() void {
    std.mem.doNotOptimizeAway(&massive);
}
  1. zig build-exe -O ReleaseFast huge.zig
  2. ls -lSh huge

Resulting size: 177 KB

Expected Behavior

global var and global const should use a similar amount of disk space.

If we use bloaty to comapre the output:

When using global const:

❯ bloaty -d symbols -s file -n 100000 huge
    FILE SIZE        VM SIZE
 --------------  --------------
  99.1%   128Mi  99.1%   128Mi    [__TEXT,__const]
   0.8%  1.00Mi   0.8%  1.02Mi    [__LINKEDIT]
   0.1%   107Ki   0.1%   107Ki    _main
   0.0%  32.3Ki   0.0%       0    [Unmapped]
   0.0%  5.13Ki   0.0%  5.13Ki    [__TEXT,__eh_frame]
   0.0%  4.05Ki   0.0%  4.05Ki    [__TEXT,__unwind_info]
   0.0%  2.78Ki   0.0%  2.78Ki    [__TEXT,__cstring]
   0.0%  2.05Ki   0.0%  2.05Ki    [__DATA,__const]
   0.0%  1.85Ki   0.0%  1.75Ki    [Mach-O Headers]
   0.0%     336   0.0%     336    [__TEXT,__stub_helper]
   0.0%     312   0.0%     312    [__TEXT,__stubs]
   0.0%     208   0.0%     208    [__DATA,__la_symbol_ptr]
   0.0%      36   0.0%     140    __mh_execute_header
   0.0%      24   0.0%      24    [__DATA,__thread_vars]
   0.0%      17   0.0%      17    [__TEXT]
   0.0%      16   0.0%      16    [__DATA_CONST,__got]
   0.0%       8   0.0%       8    [__DATA,__data]
   0.0%       0   0.0%     144    [__DATA,__bss]
   0.0%       0   0.0%       8    [__DATA,__thread_bss]
 100.0%   129Mi 100.0%   129Mi    TOTAL

When using a global var:

    FILE SIZE        VM SIZE
 --------------  --------------
  65.8%   107Ki   0.1%   107Ki    _main
  19.8%  32.3Ki   0.0%       0    [Unmapped]
   3.1%  5.13Ki   0.0%  5.13Ki    [__TEXT,__eh_frame]
   2.5%  4.05Ki   0.0%  4.05Ki    [__TEXT,__unwind_info]
   2.2%  3.52Ki   0.0%  3.52Ki    [__TEXT,__const]
   1.9%  3.08Ki   0.0%  15.9Ki    [__LINKEDIT]
   1.7%  2.78Ki   0.0%  2.78Ki    [__TEXT,__cstring]
   1.3%  2.05Ki   0.0%  2.05Ki    [__DATA,__const]
   1.1%  1.85Ki   0.0%  1.75Ki    [Mach-O Headers]
   0.2%     336   0.0%     336    [__TEXT,__stub_helper]
   0.2%     312   0.0%     312    [__TEXT,__stubs]
   0.1%     208   0.0%     208    [__DATA,__la_symbol_ptr]
   0.0%      36   0.0%     140    __mh_execute_header
   0.0%      24   0.0%      24    [__DATA,__thread_vars]
   0.0%      17   0.0%      17    [__TEXT]
   0.0%      16   0.0%      16    [__DATA_CONST,__got]
   0.0%       8   0.0%       8    [__DATA,__data]
   0.0%       0  99.9%   128Mi    [__DATA,__bss]
   0.0%       0   0.0%       8    [__DATA,__thread_bss]
 100.0%   163Ki 100.0%   128Mi    TOTAL
Vexu commented 1 week ago

Related to #18404 but not caused by #20120