Skip to content

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

Closed
laicasaane opened this issue Oct 15, 2022 · 3 comments
Closed

Question: does custom serializable work with structs? #32

laicasaane opened this issue Oct 15, 2022 · 3 comments

Comments

@laicasaane
Copy link

The examples for custom serializable types are all classes so I wonder if structs are possible?

@quabug
Copy link
Owner

quabug commented Oct 15, 2022

I just made a quick fix for supporting serializable struct type.
But it still needs more tests before release.

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.
But who knows, it might be useful in some cases.

Feel free to reopen this issue if it is not what you're asking for.

@quabug quabug closed this as completed Oct 15, 2022
@laicasaane
Copy link
Author

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).

@quabug
Copy link
Owner

quabug commented Oct 15, 2022

That really depends on which platform and environment your tests are running on.
General speaking, struct with SetValueDirectly is the fastest way to deserialize data since there's no boxing involved, but unfortunately, unity's IL2CPP does not support this operator yet, so it fallback to SetValue for IL2CPP, and nowadays, almost all platform requires IL2CPP for some reason.
Anyway, always run benchmarks on your target platform and see if it is worth optimizing.

Back to serializable struct here in your question, the only difference between struct and class is SetValue processing. I don't know how exactly Unity write code on this, but it is likely the same as the code from following benchmark.

BenchmarkDotNet=v0.13.2, OS=macOS Monterey 12.4 (21F79) [Darwin 21.5.0]
Apple M1 2.40GHz, 1 CPU, 8 logical and 8 physical cores
  [Host]     : Mono 6.12.0.182 (2020-02/6051b710727 Tue), X64 VectorSize=128
  DefaultJob : Mono 6.12.0.182 (2020-02/6051b710727 Tue), X64 VectorSize=128


|                                 Method |       Mean |   Error |  StdDev |   Gen0 | Allocated |
|--------------------------------------- |-----------:|--------:|--------:|-------:|----------:|
|                   Class_CreateInstance |   902.8 ns | 2.17 ns | 2.03 ns | 0.0381 |         - |
|           Class_GetUninitializedObject |   541.0 ns | 3.65 ns | 3.41 ns | 0.0381 |         - |
|         Struct_CreateInstance_SetValue | 1,070.9 ns | 2.65 ns | 2.21 ns | 0.0458 |         - |
| Struct_CreateInstance_SetValueDirectly |   846.7 ns | 1.11 ns | 0.99 ns | 0.0458 |         - |
|       Struct_GetUninitialized_SetValue |   633.7 ns | 0.80 ns | 0.67 ns | 0.0381 |         - |
| Struct_GetUninitialized_SetValueDirect |   360.8 ns | 0.18 ns | 0.15 ns | 0.0381 |         - |

Code
public 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;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants