fix group bugs
This commit is contained in:
parent
e76971101b
commit
df926f965c
@ -7,12 +7,21 @@ pub const EntityTraits = ecs.EntityTraitsType(.medium);
|
|||||||
pub const Velocity = struct { x: f32, y: f32 };
|
pub const Velocity = struct { x: f32, y: f32 };
|
||||||
pub const Position = struct { x: f32, y: f32 };
|
pub const Position = struct { x: f32, y: f32 };
|
||||||
|
|
||||||
/// logs the timing for views vs groups with 1,000,000 entities
|
/// logs the timing for views vs non-owning groups vs owning groups with 1,000,000 entities
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var reg = ecs.Registry.init(std.heap.c_allocator);
|
var reg = ecs.Registry.init(std.heap.c_allocator);
|
||||||
defer reg.deinit();
|
defer reg.deinit();
|
||||||
|
|
||||||
var timer = try std.time.Timer.start();
|
var timer = try std.time.Timer.start();
|
||||||
|
|
||||||
|
createEntities(®);
|
||||||
|
iterateView(®);
|
||||||
|
nonOwningGroup(®);
|
||||||
|
owningGroup(®);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn createEntities(reg: *ecs.Registry) void {
|
||||||
|
var timer = std.time.Timer.start() catch unreachable;
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < 1000000) : (i += 1) {
|
while (i < 1000000) : (i += 1) {
|
||||||
var e1 = reg.create();
|
var e1 = reg.create();
|
||||||
@ -20,11 +29,14 @@ pub fn main() !void {
|
|||||||
reg.add(e1, Velocity{ .x = 1, .y = 1 });
|
reg.add(e1, Velocity{ .x = 1, .y = 1 });
|
||||||
}
|
}
|
||||||
var end = timer.lap();
|
var end = timer.lap();
|
||||||
std.debug.warn("create: \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
|
std.debug.warn("create entities: \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iterateView(reg: *ecs.Registry) void {
|
||||||
|
std.debug.warn("--- multi-view ---\n", .{});
|
||||||
var view = reg.view(.{ Velocity, Position }, .{});
|
var view = reg.view(.{ Velocity, Position }, .{});
|
||||||
|
|
||||||
timer.reset();
|
var timer = std.time.Timer.start() catch unreachable;
|
||||||
var iter = view.iterator();
|
var iter = view.iterator();
|
||||||
while (iter.next()) |entity| {
|
while (iter.next()) |entity| {
|
||||||
var pos = view.get(Position, entity);
|
var pos = view.get(Position, entity);
|
||||||
@ -34,11 +46,36 @@ pub fn main() !void {
|
|||||||
pos.*.y += vel.y;
|
pos.*.y += vel.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
end = timer.lap();
|
var end = timer.lap();
|
||||||
std.debug.warn("view (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
|
std.debug.warn("view (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn nonOwningGroup(reg: *ecs.Registry) void {
|
||||||
|
std.debug.warn("--- non-owning ---\n", .{});
|
||||||
|
var timer = std.time.Timer.start() catch unreachable;
|
||||||
|
var group = reg.group(.{}, .{Velocity, Position}, .{});
|
||||||
|
var end = timer.lap();
|
||||||
|
std.debug.warn("group (create): {d}\n", .{@intToFloat(f64, end) / 1000000000});
|
||||||
|
|
||||||
|
timer.reset();
|
||||||
|
var group_iter = group.iterator();
|
||||||
|
while (group_iter.next()) |entity| {
|
||||||
|
var pos = group.get(Position, entity);
|
||||||
|
const vel = group.getConst(Velocity, entity);
|
||||||
|
|
||||||
|
pos.*.x += vel.x;
|
||||||
|
pos.*.y += vel.y;
|
||||||
|
}
|
||||||
|
|
||||||
var group = reg.group(.{ Velocity, Position }, .{}, .{});
|
|
||||||
end = timer.lap();
|
end = timer.lap();
|
||||||
|
std.debug.warn("group (iter): \t{d}\n", .{@intToFloat(f64, end) / 1000000000});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn owningGroup(reg: *ecs.Registry) void {
|
||||||
|
std.debug.warn("--- owning ---\n", .{});
|
||||||
|
var timer = std.time.Timer.start() catch unreachable;
|
||||||
|
var group = reg.group(.{ Velocity, Position }, .{}, .{});
|
||||||
|
var end = timer.lap();
|
||||||
std.debug.warn("group (create): {d}\n", .{@intToFloat(f64, end) / 1000000000});
|
std.debug.warn("group (create): {d}\n", .{@intToFloat(f64, end) / 1000000000});
|
||||||
|
|
||||||
timer.reset();
|
timer.reset();
|
||||||
|
@ -176,6 +176,7 @@ pub const Registry = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (overlapping > 0) return i;
|
if (overlapping > 0) return i;
|
||||||
|
if (i == 0) return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@ -615,6 +616,8 @@ pub const Registry = struct {
|
|||||||
/// expects a tuple of types. Convertes them to type names, sorts them then concatenates and returns the string.
|
/// expects a tuple of types. Convertes them to type names, sorts them then concatenates and returns the string.
|
||||||
inline fn concatTypes(comptime types: var) []const u8 {
|
inline fn concatTypes(comptime types: var) []const u8 {
|
||||||
comptime {
|
comptime {
|
||||||
|
if (types.len == 0) return "_";
|
||||||
|
|
||||||
const impl = struct {
|
const impl = struct {
|
||||||
fn asc(context: void, lhs: []const u8, rhs: []const u8) bool {
|
fn asc(context: void, lhs: []const u8, rhs: []const u8) bool {
|
||||||
return std.mem.lessThan(u8, lhs, rhs);
|
return std.mem.lessThan(u8, lhs, rhs);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user