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); } }
public static T Spawn <T>() where T : class, IFilterBase, new() { var key = WorldUtilities.GetKey <T>(); PoolInternalBase pool; if (PoolFilters.pool.TryGetValue(key, out pool) == true) { var obj = pool.Spawn(); if (obj != null) { return((T)obj); } } else { pool = new PoolInternalBase(null, null); var obj = (T)pool.Spawn(); PoolFilters.pool.Add(key, pool); if (obj != null) { return(obj); } } return(PoolInternalBase.Create <T>()); }
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; } } } } } }
void IPoolImplementation.PoolRecycle <T>(ref T obj) { if (this.isNull == true || this.pools.Recycle(ref obj) == false) { PoolInternalBase.CallOnDespawn(obj, null); obj = null; } }
public static T Create <T>(PoolInternalBase pool) where T : new() { var instance = new T(); PoolInternalBase.CallOnSpawn(instance, pool); return(instance); }
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; } } } } } }
T IPoolImplementation.PoolSpawn <T>(System.Action <T> destructor) { if (this.isNull == true) { var res = new T(); PoolInternalBase.CallOnSpawn(res, null); return(res); } return(this.pools.Spawn <T, byte>(0, (state) => new T(), destructor)); }
public static StructRegistryBase Spawn <T>() where T : struct, IStructComponent { var key = WorldUtilities.GetAllComponentTypeId <T>(); var obj = (StructComponents <T>)PoolRegistries.Spawn_INTERNAL(key, out var pool); if (obj != null) { return(obj); } return(PoolInternalBase.Create <StructComponents <T> >(pool)); }
public static T Spawn <T>() where T : class, IComponentBase, new() { var key = WorldUtilities.GetKey <T>(); var obj = (T)PoolComponents.Spawn_INTERNAL(key); if (obj != null) { return(obj); } return(PoolInternalBase.Create <T>()); }
public static object Spawn(System.Type type) { var key = WorldUtilities.GetKey(type); var instance = PoolComponents.Spawn_INTERNAL(type, key, out var pool); if (instance == null) { instance = (IComponent)System.Activator.CreateInstance(type); PoolInternalBase.CallOnSpawn(instance, pool); } return(instance); }
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); }
T IPoolImplementation.PoolSpawn <T, TState>(TState state, System.Func <TState, T> constructor, System.Action <T> destructor) { if (this.isNull == true) { var res = constructor.Invoke(state); PoolInternalBase.CallOnSpawn(res, null); //ME.WeakRef.Reg(res); return(res); } var instance = this.pools.Spawn <T, TState>(state, constructor, destructor); //ME.WeakRef.Reg(instance); return(instance); }
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(); } }
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 }
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); }
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!"); } }
public T Spawn <T, TState>(TState state, System.Func <TState, T> constructor, System.Action <T> destructor = null) where T : class { if (Pools.isActive == false) { var instance = constructor.Invoke(state); PoolInternalBase.CallOnSpawn(instance, null); return(instance); } var type = typeof(T); if (this.pool.TryGetValue(type, out var pool) == true) { return((T)pool.Spawn(state)); } pool = new PoolInternal <T, TState>(type, constructor, destructor); this.pool.Add(type, pool); return((T)pool.Spawn(state)); }
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 max = null; int maxCount = 0; for (int i = 0; i < PoolInternalBase.list.Count; ++i) { var item = PoolInternalBase.list[i]; if (maxCount < item.cache.Count) { maxCount = item.cache.Count; max = item; } } if (max != null) { UnityEngine.Debug.Log("Max type: " + max.cache.First().GetType() + ", Pool: " + max); } }
private static object Spawn_INTERNAL(System.Type type, int key, out PoolInternalBase pool) { if (PoolComponents.pool.TryGetValue(key, out pool) == true) { var obj = pool.Spawn(); if (obj != null) { return(obj); } } else { pool = new PoolInternalBase(type, null, null); var obj = pool.Spawn(); PoolComponents.pool.Add(key, pool); if (obj != null) { return(obj); } } return(null); }
private static object Spawn_INTERNAL(int key, out PoolInternalBase pool) { if (PoolRegistries.pool.TryGetValue(key, out pool) == true) { var obj = pool.Spawn(); if (obj != null) { return(obj); } } else { pool = new PoolInternalBase(typeof(StructRegistryBase), null, null); var obj = pool.Spawn(); PoolRegistries.pool.Add(key, pool); if (obj != null) { return(obj); } } return(null); }
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!"); } }
public bool Recycle <T>(ref T obj) where T : class { if (obj == null) { return(false); } if (Pools.isActive == false) { PoolInternalBase.CallOnDespawn(obj, null); obj = default; return(false); } { var type = typeof(T); if (this.pool.TryGetValue(type, out var pool) == true) { pool.Recycle(obj); obj = default; return(true); } } { var type = obj.GetType(); if (this.pool.TryGetValue(type, out var pool) == true) { pool.Recycle(obj); obj = default; return(true); } } return(false); }