Quaternion * Vector3 explanation or alternative method because of GC allocation

Basically I’m trying to modify a method that i know is allocating garbage each frame, I found the source being an operation like:

pos += (Target.rotation * LocalSpaceOffset);

pos being Vector3, Target.rotation a Quaternion and LocalSpaceOffset a Vector3.

How can I repeat this without doing it explicitly as it allocates garbage?

That expression is the optimal way of doing it. Vector3 and Quaternion are structs, not classes. So the overloaded operators aren’t doing what you think.

If I have understood correctly, you are assuming that (Target.rotation * LocalSpaceOffset) are creating a temp Vector3 instance in the Heap that will be collected in a future run of the garbage collector. But the design decision of making them structures makes possible to write that line of code that will allocate space for a Vector3 in the Stack and not in the Heap.

You will have to wait for a more savvy programmer than me to explain how C# does it, but I expect that structures living inside a function to end in the stack, not in the heap and to not need a visit from the garbage collector.

class != struct

Completely different than other languages like C++ where you decide where to allocate a class/struct, making them the same thing.

Also I expect that those math operators are implemented in such a way that they benefit from processor cache and maybe some special instructions. But this last paragraph will require confirmation from somebody involved in the engine development, I’m only an user of the engine.