Skip to content

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.

  1. 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, and double. Most Unity value types, including the Vector types, Quaternion, and Color, meet this requirement. User-defined structs can also qualify.
  2. The struct must have the GCOptimize attribute configured. Several commonly used Unity structs, including the Vector types, Quaternion, and Color, already have this attribute. Put it on the struct, or name the struct at assembly level: [assembly: GCOptimize(typeof(Game.HitInfo))].
  3. 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 as CSharpCallLua. A struct configured in a referenced assembly is passed without boxing in the assemblies that use it too.

See the GCOptimize section of Configure.