-
Notifications
You must be signed in to change notification settings - Fork 2
Question: does custom serializable work with structs? #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I just made a quick fix for supporting serializable struct type. Personally I don't think use a serializable struct is a good idea since it requires box/unbox during deserializing, and the performance might be slower than class one. Feel free to reopen this issue if it is not what you're asking for. |
Thanks for the quick fix. Could you please elaborate about the boxing? Because I've seen some tests done by my colleagues to prove that structs are better than classes on a ScriptableObject. The results are all in favor of structs (Unity 2021). |
That really depends on which platform and environment your tests are running on. Back to serializable struct here in your question, the only difference between struct and class is
Codepublic class MyClass
{
public Matrix4x4 Value;
}
public struct MyStruct
{
public Matrix4x4 Value;
}
public class S
{
public MyClass Class;
public MyStruct Struct;
}
[MemoryDiagnoser]
public class Deserializing
{
private S instance = new();
private FieldInfo classField = typeof(S).GetField(nameof(S.Class), BindingFlags.Instance | BindingFlags.Public);
private FieldInfo structField = typeof(S).GetField(nameof(S.Struct), BindingFlags.Instance | BindingFlags.Public);
private Matrix4x4 value;
private FieldInfo classValueField = typeof(MyClass).GetField(nameof(MyClass.Value), BindingFlags.Instance | BindingFlags.Public);
private FieldInfo structValueField = typeof(MyStruct).GetField(nameof(MyStruct.Value), BindingFlags.Instance | BindingFlags.Public);
[Benchmark]
public S Class_CreateInstance()
{
var c = Activator.CreateInstance(typeof(MyClass));
classValueField.SetValue(c, value);
classField.SetValue(instance, c);
return instance;
}
[Benchmark]
public S Class_GetUninitializedObject()
{
var c = FormatterServices.GetUninitializedObject(typeof(MyClass));
classValueField.SetValue(c, value);
classField.SetValue(instance, c);
return instance;
}
[Benchmark]
public S Struct_CreateInstance_SetValue()
{
var s = Activator.CreateInstance(typeof(MyStruct));
structValueField.SetValue(s, value);
structField.SetValue(instance, s);
return instance;
}
[Benchmark]
public S Struct_CreateInstance_SetValueDirectly()
{
var s = Activator.CreateInstance(typeof(MyStruct));
structValueField.SetValueDirect(__makeref(s), value);
structField.SetValue(instance, s);
return instance;
}
[Benchmark]
public S Struct_GetUninitialized_SetValue()
{
var s = FormatterServices.GetUninitializedObject(typeof(MyStruct));
structValueField.SetValue(s, value);
structField.SetValue(instance, s);
return instance;
}
[Benchmark]
public S Struct_GetUninitialized_SetValueDirect()
{
var s = FormatterServices.GetUninitializedObject(typeof(MyStruct));
structValueField.SetValueDirect(__makeref(s), value);
structField.SetValue(instance, s);
return instance;
}
} |
The examples for custom serializable types are all classes so I wonder if structs are possible?
The text was updated successfully, but these errors were encountered: