What is more memory-efficient: allocating Vector3D once and changing its inner value, or creating a new one on each loop?

Godot Version

4.2.1

Question

What is more efficient for this simple mouse movement, and as a general best practice? Notice that I commented out the second option where I allocate a new Vector3D. In this example, I allocate the vectors once and then set the values. What is the best option?
Thanks

public partial class player : CharacterBody3D
{
    private Camera3D camera;

    [Export]
    private float mouseSensitivity = 0.15f;
    private Vector3 RotationDegreesX;
    private Vector3 RotationDegreesY;

    public override void _Ready()
    {
        camera = GetNode<Camera3D>("Camera3D");
        Input.MouseMode = Input.MouseModeEnum.Captured;
        RotationDegreesX = new Vector3(0,0,0);
        RotationDegreesY = new Vector3(0, 0, 0);
    }

    public override void _Input(InputEvent @event)
    {
        if(Input.IsActionPressed("quit"))
        {
            GetTree().Quit();
        }

        if(@event is InputEventMouseMotion)
        {
            InputEventMouseMotion ev = (InputEventMouseMotion)@event;
            RotationDegreesX.Y = ev.Relative.X * mouseSensitivity;
            RotationDegreesY.X = ev.Relative.Y * mouseSensitivity;
            RotationDegrees -= RotationDegreesX;
            camera.RotationDegrees -= RotationDegreesY;

            /*  RotationDegrees -= new Vector3(0, ev.Relative.X * mouseSensitivity,0);
              camera.RotationDegrees -= new Vector3(ev.Relative.Y * mouseSensitivity,0, 0);*/

        }
    }
}

I think vectors are passed by value, so if you are okay working with a global/class vectors, or vectors wrapped in an array/dictionary/class :nauseated_face:, it will be hard to avoid making new vector copies on each vector parameter you may pass.

I could be wrong this is c# maybe it has some tricks up it’s sleeve.

Haven’t looked into the internals of Godot, but usually vectors are value types which are stack allocated, which means it is very efficient.

Therefore I would be very surprised if that would have an actual performance implication.

As already stated below, this pragmatically won’t have an actual impact on anything. Heck, for instance, even in that snippet, each access to ev.Relative basically the creation of a new vector, so you’re already way more vectors than you think without realizing it.

This is a common over-simplification of how value types work. I’d recommend Lippert’s explanation here, here, there.

1 Like

Thats technically not true, a big enough loop with enough data would create a StackOverflowException error.

its not actually that difficult to do, you just need to call a method which calls another method which then calls the original method, that would be enough as the pointer to the method is held on the stack.

As has been correctly pointed out though Vector2D is value not reference, and even then might not end up on the stack.

Unless I’m very mistaken, I don’t believe the spirit of the original question is “is it possible to eventually overflow the stack”. Here, for all that matters, the difference between every call to _Input() constructing 2 vs. 3 Vector3 is almost always going to be irrelevant. If/when not, odds are you have other things to optimize in your codebase.

2 Likes

Agreed just a side note thats all. Might be useful if someone is trying to understand value vs reference.