From 54bcda58918cbefa6c2028a89bb746cc0ae9518a Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 26 Feb 2024 07:53:16 +0800 Subject: [PATCH] optimize DelEntBuffer --- src/EcsWorld.cs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 4a45c33..585c74b 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -407,11 +407,30 @@ namespace DCFApixels.DragonECS count = Math.Min(count, _delEntBufferCount); count = Math.Max(count, 0); _delEntBufferCount -= count; - ReadOnlySpan buffer = new ReadOnlySpan(_delEntBuffer, _delEntBufferCount, count); - for (int i = 0; i < _poolsCount; i++) + int slisedCount = count; + + for (int i = 0; i < slisedCount; i++) { - _pools[i].OnReleaseDelEntityBuffer(buffer); + int e = _delEntBuffer[i]; + if (_componentCounts[e] <= 0) + { + int tmp = _delEntBuffer[i]; + _delEntBuffer[i] = _delEntBuffer[--slisedCount]; + _delEntBuffer[slisedCount] = tmp; + i--; + } } + + ReadOnlySpan buffer = new ReadOnlySpan(_delEntBuffer, _delEntBufferCount, count); + if (slisedCount > 0) + { + ReadOnlySpan bufferSlised = new ReadOnlySpan(_delEntBuffer, _delEntBufferCount, slisedCount); + for (int i = 0; i < _poolsCount; i++) + { + _pools[i].OnReleaseDelEntityBuffer(bufferSlised); + } + } + _listeners.InvokeOnReleaseDelEntityBuffer(buffer); for (int i = 0; i < buffer.Length; i++) {