GC optimization
Garbage collection for complex value types
Section titled “Garbage collection for complex value types”By default, xLua passes C# value types (structs) to Lua by reference. This requires boxing the value before passing it to Lua. Each boxing operation creates a new object; after Lua has finished using the value, that object can become eligible for collection and add to later GC work.
xLua provides a GC optimization for structs. With a small amount of configuration, structs that meet the requirements below can be passed to Lua without causing a C# GC allocation.
Requirements
Section titled “Requirements”- The struct may contain nested structs, but it and every nested struct may contain only these primitive types:
byte,sbyte,short,ushort,int,uint,long,ulong,float, anddouble. Most Unity value types, including theVectortypes,Quaternion, andColor, meet this requirement. User-defined structs can also qualify. - The struct must have the
GCOptimizeattribute configured. Several commonly used Unity structs, including theVectortypes,Quaternion, andColor, already have this attribute. Put it on the struct, or name the struct at assembly level:[assembly: GCOptimize(typeof(Game.HitInfo))]. - The code that passes the struct must have generated bindings: configure the types whose members take or return it as
LuaCallCSharp, and the delegates and interfaces asCSharpCallLua. A struct configured in a referenced assembly is passed without boxing in the assemblies that use it too.
Configuration
Section titled “Configuration”See the GCOptimize section of Configure.
