Skip to content

Commit

Permalink
Implement a Open addressing (linear probing) hashmap, Implement a Cod…
Browse files Browse the repository at this point in the history
…egen for the JS driver
  • Loading branch information
devraymondsh committed Jan 18, 2024
1 parent 13eb25e commit c2e7317
Show file tree
Hide file tree
Showing 35 changed files with 838 additions and 586 deletions.
25 changes: 17 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"program": "node",
"request": "launch",
"name": "Benchmark debug",
// "program": "/home/raymond/.zvm/bin/zig",
"cwd": "${workspaceFolder}",
"name": "Debug Nodejs driver's tests",
"args": ["src/drivers/js/tests/kivi.js"]
},
{
"type": "node",
"program": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"args": ["bench/bench-with-builtin.js"],
// "args": ["src/drivers/js/tests/kivi.js"],
// "args": ["build", "test"],
"cwd": "${workspaceFolder}"
"name": "Debug Nodejs driver's benchmark"
},
{
"type": "lldb",
"program": "zig-out/bin/ffi-tests",
"request": "launch",
"cwd": "${workspaceFolder}",
"name": "Debug Core's FFI tests"
}
]
}
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"[cpp]": {
"editor.defaultFormatter": "ms-vscode.cpptools"
},
"[zig]": {
"editor.defaultFormatter": "ziglang.vscode-zig"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Expand All @@ -26,6 +29,7 @@
"math.h": "c",
"random": "c",
"*.tcc": "c",
"cmath": "c"
"cmath": "c",
"kivi.h": "c"
}
}
41 changes: 25 additions & 16 deletions bench/bench-with-builtin.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { Kivi } from "../src/drivers/js/index.js";
import { isBun, isDeno } from "../src/drivers/js/runtime.js";
import { Kivi, NodeKivi } from "../src/drivers/js/index.js";
import { generateFakeData } from "./faker/generate.js";
import { Buffer } from "node:buffer";

const isBun = () => "Bun" in globalThis;
const isDeno = () => "Deno" in globalThis;
// const isNodeJS = () => typeof global !== "undefined" && globalThis === global;

const repeatBenchmark = 2;
const data = generateFakeData();

const assert = (name, left, right) => {
if (
Buffer.from(left, "utf8").subarray(0, right.length).toString() !==
right.toString()
) {
throw new Error(
`Assertion '${name}' failed! Left was '${left.toString()}' and right was '${right.toString()}'.`
);
if (Buffer.isBuffer(left) && Buffer.isBuffer(right)) {
if (
left.subarray(0, right.length).toString("utf8") == right.toString("utf8")
) {
return;
}
} else {
if (left == right) {
return;
}
}

throw new Error(
`Assertion '${name}' failed! Left was '${left}' and right was '${right.toString()}'.`
);
};
const roundToTwoDecimal = (num) => +(Math.round(num + "e+2") + "e-2");

Expand Down Expand Up @@ -152,8 +162,7 @@ const builtinMapBenchmark = () => {
}
logResults(name, durationArr, average);
};
const kiviBenchmark = (config) => {
const name = "Kivi " + (config.forceUseRuntimeFFI ? "FFI" : "Napi");
const kiviBenchmark = () => {
const durationArr = [];
let average = {
insertionDuration: 0,
Expand All @@ -162,8 +171,8 @@ const kiviBenchmark = (config) => {
};
for (let i = 0; i < repeatBenchmark; i++) {
let o = {
name,
map: new Kivi(config),
name: "Kivi",
map: new NodeKivi(),
get: function (k) {
return this.map.get(k);
},
Expand Down Expand Up @@ -201,14 +210,14 @@ const kiviBenchmark = (config) => {
};
}
}
logResults(name, durationArr, average);
logResults("Kivi", durationArr, average);
};

builtinMapBenchmark();

kiviBenchmark({ forceUseRuntimeFFI: false });
kiviBenchmark();
if (isDeno() || isBun()) {
kiviBenchmark({ forceUseRuntimeFFI: true });
kiviBenchmark();
logRatio(0, 1);
logRatio(0, 2);
} else {
Expand Down
58 changes: 32 additions & 26 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ pub fn build(b: *std.Build) !void {
Dependency.addInternal(b, "Kivi", "src/core/Kivi.zig", 0, 1);
Dependency.addInternal(b, "core", "src/core/main.zig", 0, 2);

// Executes codegens
const codegen_step = b.step("codegen", "Generates bindings");
const core_codegen = b.addExecutable(.{
.name = "codegen_generate",
.root_source_file = .{ .path = "src/codegen/core.zig" },
.optimize = optimize,
.target = resolved_target,
});
const js_driver_codegen = b.addExecutable(.{
.name = "codegen_generate",
.root_source_file = .{ .path = "src/codegen/js_driver.zig" },
.optimize = optimize,
.target = resolved_target,
});
inline for (global_deps) |global_dep| {
core_codegen.root_module.addImport(global_dep.name, global_dep.module);
js_driver_codegen.root_module.addImport(global_dep.name, global_dep.module);
}
codegen_step.dependOn(&b.addRunArtifact(core_codegen).step);
codegen_step.dependOn(&b.addRunArtifact(js_driver_codegen).step);

// Compiles the core then generates static and shared libraries
const core_build_step = b.step("core", "Builds the core");
const core_targets = Libs.create(
Expand All @@ -158,6 +179,7 @@ pub fn build(b: *std.Build) !void {
"src/core/main.zig",
true,
);
core_build_step.dependOn(codegen_step);
core_build_step.dependOn(&b.addInstallArtifact(core_targets.shared, .{}).step);
core_build_step.dependOn(&b.addInstallArtifact(core_targets.static.?, .{}).step);

Expand All @@ -166,7 +188,7 @@ pub fn build(b: *std.Build) !void {
const js_driver_targets = Libs.create(
b,
"kivi-node-addon",
"src/drivers/js/nodejs/main.zig",
"src/drivers/js/runtimes/nodejs/main.zig",
false,
);
drivers_build_step.dependOn(&b.addInstallArtifact(js_driver_targets.shared, .{}).step);
Expand All @@ -176,38 +198,22 @@ pub fn build(b: *std.Build) !void {
.lib,
try std.fmt.allocPrint(allocator, "kivi-addon-{s}-{s}.node", .{ arch, tag }),
);
drivers_build_step.dependOn(codegen_step);
node_addon_install.step.dependOn(&js_driver_targets.shared.step);
drivers_build_step.dependOn(&node_addon_install.step);

// Executes codegens
const codegen_step = b.step("codegen", "Generates bindings");
const codegen = b.addExecutable(.{
.name = "codegen_generate",
.root_source_file = .{ .path = "src/codegen/core.zig" },
.optimize = optimize,
.target = resolved_target,
});
inline for (global_deps) |global_dep| {
codegen.root_module.addImport(global_dep.name, global_dep.module);
}
const codegen_run = b.addRunArtifact(codegen);
codegen_step.dependOn(&codegen_run.step);

// C FFI tests
var ffi_tests_step = b.step("test-ffi", "Runs FFI tests");
const ffi_tests = b.addExecutable(.{ .name = "ffi-shared", .target = resolved_target, .optimize = optimize });
const ffi_tests = b.addExecutable(.{ .name = "ffi-tests", .target = resolved_target, .optimize = optimize });
ffi_tests.linkLibC();
ffi_tests.linkLibrary(core_targets.shared);
ffi_tests.addSystemIncludePath(.{ .path = "src/core/include" });
ffi_tests.addCSourceFile(.{
.file = .{ .path = "src/core/tests/ffi.c" },
.flags = switch (optimize) {
// Asserts in ffi.c go away in unsafe build modes, so we need to disable errors on unused variables
.ReleaseFast, .ReleaseSmall => &.{ "-std=c17", "-pedantic", "-Wall", "-Werror", "-Wno-unused-variable" },
.ReleaseSafe, .Debug => &.{ "-std=c17", "-pedantic", "-Wall", "-Werror" },
},
.flags = &.{"-std=c17"},
});
ffi_tests.step.dependOn(codegen_step);
ffi_tests_step.dependOn(core_build_step);
ffi_tests_step.dependOn(&b.addInstallArtifact(ffi_tests, .{}).step);
ffi_tests_step.dependOn(&b.addRunArtifact(ffi_tests).step);

// Runs all tests
Expand All @@ -217,8 +223,8 @@ pub fn build(b: *std.Build) !void {
"src/drivers/js",
.{
"nodejs-test",
"deno-test",
"bun-test",
// "deno-test",
// "bun-test",
},
.{ core_build_step, drivers_build_step, ffi_tests_step },
test_step,
Expand All @@ -230,8 +236,8 @@ pub fn build(b: *std.Build) !void {
b,
"bench",
.{
"nodejs-bench",
"deno-bench",
// "nodejs-bench",
// "deno-bench",
"bun-bench",
},
.{ core_build_step, drivers_build_step },
Expand Down
12 changes: 12 additions & 0 deletions src/codegen/js_driver.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const std = @import("std");
const core_mod = @import("core");

pub fn main() !void {
const file = try std.fs.cwd().createFile("src/drivers/js/codegen-generated.js", .{});
var output = std.io.bufferedWriter(file.writer());
defer output.flush() catch {};

try output.writer().print("// This is generated by the codegen. Please don't touch this file.\n" ++
\\export const kiviInstanceSize = {any};
, .{@sizeOf(core_mod.Kivi)});
}
Loading

0 comments on commit c2e7317

Please sign in to comment.