-EntitesBlock freeing

-IDManager now increment counter on ID releasing (previously while creating new ID)
This commit is contained in:
Mergul 2018-09-20 20:33:55 +02:00
parent 45110e236c
commit 8cb5d6ce62
3 changed files with 44 additions and 13 deletions

View file

@ -11,7 +11,7 @@ struct IDManager
m_ids_array.add(Data()); m_ids_array.add(Data());
EntityID id; EntityID id;
id.id = m_next_id; id.id = m_next_id;
id.counter = ++m_ids_array[m_next_id].counter; id.counter = /*++*/m_ids_array[m_next_id].counter;
m_next_id = m_ids_array[m_next_id].next_id; m_next_id = m_ids_array[m_next_id].next_id;
if (m_next_id == uint.max) if (m_next_id == uint.max)
m_next_id = cast(uint) m_ids_array.length; m_next_id = cast(uint) m_ids_array.length;
@ -23,6 +23,7 @@ struct IDManager
Data* data = &m_ids_array[id.id]; Data* data = &m_ids_array[id.id];
if (data.counter != id.counter) if (data.counter != id.counter)
return; return;
data.counter++;
data.next_id = m_next_id; data.next_id = m_next_id;
data.entity = null; data.entity = null;
m_next_id = id.id; m_next_id = id.id;

View file

@ -118,10 +118,10 @@ class EntityManager
foreach (i; 1 .. (Parameters!(Sys.update)).length) foreach (i; 1 .. (Parameters!(Sys.update)).length)
{ {
ret ~= " ret ~= "
static if(isPointer!(types[" ~ i.to!string static if(isPointer!(types[" ~ i.to!string ~ "]))
~ "]))
{ {
comp = components_map.get(PointerTarget!(types[" ~ i.to!string ~ "]).stringof, ushort.max);\n comp = components_map.get(PointerTarget!(types["
~ i.to!string ~ "]).stringof, ushort.max);\n
if(comp == ushort.max)assert(0,\"Can't register system \\\"" ~ Sys.stringof if(comp == ushort.max)assert(0,\"Can't register system \\\"" ~ Sys.stringof
~ "\\\" due to non existing component \\\"\"~types[" ~ i.to!string ~ "].stringof~\"\\\".\"); ~ "\\\" due to non existing component \\\"\"~types[" ~ i.to!string ~ "].stringof~\"\\\".\");
system.m_optional_components[opt++] = comp; system.m_optional_components[opt++] = comp;
@ -220,7 +220,7 @@ class EntityManager
{ {
mixin(genCompList()); mixin(genCompList());
} }
ushort sys_id = systems_map.get(Sys.stringof, ushort.max); ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
if (sys_id < systems.length) if (sys_id < systems.length)
{ {
@ -257,7 +257,7 @@ class EntityManager
Sys* getSystem(Sys)() Sys* getSystem(Sys)()
{ {
return cast(Sys*)systems[Sys.system_id].system_pointer; return cast(Sys*) systems[Sys.system_id].system_pointer;
} }
void registerComponent(Comp)() void registerComponent(Comp)()
@ -513,6 +513,7 @@ class EntityManager
private void __removeComponents(EntityID entity_id, ushort[] del_ids) private void __removeComponents(EntityID entity_id, ushort[] del_ids)
{ {
Entity* entity = id_manager.getEntityPointer(entity_id); Entity* entity = id_manager.getEntityPointer(entity_id);
if(!entity)return;
EntitiesBlock* block = getMetaData(entity); EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_data; EntityInfo* info = block.type_data;
@ -583,6 +584,7 @@ class EntityManager
{ {
uint num = cast(uint) new_ids.length; uint num = cast(uint) new_ids.length;
Entity* entity = id_manager.getEntityPointer(entity_id); Entity* entity = id_manager.getEntityPointer(entity_id);
if(!entity)return;
EntitiesBlock* block = getMetaData(entity); EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_data; EntityInfo* info = block.type_data;
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0 ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0
@ -774,6 +776,7 @@ class EntityManager
else else
{ {
previous_block.next_block = block; previous_block.next_block = block;
block.prev_block = previous_block;
block.id = previous_block.id + 1; block.id = previous_block.id + 1;
} }
info.first_with_free_space = block; info.first_with_free_space = block;
@ -837,6 +840,28 @@ class EntityManager
} }
} }
if (block.entities_count == 0)
{
if (block.type_data.first_block is block)
{
block.type_data.first_block = block.next_block;
}
if (block.type_data.first_with_free_space is block)
{
block.type_data.first_with_free_space = block.next_block;//block.type_data.first_block;
}
if (block.prev_block)
{
block.prev_block.next_block = block.next_block;
}
if(block.next_block)
{
block.next_block.prev_block = block.prev_block;
}
allocator.freeBlock(block);
return;
}
if (pos == block.entities_count) if (pos == block.entities_count)
return; return;
@ -922,8 +947,8 @@ class EntityManager
export void begin() export void begin()
{ {
updateBlocks(); updateBlocks();
changeEntites();
removeEntities(); removeEntities();
changeEntites();
foreach (ref system; instance.systems) foreach (ref system; instance.systems)
{ {
if (system.m_begin) if (system.m_begin)
@ -939,8 +964,8 @@ class EntityManager
system.m_end(system.m_system_pointer); system.m_end(system.m_system_pointer);
} }
updateBlocks(); updateBlocks();
changeEntites();
removeEntities(); removeEntities();
changeEntites();
clearEvents(); clearEvents();
} }
@ -1003,15 +1028,17 @@ class EntityManager
} }
///pointer to Entity type info ///pointer to Entity type info
EntityInfo* type_data; EntityInfo* type_data = null;
///number of entities in block ///number of entities in block
ushort entities_count; ushort entities_count = 0;
///number of new entities in block ///number of new entities in block
ushort added_count; ushort added_count = 0;
///block id ///block id
uint id; uint id = 0;
///pointer to next block/page ///pointer to next block/page
EntitiesBlock* next_block; EntitiesBlock* next_block = null;
///pointer to next block/page
EntitiesBlock* prev_block = null;
//there is a loooot of data (4kB, pure magic) //there is a loooot of data (4kB, pure magic)
} }

View file

@ -93,6 +93,7 @@ int main()
struct TestSystem struct TestSystem
{ {
__gshared ushort system_id;
void onCreate() void onCreate()
{ {
@ -152,6 +153,7 @@ int main()
struct TestSystemWithHighPriority struct TestSystemWithHighPriority
{ {
__gshared ushort system_id;
void initialize(ref Entity entity, ref TestComp comp) void initialize(ref Entity entity, ref TestComp comp)
{ {
@ -173,6 +175,7 @@ int main()
struct TestSystem2 struct TestSystem2
{ {
__gshared ushort system_id;
void onEnable() void onEnable()
{ {