Compare commits

..

No commits in common. "8e89cc5f4f0f4d4d4ed82f21ea5d37f04bae1083" and "7bdcbfd6f51cb1bcaee4fbc2b5e84099e46fbc0a" have entirely different histories.

3 changed files with 15 additions and 12 deletions

View File

@ -37,7 +37,6 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("src/bench.zig"), .root_source_file = b.path("src/bench.zig"),
.target = target, .target = target,
.optimize = .ReleaseFast, .optimize = .ReleaseFast,
.link_libc = true,
}); });
bench.root_module.addImport("hamt", mod); bench.root_module.addImport("hamt", mod);

View File

@ -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 = u32; pub const Digest = u64;
pub fn hash(input: []const u8) Digest { pub fn hash(input: []const u8) Digest {
return @truncate(std.hash.Wyhash.hash(0, input)); return 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,13 +17,16 @@ 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 {
const allocator = std.heap.c_allocator; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
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(0); var rand = std.rand.DefaultPrng.init(1337);
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);

View File

@ -18,7 +18,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
const t: Log2Int(Digest) = @intCast(@typeInfo(Log2Int(Digest)).Int.bits); const t: Log2Int(Digest) = @intCast(@typeInfo(Log2Int(Digest)).Int.bits);
free_list: FreeList, free_list: FreeList,
root: *[table_size]?*Node, root: []?*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 };
@ -146,7 +146,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
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.create([table_size]?*Node); const root = try allocator.alloc(?*Node, table_size);
@memset(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,7 +181,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
} }
} }
inline fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) { fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
const shift_amt: Log2Int(Digest) = @intCast(table_size - offset); const shift_amt: Log2Int(Digest) = @intCast(table_size - offset);
return @truncate(hash >> shift_amt); return @truncate(hash >> shift_amt);
@ -198,12 +198,13 @@ 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
const idx = @popCount(table.map & (mask - 1)); if (table.map & mask != 0) {
current = &table.base[idx]; const idx = @popCount(table.map & (mask - 1));
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;