From 01ff3962b5a2aac760aef7aa03288795c8be1e12 Mon Sep 17 00:00:00 2001 From: Mergul Date: Tue, 18 Jun 2019 20:12:18 +0200 Subject: [PATCH] -added SimpleVector (vector with only ubyte data) -components adding and removing now use SimpleVector (optimalization) --- source/ecs/manager.d | 6 ++-- source/ecs/simple_vector.d | 66 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 source/ecs/simple_vector.d diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 140d8f2..f2df314 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -20,6 +20,7 @@ import ecs.hash_map; import ecs.id_manager; import ecs.system; import ecs.vector; +import ecs.simple_vector; import ecs.events; import ecs.traits; @@ -1387,7 +1388,7 @@ export class EntityManager data.change_entities_list.add(0); data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. EntityID.sizeof]); data.change_entities_list.add((cast(ubyte*)&num)[0 .. uint.sizeof]); - data.change_entities_list.add(cast(ubyte[]) del_ids); + data.change_entities_list.add((cast(ubyte*)del_ids.ptr)[0 .. num * 2]); } private void __removeComponents(EntityID entity_id, ushort[] del_ids) @@ -2685,7 +2686,8 @@ export class EntityManager struct ThreadData { Vector!EntityID entities_to_remove; - Vector!ubyte change_entities_list; + //Vector!ubyte change_entities_list; + SimpleVector change_entities_list; Vector!(EntitiesBlock*) blocks_to_update; } diff --git a/source/ecs/simple_vector.d b/source/ecs/simple_vector.d new file mode 100644 index 0000000..1751040 --- /dev/null +++ b/source/ecs/simple_vector.d @@ -0,0 +1,66 @@ +module ecs.simple_vector; + +import std.experimental.allocator; +import std.experimental.allocator.mallocator; + +import core.stdc.string; + +struct SimpleVector +{ + + @disable this(this); + + void add(ubyte el) nothrow @nogc + { + while(used >= data.length) + { + if(data is null)data = Mallocator.instance.makeArray!ubyte(1024); + else Mallocator.instance.expandArray(data,data.length); + } + data[used++] = el; + } + + void add(ubyte[] el) nothrow @nogc + { + while(used + el.length >= data.length) + { + if(data is null)data = Mallocator.instance.makeArray!ubyte(1024); + else Mallocator.instance.expandArray(data,data.length); + } + memcpy(data.ptr + used, el.ptr, el.length); + used += el.length; + } + + size_t length() nothrow @nogc + { + return used; + } + + export ref ubyte opIndex(size_t pos) nothrow @nogc + { + return data[pos]; + } + + export ubyte[] opSlice() nothrow @nogc + { + return data[0 .. used]; + } + + export ubyte[] opSlice(size_t x, size_t y) nothrow @nogc + { + return data[x .. y]; + } + + export size_t opDollar() nothrow @nogc + { + return used; + } + + void clear() nothrow @nogc + { + used = 0; + } + + ubyte[] data = null; + size_t used = 0; +} \ No newline at end of file