8000 GitHub - c4milo/stdx: Compression, JSON and base64 for Zig: gzip, deflate, zstd and brotli encoders and decoders, written from the RFCs, with no heap and no I/O. Benchmarked against zlib, libdeflate, libzstd, Google's brotli and simdjson. · GitHub
[go: up one dir, main page]

Skip to content
c4miloPublic

About

Compression, JSON and base64 for Zig: gzip, deflate, zstd and brotli encoders and decoders, written from the RFCs, with no heap and no I/O. Benchmarked against zlib, libdeflate, libzstd, Google's brotli and simdjson.

Topics

Resources

Contributing

Security policy

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

stdx

main coverage license zig release

stdx extends Zig's standard library with compression, checksums, JSON, base 64, regular expressions and Unicode. It groups its modules as std groups its own, and each is written from its RFC or from the specification that it names. Nothing in stdx allocates, opens a file, starts a thread or reads a clock, so it fits the hot path of a server.

Modules

Each module's folder holds a README with its examples, its options, its limits and its speed.

Module What it holds
stdx.compress.deflate DEFLATE (RFC 1951): an encoder and a decoder
stdx.compress.zlib The zlib container (RFC 1950): HTTP's deflate coding
stdx.compress.gzip The gzip container (RFC 1952): HTTP's gzip coding
stdx.compress.zstd Zstandard (RFC 8878, with RFC 9659's limits for HTTP): HTTP's zstd coding
stdx.compress.brotli brotli (RFC 7932): HTTP's br coding
stdx.hash CRC-32, Adler-32 and XXH64, with SIMD paths that the CPU selects at run time
stdx.encoding.json JSON (RFC 8259) with JSON text sequences (RFC 7464): an encoder and a pull decoder
stdx.encoding.base64 Base 64 (RFC 4648): an encoder and a decoder
stdx.regex Regular expressions in RFC 9485's grammar with RE2's syntax, matched in time linear in the text
stdx.unicode Unicode 17.0.0's categories, scripts and case folding
stdx.unicode.utf8 UTF-8 (RFC 3629), with a validator proved in Lean
stdx.platform What the CPU offers, which a program probes once when it starts
stdx.codec The streaming contract that every codec shares

At a glance

Area What stdx does
Memory No allocator anywhere in src/, tests included. Your program owns each state and buffer, and each codec's state has a comptime size.
I/O None. The one exception is the probe of stdx.platform, which makes one system call on macOS.
Streaming A call takes any amount of input, writes what fits, and says whether it needs more input, more room, or is done. Each decoder is also a std.Io.Reader, and each encoder a std.Io.Writer.
Output An encoder writes the same bytes on every host, in every build mode, and for every split of its input.
Threads No global state: run codecs on as many threads as you like.
Safety Assertions stay on in production. Hostile input returns an error value, and the code cites the RFC section behind each refusal.
Testing Differential checks against zlib, Wuffs, libzstd, Google's brotli, Go's regexp and RE2's own test logs. A fuzzer every night. Lean proofs of the UTF-8 validator, of the state machine that reads JSON numbers, and of a model of the regex matcher.

Install

Run zig fetch to add stdx to your build.zig.zon:

zig fetch --save git+https://github.com/c4milo/stdx#v0.3.0

Then import it in build.zig:

const stdx = b.dependency("stdx", .{ .target = target, .release = true });
exe.root_module.addImport("stdx", stdx.module("stdx"));
  • Zig compiles only the parts that a program uses.
  • stdx builds in Debug or ReleaseSafe only, because its assertions stay on (decision 17). .release = true selects ReleaseSafe, and stdx takes no .optimize option.
  • The package exports stdx alone. A module's own name, such as stdx.module("gzip"), stops the build: use stdx.compress.gzip.

Example

Call stdx.platform.CpuFeatures.detect() once when the program starts, and pass the result to each codec and searcher.

/// Decodes the gzip file `body` into `output`, and returns the decoded octets.
fn gunzip(features: stdx.platform.CpuFeatures, body: []const u8, output: []u8) ![]u8 {
    var decoder: stdx.compress.gzip.Decoder = undefined; // about 43 KiB
    decoder.init(features);
    const whole = try decoder.decode_all(body, output);
    return output[0..whole.written];
}

Each module's README holds its own examples. Every example comes from tools/consumer/, a program that zig build test builds against this tree and runs.

Performance

The benchmarks compare stdx with other libraries on GitHub's hosted runners. Every library of a row runs in the same job (decision 20). On the Arm Neoverse N2 runner:

  • brotli decodes faster than Google's decoder on all 39 corpus files, at 1.10 to 3.36 times its speed.
  • gzip decodes faster than libdeflate on 31 of the 39 files.
  • json's TextWriter writes JSON at 1.64 to 16.35 times serde_json's speed.
  • regex was tuned on each set that measures it. On rebar's benchmarks outside its curated set it runs 125 of 181 searches faster than Rust's regex crate, and 55 slower, the slowest by about 140 times.
  • Zstandard decodes slower than libzstd on 25 of the 39 files, and libzstd's level 3 is both faster and smaller than stdx's levels 1 and 3.

Each module's README shows its chart. docs/benchmarks.md shows every chart, lists the counts of each, the losses included, and says how to run each benchmark again.

Status

stdx is before 1.0, and its releases follow SemVer 2.0.0 (decision 65). The latest release is 0.3.0. Open work is in the issues.

Development

zig build test

License

stdx is licensed under the Apache License, Version 2.0 (decision 83).

About

Compression, JSON and base64 for Zig: gzip, deflate, zstd and brotli encoders and decoders, written from the RFCs, with no heap and no I/O. Benchmarked against zlib, libdeflate, libzstd, Google's brotli and simdjson.

Topics

Resources

Contributing

Security policy

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages

0