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

fstat + fmtIntSizeBin causes huge substantial .rodata section #12061

Closed
motiejus opened this issue Jul 9, 2022 · 2 comments
Closed

fstat + fmtIntSizeBin causes huge substantial .rodata section #12061

motiejus opened this issue Jul 9, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@motiejus
Copy link
Contributor

motiejus commented Jul 9, 2022

Zig Version

0.10.0-dev.2880+6f0807f50

Steps to Reproduce

Compile this:

test.zig

const std = @import("std");
const fmtIntSizeBin = std.fmt.fmtIntSizeBin;

pub fn main() void {
    const stat = std.os.fstat(0) catch return;
    const file_size = stat.size;
    std.debug.print("{s}\n", .{fmtIntSizeBin(@intCast(u64, file_size))});
}

Build and analyze:

$ zig build-exe --strip -O ReleaseSmall test.zig && bloaty test 
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  70.6%  31.4Ki  59.8%  31.4Ki    .rodata
   0.0%       0  23.4%  12.3Ki    .bss
  19.1%  8.51Ki  16.2%  8.51Ki    .text
   8.3%  3.70Ki   0.0%       0    [Unmapped]
   0.8%     384   0.0%       0    [ELF Section Headers]
   0.6%     280   0.5%     280    [ELF Program Headers]
   0.2%     103   0.0%       0    .comment
   0.1%      64   0.1%      64    [ELF Header]
   0.1%      41   0.0%       0    .shstrtab
   0.0%       8   0.0%       8    [LOAD #1 [R]]
 100.0%  44.5Ki 100.0%  52.6Ki    TOTAL

Notice that .rodata section is over 30KiB, as it seems to include the full errol3 table, as well as some other stuff I wasn't able to recognize.

Removing either fstat (by changing to a constant) or fmtIntSizeBin (by changing to a raw value) reduces the .rodata section to a few hundred bytes. Desired outcome: the .rodata section of the example above should also be a few hundred bytes. I am not sure why a combination of those causes it to include that much stuff, but, well, it does.

The result is the same with both stage1 and stage2.

@motiejus motiejus added the bug Observed behavior contradicts documented or intended behavior label Jul 9, 2022
@Vexu
Copy link
Member

Vexu commented Jul 9, 2022

I am not sure why a combination of those causes it to include the miscellaneous stuff, but, well, it does.

My guess is that LLVM cannot aggressively inline the float formatting when the value is not known at compile time.

Desired outcome: the .rodata section of the example above should also be a few hundred bytes.

I don't think that is possible with the current float formatting implementation.

Relevant #1299

@motiejus
Copy link
Contributor Author

motiejus commented Jul 9, 2022

Thanks, spot on. Will optimize the floats.

@motiejus motiejus closed this as completed Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants