Skip to content

Commit

Permalink
Swtich to swift_lib
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Feb 6, 2024
1 parent 23f309d commit 32c6733
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 390 deletions.
11 changes: 8 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var target: std.Target = undefined;
var optimize: std.builtin.OptimizeMode = undefined;
var resolved_target: std.Build.ResolvedTarget = undefined;

var global_deps: [2]Dependency = undefined;
var global_deps: [3]Dependency = undefined;
const Dependency = struct {
name: []const u8,
module: *std.Build.Module,
Expand All @@ -29,11 +29,15 @@ const Dependency = struct {
comptime name: []const u8,
comptime source: []const u8,
comptime n: comptime_int,
comptime link_module_to: ?comptime_int,
) void {
global_deps[n] = .{
.name = name,
.module = b.createModule(.{ .root_source_file = .{ .path = source } }),
};
if (link_module_to) |link_module| {
global_deps[n].module.addImport(global_deps[link_module].name, global_deps[link_module].module);
}
}
};

Expand Down Expand Up @@ -142,8 +146,9 @@ pub fn build(b: *std.Build) !void {
const tag = @tagName(target.os.tag);

// Declares dependencies
Dependency.addInternal(b, "Kivi", "src/core/Kivi.zig", 0);
Dependency.addInternal(b, "core", "src/core/main.zig", 1);
Dependency.addExternal(b, "swift_lib", 0);
Dependency.addInternal(b, "Kivi", "src/core/Kivi.zig", 1, 0);
Dependency.addInternal(b, "core", "src/core/main.zig", 2, 0);

// Executes codegens
const codegen_step = b.step("codegen", "Generates bindings");
Expand Down
7 changes: 6 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
.version = "0.0.0",
.minimum_zig_version = "0.11.0",
.paths = .{ "src", "build.zig", "build.zig.zon" },
.dependencies = .{},
.dependencies = .{
.swift_lib = .{
.url = "https://github.com/devraymondsh/swift_lib/archive/refs/tags/v0.2.1.tar.gz",
.hash = "1220c061e19c7a2315d009adc63c44657fe47f043aaa4cae3cade745095de4cf0488",
},
},
}
13 changes: 6 additions & 7 deletions src/core/ByteMap.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/// This is Byte(u8) hashmap implementation that relies on the caller to handle allocations and lifetimes.
const builtin = @import("builtin");
const Wyhash = @import("./Wyhash.zig");
const Math = @import("Math.zig");
const Mmap = @import("./Mmap.zig");
const swift_lib = @import("swift_lib");

const Entry = struct {
key: []u8,
Expand Down Expand Up @@ -71,8 +70,8 @@ table_size: usize,

var hasher = Wyhash.init(0);

pub fn init(self: *ByteMap, allocator: *Mmap, size: usize) !void {
self.table_size = @intCast(Math.ceilPowerOfTwo(@intCast(size)));
pub fn init(self: *ByteMap, allocator: swift_lib.heap.Allocator, size: usize) !void {
self.table_size = swift_lib.math.ceilPowerOfTwo(size);
self.table_metadata = try allocator.alloc(GroupMetadata, self.table_size);
self.table = try allocator.alloc(Group, self.table_size);

Expand All @@ -87,7 +86,7 @@ fn hash(key: []const u8) usize {
return hasher.reset_hash(@intCast(key.len), key);
}
fn hash_to_groupidx(self: *ByteMap, hashed: usize) usize {
return (hashed >> 7) & (self.table_size - 1);
return (hashed >> 7) % self.table_size;
}
fn hash_to_elemidx(hashed: usize) usize {
return (hashed & 0x7F) % 7;
Expand All @@ -104,7 +103,7 @@ fn find_index(self: *ByteMap, key: []const u8, comptime lookup: bool) ?FoundEnti
const elemidx = hash_to_elemidx(hashed);

var searching_second_time = false;
while (true) : (groupidx += 1) {
while (true) : (groupidx = (groupidx + 1) % self.table_size) {
const group = &self.table[groupidx];
const metadata = self.table_metadata[groupidx];
const metadata_vec: @Vector(16, u8) = metadata.elements;
Expand Down Expand Up @@ -184,7 +183,7 @@ pub fn get(self: *ByteMap, key: []const u8) ?[]u8 {
}
return null;
}
pub fn del(self: *ByteMap, allocator: *Mmap, key: []const u8) ?[]u8 {
pub fn del(self: *ByteMap, allocator: swift_lib.heap.Allocator, key: []const u8) ?[]u8 {
const found_entity = self.find_index(key, true);
if (found_entity) |entity| {
const entry = &self.table[entity.group_idx].elements[entity.elem_idx];
Expand Down
26 changes: 16 additions & 10 deletions src/core/Kivi.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const MMap = @import("Mmap.zig");
const ByteMap = @import("ByteMap.zig");
const swift_lib = @import("swift_lib");

pub const Config = extern struct {
// (2 ** 18) * 16 = 4194304
group_size: usize = 262144,
mem_size: usize = 1 * 1024 * 1024 * 1024,
};

mem: MMap,
map: ByteMap,
allocator: swift_lib.heap.Allocator,
freelist: swift_lib.heap.FreelistAllocator,

const Kivi = @This();

Expand All @@ -20,18 +21,21 @@ fn stringcpy(dest: []u8, src: []const u8) !void {
}

pub fn init(self: *Kivi, config: *const Config) !usize {
self.mem = try MMap.init(config.mem_size);
const pages = try swift_lib.heap.PageAllocator.init(config.mem_size / swift_lib.os.page_size);

try self.map.init(&self.mem, config.group_size);
self.freelist = swift_lib.heap.FreelistAllocator.init(pages.mem);
self.allocator = self.freelist.allocator();

try self.map.init(self.allocator, config.group_size);

return @sizeOf(Kivi);
}

pub fn reserve_key(self: *Kivi, size: usize) ![]u8 {
return self.mem.alloc(u8, size);
return self.allocator.alloc(u8, size);
}
pub fn reserve_value(self: *Kivi, size: usize) ![]u8 {
return try self.mem.alloc(u8, size);
return try self.allocator.alloc(u8, size);
}
pub fn put_entry(self: *Kivi, key: []u8, value: []u8) !void {
return self.map.put(key, value);
Expand Down Expand Up @@ -65,7 +69,7 @@ pub fn get_copy(self: *Kivi, key: []const u8, value: ?[]u8) !usize {
}

pub fn del(self: *Kivi, key: []const u8) ![]u8 {
if (self.map.del(&self.mem, key)) |value| {
if (self.map.del(self.allocator, key)) |value| {
return value;
} else {
return error.NotFound;
Expand All @@ -79,17 +83,19 @@ pub fn del_copy(self: *Kivi, key: []const u8, value: ?[]u8) !usize {
try stringcpy(value.?, value_slice);
}

self.mem.free(value_slice);
self.allocator.free(value_slice);

return value_slice_len;
}

pub fn rm(self: *Kivi, key: []const u8) !void {
const value_slice = try self.del(key);
self.mem.free(value_slice);
self.allocator.free(value_slice);
}

pub fn deinit(self: *Kivi) void {
var pages: swift_lib.heap.PageAllocator = .{ .mem = @alignCast(self.freelist.mem) };

self.map.deinit();
self.mem.deinit();
pages.deinit();
}
32 changes: 0 additions & 32 deletions src/core/Math.zig

This file was deleted.

110 changes: 0 additions & 110 deletions src/core/Mmap.zig

This file was deleted.

Loading

0 comments on commit 32c6733

Please sign in to comment.