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.
And here the results:
- The old way
- The new way
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?
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
- The new way
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?