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.
Zig Version
0.11.0
Steps to Reproduce and Observed Behavior
Produced on Debian Bookworm (amd64).
Put the following in
hello_http.zig:compile it:
note the size (in bytes) of the binary:
Expected Behavior
On the same system, save the following ziget based (TCP) based implementation to
hello_ziget.zig:compile it:
note the size:
Conclusion
The
std.httppackage 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.