Compare commits
3 Commits
7bdcbfd6f5
...
8e89cc5f4f
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 8e89cc5f4f | |
Rekai Nyangadzayi Musuka | 052dd57cdd | |
Rekai Nyangadzayi Musuka | 3e82fde54a |
|
@ -37,6 +37,7 @@ pub fn build(b: *std.Build) void {
|
|||
.root_source_file = b.path("src/bench.zig"),
|
||||
.target = target,
|
||||
.optimize = .ReleaseFast,
|
||||
.link_libc = true,
|
||||
});
|
||||
bench.root_module.addImport("hamt", mod);
|
||||
|
||||
|
|
|
@ -2,10 +2,10 @@ const std = @import("std");
|
|||
const HashArrayMappedTrie = @import("hamt").HashArrayMappedTrie;
|
||||
|
||||
const StringContext = struct {
|
||||
pub const Digest = u64;
|
||||
pub const Digest = u32;
|
||||
|
||||
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 {
|
||||
|
@ -17,16 +17,13 @@ const StringArrayHashMap = std.array_hash_map.StringArrayHashMap(void);
|
|||
const StringHashMap = std.hash_map.StringHashMap(void);
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer std.debug.assert(gpa.deinit() == .ok);
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
const allocator = std.heap.c_allocator;
|
||||
const elem_count = 1000;
|
||||
|
||||
const keys = try allocator.alloc([32]u8, elem_count);
|
||||
defer allocator.free(keys);
|
||||
|
||||
var rand = std.rand.DefaultPrng.init(1337);
|
||||
var rand = std.rand.DefaultPrng.init(0);
|
||||
for (keys) |*key| rand.fill(key);
|
||||
|
||||
var trie = try HashArrayMappedTrie([]const u8, void, StringContext).init(allocator);
|
||||
|
|
|
@ -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);
|
||||
|
||||
free_list: FreeList,
|
||||
root: []?*Node,
|
||||
root: *[table_size]?*Node,
|
||||
|
||||
const Node = union(enum) { kv: Pair, table: Table };
|
||||
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 {
|
||||
// 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);
|
||||
@memset(root, null);
|
||||
|
||||
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:
|
|||
}
|
||||
}
|
||||
|
||||
fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
||||
inline fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
||||
const shift_amt: Log2Int(Digest) = @intCast(table_size - offset);
|
||||
|
||||
return @truncate(hash >> shift_amt);
|
||||
|
@ -198,13 +198,12 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
|||
switch (current.*) {
|
||||
.table => |table| {
|
||||
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));
|
||||
current = &table.base[idx];
|
||||
|
||||
hash_offset += t;
|
||||
} else return null; // hash table entry is empty
|
||||
},
|
||||
.kv => |pair| {
|
||||
if (!Context.eql(pair.key, key)) return null;
|
||||
|
|
Loading…
Reference in New Issue