Home

Awesome

uuid6-zig

This is a prototype UUIDv6 draft 03 implementation in Zig. It also includes versions 1 & 3-5 for comparison.

Installation

The library is contained entirely in src/Uuid.zig. Feel free to copy and customize or clone it as a submodule.

It targets Zig master and may not work on the latest release.

Adding to your build.zig

libOrExe.addPackagePath("uuid6", "path/to/uuid6-zig/src/Uuid.zig");

Usage

There are namespaces for the various UUID versions, v1, v3 - v7. Each has a create(...) method to create a single UUID, or a Source that can be used to create many with shared parameters and/or state.

Sources are not thread-safe and should be protected with a mutex.

Example

const std = @import("std");
const Uuid = @import("uuid6");

pub fn main() anyerror!void {
    var rng = std.rand.DefaultPrng.init(0);
    const source = Uuid.v4.Source.init(&rng.random);

    var i: usize = 0;
    while (i < 10) : (i += 1) {
        const uuid = source.create();
        std.debug.print("{}\n", .{uuid});
    }
}

See also examples/bench.

Performance

The following tables were generated by hyperfine running examples/bench.

Note that the times are for 1e7 UUIDs, so for example each v4 takes ~12ns, and each v3 takes ~99ns.

Thread-safe

CommandMean [ms]Min [ms]Max [ms]Relative
bench -n 10000000 -v 1232.4 ± 1.5230.6235.71.83 ± 0.02
bench -n 10000000 -v 3998.9 ± 2.9995.71004.77.88 ± 0.09
bench -n 10000000 -v 4126.8 ± 1.3123.7129.71.00
bench -n 10000000 -v 5740.7 ± 2.6736.6743.95.84 ± 0.06
bench -n 10000000 -v 6189.1 ± 1.1187.3191.61.49 ± 0.02
bench -n 10000000 -v 7206.7 ± 1.6204.3209.71.63 ± 0.02

Single-threaded

CommandMean [ms]Min [ms]Max [ms]Relative
bench -n 10000000 -v 1229.7 ± 0.9228.3231.31.89 ± 0.02
bench -n 10000000 -v 31005.7 ± 17.4998.51054.98.27 ± 0.17
bench -n 10000000 -v 4121.7 ± 1.3119.8125.71.00
bench -n 10000000 -v 5747.8 ± 8.9737.0760.26.15 ± 0.10
bench -n 10000000 -v 6151.6 ± 1.0150.0153.91.25 ± 0.02
bench -n 10000000 -v 7203.4 ± 1.4201.4206.41.67 ± 0.02

Environment: