GameObject extensions performance?

johnlondergan

New member
Does this allocate on all gameobjects globally?

namespace Opsive.Shared.Game
{
public static class GameObjectExtensions
{
private static Dictionary<GameObject, Dictionary<Type, object>> s_GameObjectComponentMap = new Dictionary<GameObject, Dictionary<Type, object>>();

private static Dictionary<GameObject, Dictionary<Type, object>> s_GameObjectParentComponentMap = new Dictionary<GameObject, Dictionary<Type, object>>();

private static Dictionary<GameObject, Dictionary<Type, object>> s_GameObjectInactiveParentComponentMap = new Dictionary<GameObject, Dictionary<Type, object>>();

private static Dictionary<GameObject, Dictionary<Type, object[]>> s_GameObjectComponentsMap = new Dictionary<GameObject, Dictionary<Type, object[]>>();

private static Dictionary<GameObject, Dictionary<Type, object[]>> s_GameObjectParentComponentsMap = new Dictionary<GameObject, Dictionary<Type, object[]>>();

public static T GetCachedComponent<T>(this GameObject gameObject)
{
 
It will allocate once. The performance gains are then realized when you get a cached component. You can use the profiler to see the memory allocations on your machine.
 
oh right, static.. ok. I guess one thing I would wonder about is cleanup, since you might create/destroy many gameobjects and get their components the list would keep growing? but that looks like the only downside, if you didn't use pooling for projectiles etc I just wonder how big this list could become. Another question is, does keeping the reference to these gameobjects and components prevent some garbage collection in any way?
 
Top