Performance
When passing values in dataflow, there is a risk of performance degradation due to GC Alloc in boxing.
Conditions for boxing
Boxed will occur if any of the following.
- You are passing a value type that is not registered in ValueMediator.
- The object version I/O method is used for value type input / output.
- A member via object type is used in Reflection etc.
- You are typing directly into the value field using DataLinkAttribute.
- If you are using the following built-in scripts, boxing cannot be avoided because they deal directly with object types
How to avoid boxing
Use a type-specific slot class
Avoid using slots of unnecessary generic types, for example, use OutputSlotInt if you are sure that only int type will be used.
Avoid boxing with ValueMediator
If you use OutputSlotAny or OutputSlotTypable, you can reduce the boxing by using ValueMediator.
However, even if ValueMediator is used, GC Alloc will occur to create a storage instance at the first output.
By reusing the created storage instance for the second and subsequent outputs, boxing is avoided.
Registering ValueMediator
Boxing can be reduced by registering the value type in ValueMediator.
- ValueMediatorInitializeOnLoadMethod
By adding the ValueMediatorInitializeOnLoadMethod attribute to the static method, it will be called when the ValueMediator is initialized. - ValueMediator.Register
Register the value type with the ValueMediator. - ValueMediator.RegisterEnum
Register the enumeration type with ValueMediator.
Enumeration types are internally special, so use this instead of the Register method.
As an example, the code to register the DataFlowExampleData structure in the Arbor.Example namespace to the ValueMediator is as follows
|
|
Types pre-registered
- sbyte
- byte
- short
- ushort
- int
- uint
- long
- ulong
- char
- float
- double
- bool
- decimal
- UnityEngine.Vector2
- UnityEngine.Vector3
- UnityEngine.Vector4
- UnityEngine.Quaternion
- UnityEngine.Rect
- UnityEngine.Bounds
- UnityEngine.Color
- UnityEngine.Color32
- UnityEngine.Matrix4x4
- UnityEngine.Vector2Int
- UnityEngine.Vector3Int
- UnityEngine.RectInt
- UnityEngine.BoundsInt
- UnityEngine.Ray
- UnityEngine.Ray2D
- UnityEngine.RaycastHit
- UnityEngine.RaycastHit2D
Use the generic version of the method for value input and output
If you use the object version of the method, such as OutputSlotAny.SetValue(object), it will be boxed at that point, so use a generic method such as SetValue<T>(T value) instead.
Example of output slot side
|
|
Example of the input slot side
|
|
Do not use Reflection
While DataLinkAttribute and InvokeMethod scripts are convenient, they are boxed because they go through Reflection.
Please input from FlexibleField system or make your own script such as unique method call.