~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zat

atproto utils for zig · mirror of https://tangled.org/zat.dev/zat

0
0
157 lines · 8.5 KB Raw History

without libc

v0.7.0-alpha.3 stops linking libc. zat's Linux binaries are static, and Windows and wasi builds carry no C runtime: zig's standard library makes system calls itself on all three, and std.http brings its own TLS. the one thing standing in the way was compression. v0.7.0-alpha.4 pins the faster decoder that replaced it.

where libc came from

zat made two libc calls of its own: a stress test and the docs publisher each read an environment variable with getenv. both read it through std.process now. zttp set link_libc on its module without calling libc, so every consumer linked it for nothing; zttp 0.3.0 drops the flag.

the rest was websocket.zig. permessage-deflate (RFC 7692) compresses WebSocket messages, and the fork compiled C zlib to do it, which meant linking a C library. every zat stream client goes through websocket.zig: the firehose, jetstream and label subscriptions. none of them negotiates compression, but zlib was compiled in either way, so alpha.2 linked musl on Linux, the UCRT on Windows and wasi-libc on wasi, all for zlib.

pmdeflate

websocket.zig 0.3.0 compresses with pmdeflate, a new pure-zig library of about 700 lines. a Deflater compresses each message with std.compress.flate and strips the 00 00 ff ff the sync flush ends in, as the RFC says to. an Inflater puts it back and decodes, keeping the 32 KiB window between messages unless context takeover is off for that direction.

decoding started out on std's flate.Decompress, which reads one symbol at a time through an Io.Reader and writes each literal through a checked writer call. pmdeflate decodes with its own canonical Huffman tables now: a 10-bit lookup for most codes and a 64-bit bit buffer. 0.1.1 adds a fast loop that runs while a whole symbol is safe to decode without checks. one branchless refill buffers at least 56 bits, enough for two literals followed by a length and a distance with all their extra bits, and matches copy by the word. anything else, such as a code longer than the table, the last bytes of a payload, or the message limit, falls back to the careful path one symbol at a time, so it fails the same way.

pmdeflate decode speed relative to zlib

on a dedicated core, pmdeflate 0.1.1 decodes 3,160 relay frames at 1.28 to 1.33 times zlib 1.3.2's speed, and a generated corpus of noise and long runs at 0.39 to 0.74 times, where zlib's hand-tuned stored-block and run handling wins. that is 1.7 and 1.9 times 0.1.0. getting there turned up a flaw in the benchmark: it decoded zlib through the test oracle, which rebuilds a 32 KiB history after every message, and that made zlib look slow on small frames. zlib now decodes the way a production codec drives it. compression ratio is within 0.6% of zlib's at every level measured.

zlib as the oracle

zlib stayed in pmdeflate's test steps, which consumers never fetch. the property campaign uses hegel-zig, the driver from devlog 016, which now runs on its own zig port of libhegel's engine instead of the Rust library. it runs both codecs: each encodes and the other decodes, and a corrupted payload (a flipped bit, truncation, spliced noise) has to get zlib's exact decision, accepted or refused. a second property writes DEFLATE blocks straight from RFC 1951 instead of from an encoder, each dynamic header valid or wrong in exactly one way, since random corruption almost never yields a header with a single defect. zig build mutants plants sixteen known bugs and runs the campaign against each under ten seeds.

it found that a refused message could leave one byte past the caller's limit in their writer. a review afterwards found two more.

the first was in how a payload ends. the RFC says the receiver appends 00 00 ff ff before decoding. the inflater appended that, then an empty final block, so the decoder would stop by itself. but a stored block declares its length up front, and one claiming more bytes than the payload carried read straight on into what the inflater had appended. 01 09 00 f6 ff, a final stored block of nine bytes with no bytes, decoded to nine bytes the peer never sent. zlib refuses it. random payloads almost never land a length exactly in that tail, which is why the campaign missed it; its generator now builds that block on purpose. the decoder reads the payload in place with only the RFC's four bytes after it, and accepts input only when it ends exactly on a block boundary.

the second was memory. the inflater copied every payload into a buffer it never shrank, so one 4 MiB frame left about 6 MB allocated on an idle connection, whatever its size limit was. the payload isn't copied any more, and output capacity past 256 KiB goes back after each message. a Deflater holds on to its buffer too; trim hands it back, and websocket.zig's server calls it when a connection doesn't retain compression output.

the fast loop brought two more. CI runs the tests on big-endian s390x under qemu, and there the first version decoded short repeating runs wrong: a match two to seven bytes back built an 8-byte pattern and stored it through a u64 bitcast, which does not keep the bytes in order on a big-endian target. it stores the bytes now, and a test decodes runs of every period from 1 to 40, which fails on s390x without the fix.

the campaign also had a gap. the fast loop repeats the careful path's limit checks, and deleting one went unnoticed on every seed, because the next symbol trips the careful path's check and the message is refused anyway. a missing check only shows when the message's last symbol is decoded in the fast loop, and the end of a payload never is, since the loop stops 8 bytes short. RFC 7692 allows a sender to follow the data with empty blocks, so the property now sometimes does, and draws limits inside the message. every fast-loop mutant is caught now, on six to ten seeds of ten, one of them by a crash rather than a failed property.

the Autobahn TestSuite's 517 cases, run against plain, deflate and deflate-with-takeover endpoints, gave the same 1,551 results with pmdeflate 0.1.0 as with zlib.

what it builds

build matrix: zat links no libc on Linux, Windows and wasi

atproto-bench's build matrix cross-compiles a probe that resolves a DID and reads a record over HTTPS with zat, indigo, Atmos, jacquard, rsky and @atproto/api, for eleven targets from one Linux machine. zat's column matches the Go SDKs on Linux, Windows and wasi now, at 615 to 956 KiB a binary. the two Rust SDKs link musl or glibc on Linux, and cross-compiling them for macOS needs Apple's SDK, because their HTTP stacks link Apple frameworks; both build on a Mac. the TypeScript one ships as a 90 to 115 MiB bun binary. built unstripped, alpha.2's probes carry musl's start-up symbols, import the UCRT's api-ms-win-crt DLLs and contain wasi-libc; alpha.3's carry none, and the Windows probe imports only KERNEL32, ntdll and CRYPT32.

macOS links libSystem, as everything there must. FreeBSD is the one cell that differs from Go: Go makes FreeBSD's system calls itself, and zig's std goes through its libc.

upgrading

if your code made its own libc calls and relied on zat linking it, set link_libc on your module. the same goes for a Linux program running websocket.zig's server, which calls libc directly; websocket.zig 0.3.0 is a minor release for that reason. clients need nothing.

also in alpha.2

devlog 017 ended on ordered_keys costing more than half of STAR write throughput, because the writer copied every key to compare it with the next. it now finds the shared prefix with the previous key once, decides the order from the next byte, and copies only what follows: ordered writes of a 100k-record archive went from about 30 million records a second to 49 or 50 million. mst.commonPrefixLen, which MST serialization calls for every entry, compares eight bytes at a time.

alpha.2 also moved zttp's deadlines onto Io.concurrent. a request with a deadline, on an Io that can't run concurrent tasks, fails with error.ConcurrencyUnavailable instead of going out without its deadline. pass init.io from std.process.Init, not std.Options.debug_io.