Skip to content

basic http (not TLS) app is unreasonably big #17051

Closed

Description

@blenessy

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

Produced on Debian Bookworm (amd64).

Put the following in hello_http.zig:

// Based on : https://zigbyexample.github.io/http-client

const std = @import("std");
const allocator = std.heap.page_allocator;
const uri = std.Uri.parse("http://ftp.debian.org/debian/") catch unreachable;  // NOTE: http

pub fn main() !void {
    var client: std.http.Client = .{ .allocator = allocator };
    defer client.deinit();

    var req = try client.request(.GET, uri, .{ .allocator = allocator }, .{});
    defer req.deinit();
    try req.start();
    try req.wait();

    var buf: [1024]u8 = undefined;
    const stdout = std.io.getStdOut().writer();
    while (true) {
        const byte_count = try req.read(&buf);
        if (byte_count == 0) break;
        _ = try stdout.write(buf[0..byte_count]);
    }
}

compile it:

zig build-exe -O ReleaseSmall -fsingle-threaded hello_http.zig

note the size (in bytes) of the binary:

$ du -b hello_http
551448  hello_http

Expected Behavior

On the same system, save the following ziget based (TCP) based implementation to hello_ziget.zig:

const std = @import("std");

pub fn main() !void {
    var allocator = std.heap.page_allocator;

    var conn = try std.net.tcpConnectToHost(allocator, "ftp.debian.org", 80);
    defer conn.close();

    const msg = "GET /debian/ HTTP/1.1\r\nHost: ftp.debian.org\r\nConnection: close\r\n\r\n";
    _ = try conn.write(msg);

    var buf: [1024]u8 = undefined;
    const stdout = std.io.getStdOut().writer();
    while (true) {
        const byte_count = try conn.read(&buf);
        if (byte_count == 0) break;
        _ = try stdout.write(buf[0..byte_count]);
    }
}

compile it:

zig build-exe -O ReleaseSmall -fsingle-threaded hello_ziget.zig

note the size:

$ du -b hello_ziget
46472   hello_ziget

Conclusion

The std.http package brings in a lot dead code when only the HTTP protocol is used. The "dead code" in question is probably the TLS stack, which is clearly unused in http only use-cases.

551k vs 46k might be unimportant in some use-cases, but for IoT devices this is a deal-breaker.

Activity

  1. added
    bug
    on Sep 2, 2023
  2. truemedian commented on Sep 5, 2023

    @truemedian
    Contributor

    std.crypto.tls is currently tied into std.http together rather tightly. I'm not sure but I don't think any of the crypto stuff used in TLS has any optimizations for ReleaseSmall. A flag to disable TLS and optimizations for std.http in ReleaseSmall would both be good changes here.

  3. added this to the 0.15.0 milestone on Sep 7, 2023
  4. Ristovski commented on Sep 25, 2023

    @Ristovski
    Contributor

    Not to mention the compilation is drastically slower.
    Reusing the same examples from OP issue:

    First example (std.http.Client):

    $ time zig build-exe test1.zig 
    
    real	0m5.125s
    user	0m4.795s
    sys	0m0.358s
    

    Second example (std.net.tcpConnectToHost):

     $ time zig build-exe test2.zig 
    
    real	0m0.947s
    user	0m0.875s
    sys	0m0.100s
    
  5. truemedian commented on Jan 22, 2024

    @truemedian
    Contributor

    This was solved in 0eef21d. You can now use a no-TLS std.http by setting the http_disable_tls option to false.

    pub const std_options = struct {
        pub const http_disable_tls = true;
    };

    This will prevent std.http from pulling in any std.crypto code, while also making HTTPS requests an error.

  6. removed
    bug
    on Jan 31, 2024
  7. clickingbuttons commented on Apr 12, 2024

    @clickingbuttons
    Contributor

    I studied this to decide whether to make crypto types generic over Hash functions in #19308 . On 0.12.0-dev.3496+a2df84d0f the size has shrunk from the OP's 551KB to 330KB, but this is still unacceptably large.

    I've run bloaty via:

    zig build-exe ./test.zig --build-id=fast -flto
    cp ./test ./test-lto-stripped
    mv ./test ./test-lto
    strip -s ./test-lto-stripped
    bloaty -d symbols --debug-file=./test-lto --csv -n 0 -d symbols ./test-lto-stripped > size.csv

    Here's size.csv.

    After filtering out debug and DWARF data:

    • std.crypto is 853KB (70%)
      • pcurves is 539KB (44%)
        • p384 is 346KB (28%)
          • p384_64.zig is 173KB (14%)
          • p384_scalar_64.zig is 130KB (11%)
        • p256 is 182KB (15%)
      • tls.Client is 66KB (5%)
        • This includes RSA code.
    • std.http is 41KB (3%)
    • std.compress is 36KB (3%)
      • std.http.HeadParser is 6KB

    Takeaways:

  8. jedisct1 commented on Apr 13, 2024

    @jedisct1
    Contributor

    The fiat-crypto generated code is VERY large. Is there a way to shrink this code size and make it comparable to ed25519?

    Not really. At least not without substantially decreasing performance or losing the security of a verified implementation.
    Curve25519 was specifically designed to use a field order that's very implementation friendly. It's not the case for NIST curves.

    But this is off-topic.

    0eef21d solved the OP problem, so I guess that issue can be closed.

  9. added
    standard library
    on Apr 18, 2024
  10. modified the milestones: 0.15.0, 0.12.0 on Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    optimizationstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions