Improvement on Preset.Initialize ( StateSystem )

suvlimex

New member
Hello everyone and a happy new year, I'm playing around with the StateSystem to see if I can improve performance somewhat, since initializing the DefaultPresets is a bit expensive sometimes. I understand that perhaps it is as optimized as possible, but here is something I have found and some metrics. The thing is in the Preset class in the Initialize method when MakeGenericType is called, I have tried pooling the genericDelegateType and there seems to be a substantial improvement.

C#:
        public virtual void Initialize(object obj, MemberVisibility visibility)
        {
            Profiler.BeginSample("InitializePreset");
            var properties = Serialization.GetSerializedProperties(obj.GetType(), visibility);
            var valueCount = 0;
            int propertiesLength = properties.Length;
            m_Delegates = new BaseDelegate[propertiesLength];
            for (int i = 0; i < propertiesLength; ++i) {
                var propertyInfo = properties[i];
                // The property may not be valid.
                if (Serialization.GetValidGetMethod(propertyInfo, visibility) == null) {
                    continue;
                }

                // Create a generic delegate based on the property type.                      
                // # NEW CODE
                Type genericDelegateType;
                if(!m_Pool.TryGetValue(propertyInfo.PropertyType, out genericDelegateType)){
                    genericDelegateType = typeof(GenericDelegate<>).MakeGenericType(propertyInfo.PropertyType);
                    m_Pool.Add(propertyInfo.PropertyType, genericDelegateType);
                }                                                    
                // # OLD CODE
                // var genericDelegateType = typeof(GenericDelegate<>).MakeGenericType(propertyInfo.PropertyType);              
                m_Delegates[valueCount] = Activator.CreateInstance(genericDelegateType) as BaseDelegate;

                // Initialize the delegate.
                if (m_Delegates[valueCount] != null) {
                    m_Delegates[valueCount].Initialize(obj, propertyInfo, visibility);
                } else {
                    Debug.LogWarning("Warning: Unable to create preset of type " + propertyInfo.PropertyType);
                }
                valueCount++;
            }
            if (m_Delegates.Length != valueCount) {
                Array.Resize(ref m_Delegates, valueCount);
            }
            Profiler.EndSample();
        }

And here the results:
- The old way
Captura de pantalla 2024-01-01 a las 15.16.58.png
- The new way
Captura de pantalla 2024-01-01 a las 15.14.42.png

Now I'm testing my entire game to check that everything is correct, if someone from opsive can indicate if there is anything wrong with this it would be great. Maybe the problem could be in memory management?
 
To put it in context, I'm trying to optimize the Awake/State.Initialize performance of any character, AI or not.
 
Thanks for posting, I'll do the same. Using the object pool makes sense.
 
Thanks Justin, as far as I've been testing it's not a problem. I'm glad it's helpful. I'll post if I find anything else that might be helpful.
 
Top