From ba3ce72029bbebecb3bb1e368f275f75d9895237 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 8 Jul 2024 11:08:04 +0800 Subject: [PATCH] GetProcesses optimization --- src/EcsPipeline.cs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index d666107..fd28f47 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -99,11 +99,38 @@ namespace DCFApixels.DragonECS } else { - result = _allSystems.OfType().ToArray(); + result = CreateProcess(); _processes.Add(type, result); } return new EcsProcess(result); } + [ThreadStatic] + private static IEcsProcess[] _buffer; + private T[] CreateProcess() where T : IEcsProcess + { + if(_buffer == null || _buffer.Length < _allSystems.Length) + { + if(_buffer == null) + { + _buffer = new IEcsProcess[_allSystems.Length]; + } + else + { + Array.Resize(ref _buffer, _allSystems.Length); + } + } + int l = 0; + for (int i = 0, iMax = _allSystems.Length; i < iMax; i++) + { + if (_allSystems[i] is T) + { + _buffer[l++] = _allSystems[i]; + } + } + T[] result = new T[l]; + Array.Copy(_buffer, result, l); + return result; + } #endregion #region GetRunner