Compare commits
9 Commits
fa97344b24
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e89cc5f4f | |||
| 052dd57cdd | |||
| 3e82fde54a | |||
| 7bdcbfd6f5 | |||
| 7a95a1fac0 | |||
| b0389bb1a4 | |||
| f8153f6569 | |||
| 722427b693 | |||
| 56c5f254ee |
40
.github/workflows/main.yml
vendored
Normal file
40
.github/workflows/main.yml
vendored
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
name: Nightly
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
paths:
|
||||||
|
- "**.zig"
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
schedule:
|
||||||
|
- cron: "0 0 * * *"
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: goto-bus-stop/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: 0.12.0-dev.2063+804cee3b9
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- run: zig fmt src/*.zig
|
||||||
|
test:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||||
|
runs-on: ${{matrix.os}}
|
||||||
|
steps:
|
||||||
|
- uses: goto-bus-stop/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: 0.12.0-dev.2063+804cee3b9
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- run: zig build test
|
||||||
|
bench:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: goto-bus-stop/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: 0.12.0-dev.2063+804cee3b9
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- run: zig build -Doptimize=ReleaseFast -Dcpu=baseline bench
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
.zig-cache/
|
||||||
zig-cache/
|
zig-cache/
|
||||||
zig-out/
|
zig-out/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|||||||
30
build.zig
30
build.zig
@@ -15,13 +15,13 @@ pub fn build(b: *std.Build) void {
|
|||||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const hamt_module = b.addModule("hamt", .{ .source_file = .{ .path = "src/lib.zig" } });
|
const mod = b.addModule("hamt", .{ .root_source_file = b.path("src/lib.zig") });
|
||||||
|
|
||||||
const lib = b.addStaticLibrary(.{
|
const lib = b.addStaticLibrary(.{
|
||||||
.name = "hamt",
|
.name = "hamt",
|
||||||
// In this case the main source file is merely a path, however, in more
|
// In this case the main source file is merely a path, however, in more
|
||||||
// complicated build scripts, this could be a generated file.
|
// complicated build scripts, this could be a generated file.
|
||||||
.root_source_file = .{ .path = "src/lib.zig" },
|
.root_source_file = b.path("src/lib.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
@@ -33,30 +33,32 @@ pub fn build(b: *std.Build) void {
|
|||||||
|
|
||||||
// Benchmark
|
// Benchmark
|
||||||
const bench = b.addExecutable(.{
|
const bench = b.addExecutable(.{
|
||||||
.name = "hamt-benchmark",
|
.name = "hamt-bench",
|
||||||
.root_source_file = .{ .path = "src/bench.zig" },
|
.root_source_file = b.path("src/bench.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = .ReleaseFast,
|
||||||
|
.link_libc = true,
|
||||||
});
|
});
|
||||||
bench.addModule("hamt", hamt_module);
|
bench.root_module.addImport("hamt", mod);
|
||||||
|
|
||||||
const bench_cmd = b.addRunArtifact(bench);
|
const bench_cmd = b.addRunArtifact(bench);
|
||||||
|
|
||||||
const bench_step = b.step("bench", "Run benchmark");
|
const bench_step = b.step("bench", "Run benchmark");
|
||||||
bench_step.dependOn(&bench_cmd.step);
|
bench_step.dependOn(&bench_cmd.step);
|
||||||
|
|
||||||
// Creates a step for unit testing. This only builds the test executable
|
// Creates a step for unit testing. This only builds the test executable
|
||||||
// but does not run it.
|
// but does not run it.
|
||||||
const main_tests = b.addTest(.{
|
const lib_unit_tests = b.addTest(.{
|
||||||
.root_source_file = .{ .path = "src/tests.zig" },
|
.root_source_file = b.path("src/tests.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
const run_main_tests = b.addRunArtifact(main_tests);
|
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
||||||
|
|
||||||
// This creates a build step. It will be visible in the `zig build --help` menu,
|
// Similar to creating the run step earlier, this exposes a `test` step to
|
||||||
// and can be selected like this: `zig build test`
|
// the `zig build --help` menu, providing a way for the user to request
|
||||||
// This will evaluate the `test` step rather than the default, which is "install".
|
// running the unit tests.
|
||||||
const test_step = b.step("test", "Run library tests");
|
const test_step = b.step("test", "Run unit tests");
|
||||||
test_step.dependOn(&run_main_tests.step);
|
test_step.dependOn(&run_lib_unit_tests.step);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,72 @@
|
|||||||
.{
|
.{
|
||||||
.name = "hamt",
|
// This is the default name used by packages depending on this one. For
|
||||||
.version = "0.1.0",
|
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||||
.dependencies = .{},
|
// as the key in the `dependencies` table. Although the user can choose a
|
||||||
|
// different name, most users will stick with this provided value.
|
||||||
|
//
|
||||||
|
// It is redundant to include "zig" in this name because it is already
|
||||||
|
// within the Zig package namespace.
|
||||||
|
.name = "hamt",
|
||||||
|
|
||||||
|
// This is a [Semantic Version](https://semver.org/).
|
||||||
|
// In a future version of Zig it will be used for package deduplication.
|
||||||
|
.version = "0.1.0",
|
||||||
|
|
||||||
|
// This field is optional.
|
||||||
|
// This is currently advisory only; Zig does not yet do anything
|
||||||
|
// with this value.
|
||||||
|
//.minimum_zig_version = "0.11.0",
|
||||||
|
|
||||||
|
// This field is optional.
|
||||||
|
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||||
|
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||||
|
// Once all dependencies are fetched, `zig build` no longer requires
|
||||||
|
// internet connectivity.
|
||||||
|
.dependencies = .{
|
||||||
|
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||||
|
//.example = .{
|
||||||
|
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||||
|
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||||
|
// // the new URL.
|
||||||
|
// .url = "https://example.com/foo.tar.gz",
|
||||||
|
//
|
||||||
|
// // This is computed from the file contents of the directory of files that is
|
||||||
|
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||||
|
// // `paths`.
|
||||||
|
// //
|
||||||
|
// // This field is the source of truth; packages do not come from a `url`; they
|
||||||
|
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||||
|
// // obtain a package matching this `hash`.
|
||||||
|
// //
|
||||||
|
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||||
|
// .hash = "...",
|
||||||
|
//
|
||||||
|
// // When this is provided, the package is found in a directory relative to the
|
||||||
|
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||||
|
// // computed. This field and `url` are mutually exclusive.
|
||||||
|
// .path = "foo",
|
||||||
|
|
||||||
|
// // When this is set to `true`, a package is declared to be lazily
|
||||||
|
// // fetched. This makes the dependency only get fetched if it is
|
||||||
|
// // actually used.
|
||||||
|
// .lazy = false,
|
||||||
|
//},
|
||||||
|
},
|
||||||
|
|
||||||
|
// Specifies the set of files and directories that are included in this package.
|
||||||
|
// Only files and directories listed here are included in the `hash` that
|
||||||
|
// is computed for this package. Only files listed here will remain on disk
|
||||||
|
// when using the zig package manager. As a rule of thumb, one should list
|
||||||
|
// files required for compilation plus any license(s).
|
||||||
|
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||||
|
// the build root itself.
|
||||||
|
// A directory listed here means that all files within, recursively, are included.
|
||||||
|
.paths = .{
|
||||||
|
"build.zig",
|
||||||
|
"build.zig.zon",
|
||||||
|
"src",
|
||||||
|
// For example...
|
||||||
|
//"LICENSE",
|
||||||
|
//"README.md",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ const std = @import("std");
|
|||||||
const HashArrayMappedTrie = @import("hamt").HashArrayMappedTrie;
|
const HashArrayMappedTrie = @import("hamt").HashArrayMappedTrie;
|
||||||
|
|
||||||
const StringContext = struct {
|
const StringContext = struct {
|
||||||
pub const Digest = u64;
|
pub const Digest = u32;
|
||||||
|
|
||||||
pub fn hash(input: []const u8) Digest {
|
pub fn hash(input: []const u8) Digest {
|
||||||
return std.hash.Wyhash.hash(0, input);
|
return @truncate(std.hash.Wyhash.hash(0, input));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eql(left: []const u8, right: []const u8) bool {
|
pub fn eql(left: []const u8, right: []const u8) bool {
|
||||||
@@ -17,16 +17,13 @@ const StringArrayHashMap = std.array_hash_map.StringArrayHashMap(void);
|
|||||||
const StringHashMap = std.hash_map.StringHashMap(void);
|
const StringHashMap = std.hash_map.StringHashMap(void);
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
const allocator = std.heap.c_allocator;
|
||||||
defer std.debug.assert(!gpa.deinit());
|
|
||||||
|
|
||||||
const allocator = gpa.allocator();
|
|
||||||
const elem_count = 1000;
|
const elem_count = 1000;
|
||||||
|
|
||||||
const keys = try allocator.alloc([32]u8, elem_count);
|
const keys = try allocator.alloc([32]u8, elem_count);
|
||||||
defer allocator.free(keys);
|
defer allocator.free(keys);
|
||||||
|
|
||||||
var rand = std.rand.DefaultPrng.init(1337);
|
var rand = std.rand.DefaultPrng.init(0);
|
||||||
for (keys) |*key| rand.fill(key);
|
for (keys) |*key| rand.fill(key);
|
||||||
|
|
||||||
var trie = try HashArrayMappedTrie([]const u8, void, StringContext).init(allocator);
|
var trie = try HashArrayMappedTrie([]const u8, void, StringContext).init(allocator);
|
||||||
|
|||||||
35
src/trie.zig
35
src/trie.zig
@@ -15,10 +15,10 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
|
|
||||||
const Digest = Context.Digest; // as in Hash Code or Hash Digest
|
const Digest = Context.Digest; // as in Hash Code or Hash Digest
|
||||||
const table_size = @typeInfo(Digest).Int.bits;
|
const table_size = @typeInfo(Digest).Int.bits;
|
||||||
const t = @intCast(Log2Int(Digest), @typeInfo(Log2Int(Digest)).Int.bits);
|
const t: Log2Int(Digest) = @intCast(@typeInfo(Log2Int(Digest)).Int.bits);
|
||||||
|
|
||||||
free_list: FreeList,
|
free_list: FreeList,
|
||||||
root: []?*Node,
|
root: *[table_size]?*Node,
|
||||||
|
|
||||||
const Node = union(enum) { kv: Pair, table: Table };
|
const Node = union(enum) { kv: Pair, table: Table };
|
||||||
const Table = struct { map: Digest = 0, base: [*]Node };
|
const Table = struct { map: Digest = 0, base: [*]Node };
|
||||||
@@ -37,7 +37,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
pub fn deinit(self: *const FreeList.Node, allocator: Allocator, len: usize) void {
|
pub fn deinit(self: *const FreeList.Node, allocator: Allocator, len: usize) void {
|
||||||
switch (len) {
|
switch (len) {
|
||||||
0 => unreachable,
|
0 => unreachable,
|
||||||
1 => allocator.destroy(@ptrCast(*Self.Node, self.inner)),
|
1 => allocator.destroy(@as(*Self.Node, @ptrCast(self.inner))),
|
||||||
else => allocator.free(self.inner[0..len]),
|
else => allocator.free(self.inner[0..len]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
|
|
||||||
pub fn init(allocator: Allocator) !FreeList {
|
pub fn init(allocator: Allocator) !FreeList {
|
||||||
const list = try allocator.create([table_size]?FreeList.Node);
|
const list = try allocator.create([table_size]?FreeList.Node);
|
||||||
std.mem.set(?FreeList.Node, list, null);
|
@memset(list, null);
|
||||||
|
|
||||||
return .{ .list = list };
|
return .{ .list = list };
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn create(self: *FreeList, allocator: Allocator, comptime T: type) !*T {
|
pub fn create(self: *FreeList, allocator: Allocator, comptime T: type) !*T {
|
||||||
return @ptrCast(*T, try self.alloc(allocator, T, 1));
|
return @ptrCast(try self.alloc(allocator, T, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Free'd nodes aren't deallocated, but instead are tracked by a free list where they
|
/// Free'd nodes aren't deallocated, but instead are tracked by a free list where they
|
||||||
@@ -140,14 +140,14 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn destroy(self: *FreeList, allocator: Allocator, node: *Self.Node) !void {
|
pub fn destroy(self: *FreeList, allocator: Allocator, node: *Self.Node) !void {
|
||||||
self.free(allocator, @ptrCast([*]Self.Node, node)[0..1]);
|
self.free(allocator, @as([*]Self.Node, @ptrCast(node))[0..1]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) !Self {
|
pub fn init(allocator: Allocator) !Self {
|
||||||
// TODO: Add ability to have a larger root node (for quicker lookup times)
|
// TODO: Add ability to have a larger root node (for quicker lookup times)
|
||||||
const root = try allocator.alloc(?*Node, table_size);
|
const root = try allocator.create([table_size]?*Node);
|
||||||
std.mem.set(?*Node, root, null);
|
@memset(root, null);
|
||||||
|
|
||||||
return Self{ .root = root, .free_list = try FreeList.init(allocator) };
|
return Self{ .root = root, .free_list = try FreeList.init(allocator) };
|
||||||
}
|
}
|
||||||
@@ -181,10 +181,10 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
inline fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
||||||
const shift_amt = @intCast(Log2Int(Digest), table_size - offset);
|
const shift_amt: Log2Int(Digest) = @intCast(table_size - offset);
|
||||||
|
|
||||||
return @truncate(Log2Int(Digest), hash >> shift_amt);
|
return @truncate(hash >> shift_amt);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search(self: *Self, key: K) ?Pair {
|
pub fn search(self: *Self, key: K) ?Pair {
|
||||||
@@ -198,13 +198,12 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
switch (current.*) {
|
switch (current.*) {
|
||||||
.table => |table| {
|
.table => |table| {
|
||||||
const mask = @as(Digest, 1) << tableIdx(hash, hash_offset);
|
const mask = @as(Digest, 1) << tableIdx(hash, hash_offset);
|
||||||
|
if (table.map & mask == 0) return null; // empty table
|
||||||
|
|
||||||
if (table.map & mask != 0) {
|
const idx = @popCount(table.map & (mask - 1));
|
||||||
const idx = @popCount(table.map & (mask - 1));
|
current = &table.base[idx];
|
||||||
current = &table.base[idx];
|
|
||||||
|
|
||||||
hash_offset += t;
|
hash_offset += t;
|
||||||
} else return null; // hash table entry is empty
|
|
||||||
},
|
},
|
||||||
.kv => |pair| {
|
.kv => |pair| {
|
||||||
if (!Context.eql(pair.key, key)) return null;
|
if (!Context.eql(pair.key, key)) return null;
|
||||||
@@ -243,7 +242,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
|
|
||||||
var i: Log2Int(Digest) = 0;
|
var i: Log2Int(Digest) = 0;
|
||||||
for (0..table_size) |shift| {
|
for (0..table_size) |shift| {
|
||||||
const mask_loop = @as(Digest, 1) << @intCast(Log2Int(Digest), shift);
|
const mask_loop = @as(Digest, 1) << @as(Log2Int(Digest), @intCast(shift));
|
||||||
|
|
||||||
if (new_map & mask_loop != 0) {
|
if (new_map & mask_loop != 0) {
|
||||||
defer i += 1;
|
defer i += 1;
|
||||||
@@ -287,7 +286,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||||||
const copied_pair = try self.free_list.create(allocator, Node);
|
const copied_pair = try self.free_list.create(allocator, Node);
|
||||||
copied_pair.* = .{ .kv = prev_pair };
|
copied_pair.* = .{ .kv = prev_pair };
|
||||||
|
|
||||||
current.* = .{ .table = .{ .map = mask, .base = @ptrCast([*]Node, copied_pair) } };
|
current.* = .{ .table = .{ .map = mask, .base = @as([*]Node, @ptrCast(copied_pair)) } };
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user