thomasfazzari1/zig-hll
A fast, memory-efficient HyperLogLog cardinality estimation library for Zig
A fast, memory-efficient HyperLogLog cardinality estimation library for Zig.
Add to your build.zig.zon
:
.dependencies = .{
.@"zig-hll" = .{
.url = "https://github.com/thomasfazzari1/zig-hll/archive/main.tar.gz",
.hash = "...",
},
},
const std = @import("std");
const HyperLogLog = @import("zig-hll").HyperLogLog;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
// Create HLL with precision 14 (standard)
const HLL = HyperLogLog(.{ .precision = 14 });
var hll = try HLL.init(gpa.allocator());
defer hll.deinit();
// Add some data
try hll.add("user123");
try hll.add("user456");
try hll.add("user123"); // duplicate
// Get cardinality estimate
const count = hll.count();
std.debug.print("Estimated unique items: {}\n", .{count});
}
// Basic usage
const HLL = HyperLogLog(.{ .precision = 14 });
// Thread-safe version
const ThreadSafeHLL = HyperLogLog(.{ .precision = 14, .thread_safe = true });
// High precision (more memory, better accuracy)
const HighPrecisionHLL = HyperLogLog(.{ .precision = 16 });
Precision | Memory (Dense) | Standard Error |
---|---|---|
10 | 1 KB | 3.2% |
12 | 4 KB | 1.6% |
14 | 16 KB | 0.8% |
16 | 64 KB | 0.4% |
zig build benchmark
Generates a detailed report in tests/benchmark_results.md
See benchmark results for a comprehensive output example ran on my machine
init(allocator)
- Create new HLLadd(data)
- Add elementaddHash(hash)
- Add pre-hashed elementcount()
- Get cardinality estimatemerge(other)
- Combine two HLLsclear()
- Reset to empty stateserialize(writer)
- Save statedeserialize(allocator, reader)
- Restore statezig build test # Run tests
zig build example # Run example
zig build benchmark # Performance tests
zig build docs # Generate documentation
This project is licensed under the MIT License - See LICENSE for details.