From b900dfde2d332611ac16175b3df9065144dfbc34 Mon Sep 17 00:00:00 2001 From: RUSshy <18348637+RUSshy@users.noreply.github.com> Date: Sat, 17 Jul 2021 14:04:10 +0200 Subject: [PATCH 1/4] Update handles.zig --- zig-ecs/src/ecs/handles.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zig-ecs/src/ecs/handles.zig b/zig-ecs/src/ecs/handles.zig index e444fa3..c4d9f5a 100644 --- a/zig-ecs/src/ecs/handles.zig +++ b/zig-ecs/src/ecs/handles.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const registry = @import("registry.zig"); /// generates versioned "handles" (https://floooh.github.io/2018/06/17/handles-vs-pointers.html) /// you choose the type of the handle (aka its size) and how much of that goes to the index and the version. @@ -38,7 +39,6 @@ pub fn Handles(comptime HandleType: type, comptime IndexType: type, comptime Ver return h; } } - return null; } }; @@ -59,15 +59,15 @@ pub fn Handles(comptime HandleType: type, comptime IndexType: type, comptime Ver } pub fn extractId(_: Self, handle: HandleType) IndexType { - return @truncate(IndexType, handle); + return @truncate(IndexType, handle & @as(IndexType, registry.entity_traits.entity_mask)); } pub fn extractVersion(_: Self, handle: HandleType) VersionType { - return @truncate(VersionType, handle >> @bitSizeOf(IndexType)); + return @truncate(VersionType, handle >> registry.entity_traits.entity_shift); } fn forge(id: IndexType, version: VersionType) HandleType { - return id | @as(HandleType, version) << @bitSizeOf(IndexType); + return id | @as(HandleType, version) << registry.entity_traits.entity_shift; } pub fn create(self: *Self) HandleType { From e488cc384f2523aa7a7a72541be08c2eba1a0aa4 Mon Sep 17 00:00:00 2001 From: RUSshy <18348637+RUSshy@users.noreply.github.com> Date: Sat, 17 Jul 2021 14:05:30 +0200 Subject: [PATCH 2/4] Update sparse_set.zig --- zig-ecs/src/ecs/sparse_set.zig | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/zig-ecs/src/ecs/sparse_set.zig b/zig-ecs/src/ecs/sparse_set.zig index 4338dd5..3d2e3f9 100644 --- a/zig-ecs/src/ecs/sparse_set.zig +++ b/zig-ecs/src/ecs/sparse_set.zig @@ -1,14 +1,14 @@ const std = @import("std"); const warn = std.debug.warn; const utils = @import("utils.zig"); +const registry = @import("registry.zig"); const ReverseSliceIterator = @import("utils.zig").ReverseSliceIterator; // TODO: fix entity_mask. it should come from EntityTraitsDefinition. pub fn SparseSet(comptime SparseT: type) type { return struct { const Self = @This(); - const page_size: usize = 32768; - const entity_per_page = page_size / @sizeOf(SparseT); + const page_size: usize = 4096; sparse: std.ArrayList(?[]SparseT), dense: std.ArrayList(SparseT), @@ -17,9 +17,9 @@ pub fn SparseSet(comptime SparseT: type) type { pub fn initPtr(allocator: *std.mem.Allocator) *Self { var set = allocator.create(Self) catch unreachable; - set.sparse = std.ArrayList(?[]SparseT).init(allocator); - set.dense = std.ArrayList(SparseT).init(allocator); - set.entity_mask = std.math.maxInt(SparseT); + set.sparse = std.ArrayList(?[]SparseT).initCapacity(allocator, 16) catch unreachable; + set.dense = std.ArrayList(SparseT).initCapacity(allocator, 16) catch unreachable; + set.entity_mask = registry.entity_traits.entity_mask; set.allocator = allocator; return set; } @@ -28,7 +28,7 @@ pub fn SparseSet(comptime SparseT: type) type { return Self{ .sparse = std.ArrayList(?[]SparseT).init(allocator), .dense = std.ArrayList(SparseT).init(allocator), - .entity_mask = std.math.maxInt(SparseT), + .entity_mask = registry.entity_traits.entity_mask, .allocator = null, }; } @@ -50,11 +50,11 @@ pub fn SparseSet(comptime SparseT: type) type { } pub fn page(self: Self, sparse: SparseT) usize { - return (sparse & self.entity_mask) / entity_per_page; + return (sparse & self.entity_mask) / page_size; } fn offset(_: Self, sparse: SparseT) usize { - return sparse & (entity_per_page - 1); + return sparse & (page_size - 1); } fn assure(self: *Self, pos: usize) []SparseT { @@ -65,14 +65,12 @@ pub fn SparseSet(comptime SparseT: type) type { std.mem.set(?[]SparseT, self.sparse.items[start_pos..], null); } - if (self.sparse.items[pos]) |arr| { - return arr; + if (self.sparse.items[pos] == null) { + var new_page = self.sparse.allocator.alloc(SparseT, page_size) catch unreachable; + std.mem.set(SparseT, new_page, std.math.maxInt(SparseT)); + self.sparse.items[pos] = new_page; } - var new_page = self.sparse.allocator.alloc(SparseT, entity_per_page) catch unreachable; - std.mem.set(SparseT, new_page, std.math.maxInt(SparseT)); - self.sparse.items[pos] = new_page; - return self.sparse.items[pos].?; } @@ -105,7 +103,9 @@ pub fn SparseSet(comptime SparseT: type) type { pub fn contains(self: Self, sparse: SparseT) bool { const curr = self.page(sparse); - return curr < self.sparse.items.len and self.sparse.items[curr] != null and self.sparse.items[curr].?[self.offset(sparse)] != std.math.maxInt(SparseT); + return curr < self.sparse.items.len and + self.sparse.items[curr] != null and + self.sparse.items[curr].?[self.offset(sparse)] != std.math.maxInt(SparseT); } /// Returns the position of an entity in a sparse set From 22b4fe2e028fdee3021691861f21812a26bb0b33 Mon Sep 17 00:00:00 2001 From: RUSshy <18348637+RUSshy@users.noreply.github.com> Date: Sat, 17 Jul 2021 14:07:33 +0200 Subject: [PATCH 3/4] Update handles.zig --- zig-ecs/src/ecs/handles.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zig-ecs/src/ecs/handles.zig b/zig-ecs/src/ecs/handles.zig index c4d9f5a..67d1609 100644 --- a/zig-ecs/src/ecs/handles.zig +++ b/zig-ecs/src/ecs/handles.zig @@ -59,7 +59,7 @@ pub fn Handles(comptime HandleType: type, comptime IndexType: type, comptime Ver } pub fn extractId(_: Self, handle: HandleType) IndexType { - return @truncate(IndexType, handle & @as(IndexType, registry.entity_traits.entity_mask)); + return @truncate(IndexType, handle & registry.entity_traits.entity_mask); } pub fn extractVersion(_: Self, handle: HandleType) VersionType { From 15d0e6d00d60d71de44dfed0e48ac1a547c5a2da Mon Sep 17 00:00:00 2001 From: RUSshy <18348637+RUSshy@users.noreply.github.com> Date: Sat, 17 Jul 2021 14:08:19 +0200 Subject: [PATCH 4/4] Update registry.zig --- zig-ecs/src/ecs/registry.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zig-ecs/src/ecs/registry.zig b/zig-ecs/src/ecs/registry.zig index 6ebab46..5a64402 100644 --- a/zig-ecs/src/ecs/registry.zig +++ b/zig-ecs/src/ecs/registry.zig @@ -10,7 +10,7 @@ const TypeStore = @import("type_store.zig").TypeStore; // allow overriding EntityTraits by setting in root via: EntityTraits = EntityTraitsType(.medium); const root = @import("root"); -const entity_traits = if (@hasDecl(root, "EntityTraits")) root.EntityTraits.init() else @import("entity.zig").EntityTraits.init(); +pub const entity_traits = if (@hasDecl(root, "EntityTraits")) root.EntityTraits.init() else @import("entity.zig").EntityTraits.init(); // setup the Handles type based on the type set in EntityTraits const EntityHandles = Handles(entity_traits.entity_type, entity_traits.index_type, entity_traits.version_type); @@ -261,7 +261,7 @@ pub const Registry = struct { /// Returns the version stored along with an entity identifier pub fn version(_: *Registry, entity: Entity) entity_traits.version_type { - return @truncate(entity_traits.version_type, entity >> @bitSizeOf(entity_traits.index_type)); + return @truncate(entity_traits.version_type, entity >> entity_traits.entity_shift); } /// Creates a new entity and returns it