コード例 #1
0
ファイル: PoolBase.cs プロジェクト: chromealex/ecs-submodule
        public static void Debug()
        {
            UnityEngine.Debug.Log("Allocated: " + PoolInternalBase.allocated + ", Deallocated: " + PoolInternalBase.deallocated + ", Used: " + PoolInternalBase.used +
                                  ", cached: " + (PoolInternalBase.deallocated - PoolInternalBase.allocated) + ", new: " + PoolInternalBase.newAllocated);

            PoolInternalBase maxCached = null;
            PoolInternalBase maxAlloc  = null;
            int maxCountCache          = 0;
            int maxCountAlloc          = 0;

            for (int i = 0; i < PoolInternalBase.list.Count; ++i)
            {
                var item = PoolInternalBase.list[i];
                if (maxCountCache < item.cache.Count)
                {
                    maxCountCache = item.cache.Count;
                    maxCached     = item;
                }

                if (maxCountAlloc < item.poolAllocated)
                {
                    maxCountAlloc = item.poolAllocated;
                    maxAlloc      = item;
                }
            }

            if (maxCached != null)
            {
                UnityEngine.Debug.Log("Max cache type: " + maxCached.poolType + ", Pool:\n" + maxCached);
            }

            if (maxAlloc != null)
            {
                UnityEngine.Debug.Log("Max alloc type: " + maxAlloc.poolType + ", Pool:\n" + maxAlloc);
            }

            for (int i = 0; i < PoolInternalBase.list.Count; ++i)
            {
                var item = PoolInternalBase.list[i];
                if (item.poolAllocated != item.poolDeallocated)
                {
                    UnityEngine.Debug.LogWarning("Memory leak: " + item.poolType + ", Pool:\n" + item);

                    if (PoolInternalBase.IsStackTraceEnabled() == true && item.stackTraces != null)
                    {
                        var max = 10;
                        foreach (var stack in item.stackTraces)
                        {
                            UnityEngine.Debug.Log(stack.Key.GetType() + "\n" + stack.Value);
                            --max;
                            if (max <= 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: PoolBase.cs プロジェクト: word-ptr/ecs-submodule
        public static void Debug()
        {
            UnityEngine.Debug.Log($"Allocated: {PoolInternalBase.allocated}, Deallocated: {PoolInternalBase.deallocated}, Used: {PoolInternalBase.used}, cached: {(PoolInternalBase.deallocated - PoolInternalBase.allocated)}, new: {PoolInternalBase.newAllocated}, approx bytes used: {PoolInternalBase.bytesUsed}");

            PoolInternalBase maxCached = null;
            PoolInternalBase maxAlloc  = null;
            int maxCountCache          = 0;
            int maxCountAlloc          = 0;

            for (int i = 0; i < PoolInternalBase.list.Count; ++i)
            {
                var item = PoolInternalBase.list[i];
                if (maxCountCache < item.cache.Count)
                {
                    maxCountCache = item.cache.Count;
                    maxCached     = item;
                }

                if (maxCountAlloc < item.poolAllocated)
                {
                    maxCountAlloc = item.poolAllocated;
                    maxAlloc      = item;
                }
            }

            if (maxCached != null)
            {
                UnityEngine.Debug.Log($"Max cache type: {maxCached.poolType}, Pool:\n{maxCached}");
            }

            if (maxAlloc != null)
            {
                UnityEngine.Debug.Log($"Max alloc type: {maxAlloc.poolType}, Pool:\n{maxAlloc}");
            }

            for (int i = 0; i < PoolInternalBase.list.Count; ++i)
            {
                var item = PoolInternalBase.list[i];
                if (item.poolAllocated != item.poolDeallocated)
                {
                    UnityEngine.Debug.LogWarning($"Memory leak: {item.poolType}, Pool:\n{item}");

                    if (PoolInternalBase.IsStackTraceEnabled() == true && item.stackTraces != null)
                    {
                        var max = 10;
                        foreach (var stack in item.stackTraces)
                        {
                            UnityEngine.Debug.Log($"{stack.Key.GetType()}\n{stack.Value}");
                            --max;
                            if (max <= 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: PoolBase.cs プロジェクト: word-ptr/ecs-submodule
        public virtual object Spawn <TState>(TState state)
        {
            var item = (this.cache.Count > 0 ? this.cache.Pop() : null);

            if (item == null)
            {
                ++PoolInternalBase.newAllocated;
                ++this.poolNewAllocated;
            }
            else
            {
                this.contains.Remove(item);
                ++PoolInternalBase.used;
                ++this.poolUsed;
            }

            this.Construct(ref item, state);

            if (item is IPoolableSpawn poolable)
            {
                poolable.OnSpawn();
            }

            ++this.poolAllocated;
            ++PoolInternalBase.allocated;

            #if UNITY_EDITOR
            var bytes = this.poolBytesSize;

            if (PoolInternalBase.IsStackTraceEnabled() == true)
            {
                bytes = PoolInternalBase.GetSizeOfObject(item, 8);
                this.poolBytesUsed         += bytes;
                PoolInternalBase.bytesUsed += bytes;

                this.WriteStackTrace(item);
            }
            else
            {
                this.poolBytesUsed         += bytes;
                PoolInternalBase.bytesUsed += bytes;
            }

            if (item != null)
            {
                this.poolType = item.GetType();
            }
            #endif

            return(item);
        }
コード例 #4
0
ファイル: PoolBase.cs プロジェクト: kraidiky/ecs-submodule
        public static void CallOnSpawn <T>(T instance, PoolInternalBase pool)
        {
            if (instance is IPoolableSpawn poolable)
            {
                poolable.OnSpawn();
            }

                    #if UNITY_EDITOR
            if (PoolInternalBase.IsStackTraceEnabled() == true)
            {
                pool.WriteStackTrace(instance);
            }
                    #endif
        }
コード例 #5
0
ファイル: PoolBase.cs プロジェクト: word-ptr/ecs-submodule
        public static void CallOnDespawn <T>(T instance, PoolInternalBase pool)
        {
            #if UNITY_EDITOR
            if (PoolInternalBase.IsStackTraceEnabled() == true)
            {
                pool?.RemoveStackTrace(instance);
            }
            #endif

            if (instance is IPoolableRecycle poolable)
            {
                poolable.OnRecycle();
            }
        }
コード例 #6
0
ファイル: PoolBase.cs プロジェクト: kraidiky/ecs-submodule
        public virtual object Spawn()
        {
                        #if MULTITHREAD_SUPPORT
            this.cache.TryPop(out object item);
                        #else
            var item = (this.cache.Count > 0 ? this.cache.Pop() : null);
                        #endif
            if (item == null)
            {
                ++PoolInternalBase.newAllocated;
                ++this.poolNewAllocated;
            }
            else
            {
                this.contains.Remove(item);
                ++PoolInternalBase.used;
                ++this.poolUsed;
            }

            if (this.constructor != null && item == null)
            {
                item = this.constructor.Invoke();
            }

            if (item is IPoolableSpawn poolable)
            {
                poolable.OnSpawn();
            }

            ++this.poolAllocated;
            ++PoolInternalBase.allocated;

                    #if UNITY_EDITOR
            if (PoolInternalBase.IsStackTraceEnabled() == true)
            {
                this.WriteStackTrace(item);
            }

            if (item != null)
            {
                this.poolType = item.GetType();
            }
                    #endif

            return(item);
        }
コード例 #7
0
ファイル: PoolBase.cs プロジェクト: word-ptr/ecs-submodule
        public virtual void Recycle(object instance)
        {
            #if UNITY_EDITOR
            if (PoolInternalBase.IsStackTraceEnabled() == true)
            {
                //var bytes = PoolInternalBase.GetSizeOfObject(instance, 8);
                //this.poolBytesUsed -= bytes;
                //PoolInternalBase.bytesUsed -= bytes;

                this.RemoveStackTrace(instance);
            }
            else
            {
                //var bytes = this.poolBytesUsed;
                //this.poolBytesUsed -= bytes;
                //PoolInternalBase.bytesUsed -= bytes;
            }

            if (instance != null)
            {
                this.poolType = instance.GetType();
            }
            #endif

            ++this.poolDeallocated;
            ++PoolInternalBase.deallocated;

            this.Destruct(instance);

            if (instance is IPoolableRecycle poolable)
            {
                poolable.OnRecycle();
            }

            if (this.contains.Contains(instance) == false)
            {
                this.contains.Add(instance);
                this.cache.Push(instance);
            }
            else
            {
                UnityEngine.Debug.LogError($"You are trying to push instance {instance} that already in pool!");
            }
        }
コード例 #8
0
ファイル: PoolBase.cs プロジェクト: kraidiky/ecs-submodule
        public virtual void Recycle(object instance)
        {
                    #if UNITY_EDITOR
            if (PoolInternalBase.IsStackTraceEnabled() == true)
            {
                this.RemoveStackTrace(instance);
            }

            if (instance != null)
            {
                this.poolType = instance.GetType();
            }
                    #endif

            ++this.poolDeallocated;
            ++PoolInternalBase.deallocated;

            if (this.desctructor != null)
            {
                this.desctructor.Invoke(instance);
            }

            if (instance is IPoolableRecycle poolable)
            {
                poolable.OnRecycle();
            }

            if (this.contains.Contains(instance) == false)
            {
                this.contains.Add(instance);
                this.cache.Push(instance);
            }
            else
            {
                UnityEngine.Debug.LogError("You are trying to push instance " + instance + " that already in pool!");
            }
        }