Repository navigation
Closed
Labels
Milestone
Description
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.zignote the size (in bytes) of the binary:
$ du -b hello_http
551448 hello_httpExpected 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.zignote the size:
$ du -b hello_ziget
46472 hello_zigetConclusion
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.
Reacted by Mitchell Kember
Activity
truemedian commentedon Sep 5, 2023
std.crypto.tlsis currently tied intostd.httptogether 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.Ristovski commentedon Sep 25, 2023
Not to mention the compilation is drastically slower.
Reusing the same examples from OP issue:
First example (
std.http.Client):Second example (
std.net.tcpConnectToHost):std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
std.http.Client: add option to disable https
truemedian commentedon Jan 22, 2024
This was solved in 0eef21d. You can now use a no-TLS std.http by setting the
http_disable_tlsoption to false.This will prevent std.http from pulling in any std.crypto code, while also making HTTPS requests an error.
clickingbuttons commentedon Apr 12, 2024
I studied this to decide whether to make crypto types generic over Hash functions in #19308 . On
0.12.0-dev.3496+a2df84d0fthe 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.csvHere's size.csv.
After filtering out debug and DWARF data:
std.cryptois 853KB (70%)pcurvesis 539KB (44%)p384is 346KB (28%)p384_64.zigis 173KB (14%)p384_scalar_64.zigis 130KB (11%)p256is 182KB (15%)tls.Clientis 66KB (5%)std.httpis 41KB (3%)std.compressis 36KB (3%)std.http.HeadParseris 6KBTakeaways:
Hashis worth making generic for RSA, but not for ECDSA. ECDSA needs help elsewhere.ed25519@jedisct1 ?tls.Client's growth in Refactor TLS, add TLS server #19308 .jedisct1 commentedon Apr 13, 2024
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.