Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active April 11, 2021 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/3c650fe030e91c2edabb684a2e9ebf85 to your computer and use it in GitHub Desktop.
Save creationix/3c650fe030e91c2edabb684a2e9ebf85 to your computer and use it in GitHub Desktop.
const std = @import("std");
fn AutoMap(comptime BLOCK_POWER: comptime_int) type {
return struct {
const BLOCK_SIZE = 2 << (BLOCK_POWER - 1);
const HASH_SIZE = 2 << (HASH_POWER - 1);
const BRANCH_POWER = std.math.log2(@sizeOf(Leaf) / @sizeOf(*Node));
const BRANCH_FACTOR = 2 << (BRANCH_POWER - 1);
const Hash = u256;
const Block = [BLOCK_SIZE]u8;
const Leaf = struct { hash: Hash, block: Block };
const Branch = [BRANCH_FACTOR]?*Node;
const Node = union(enum) { branch: Branch, leaf: Leaf };
};
}
test "Proper branch factor" {
// 4Kib block
const M4K = AutoMap(12);
std.debug.print("\n", .{});
std.debug.print("@sizeOf(M4K.Hash) = {}\n", .{@sizeOf(M4K.Hash)});
std.debug.print("@sizeOf(M4K.Block) = {}\n", .{@sizeOf(M4K.Block)});
std.debug.print("@sizeOf(M4K.Leaf) = {}\n", .{@sizeOf(M4K.Leaf)});
std.debug.print("@sizeOf(?*M4K.Node) = {}\n", .{@sizeOf(?*M4K.Node)});
std.debug.print("M4K.BRANCH_POWER = {}\n", .{M4K.BRANCH_POWER});
std.debug.print("M4K.BRANCH_FACTOR = {}\n", .{M4K.BRANCH_FACTOR});
// 32Kib block
const M32K = AutoMap(15);
std.debug.print("\n", .{});
std.debug.print("@sizeOf(M32K.Hash) = {}\n", .{@sizeOf(M32K.Hash)});
std.debug.print("@sizeOf(M32K.Block) = {}\n", .{@sizeOf(M32K.Block)});
std.debug.print("@sizeOf(M32K.Leaf) = {}\n", .{@sizeOf(M32K.Leaf)});
std.debug.print("@sizeOf(?*M32K.Node) = {}\n", .{@sizeOf(?*M32K.Node)});
std.debug.print("M32K.BRANCH_POWER = {}\n", .{M32K.BRANCH_POWER});
std.debug.print("M32K.BRANCH_FACTOR = {}\n", .{M32K.BRANCH_FACTOR});
}
// pub fn main() anyerror!void {
// std.log.info("All your codebase are belong to us.", .{});
// }
@creationix
Copy link
Author

PS C:\Users\tim\xlr8.rust> zig test src/main.zig -target i386-windows-msvc 
Test [1/1] test "Proper branch factor"...
@sizeOf(M4K.Hash) = 32
@sizeOf(M4K.Block) = 4096
@sizeOf(M4K.Leaf) = 4128
@sizeOf(?*M4K.Node) = 4
M4K.BRANCH_POWER = 10
M4K.BRANCH_FACTOR = 1024

@sizeOf(M32K.Hash) = 32
@sizeOf(M32K.Block) = 32768
@sizeOf(M32K.Leaf) = 32800
@sizeOf(?*M32K.Node) = 4
M32K.BRANCH_POWER = 13
M32K.BRANCH_FACTOR = 8192
All 1 tests passed.
PS C:\Users\tim\xlr8.rust> zig test src/main.zig -target x86_64-windows-msvc
Test [1/1] test "Proper branch factor"... 
@sizeOf(M4K.Hash) = 32
@sizeOf(M4K.Block) = 4096
@sizeOf(M4K.Leaf) = 4128
@sizeOf(?*M4K.Node) = 8
M4K.BRANCH_POWER = 9
M4K.BRANCH_FACTOR = 512

@sizeOf(M32K.Hash) = 32
@sizeOf(M32K.Block) = 32768
@sizeOf(M32K.Leaf) = 32800
@sizeOf(?*M32K.Node) = 8
M32K.BRANCH_POWER = 12
M32K.BRANCH_FACTOR = 4096
All 1 tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment