X Tutup
Skip to content

Use LocalVector in Animation#101285

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
Nazarwadim:use_LocalVector_in_animation
Sep 17, 2025
Merged

Use LocalVector in Animation#101285
Repiteo merged 1 commit intogodotengine:masterfrom
Nazarwadim:use_LocalVector_in_animation

Conversation

@Nazarwadim
Copy link
Contributor

@Nazarwadim Nazarwadim commented Jan 8, 2025

Closes: godotengine/godot-proposals#10594

Replaced only those that are often used by the animation system.

Number of get/_copy_on_write calls in tps-demo every 5 seconds:

Structure Name: Error CowData<Animation::TKey<Vector3>>::resize(CowData::Size) [T = Animation::TKey<Vector3>, p_ensure_zero = false]
Lookup times: 180652
Structure Name: Error CowData<Animation::Track *>::resize(CowData::Size) [T = Animation::Track *, p_ensure_zero = false]
Lookup times: 310430
Structure Name: Error CowData<Animation::TKey<Quaternion>>::resize(CowData::Size) [T = Animation::TKey<Quaternion>, p_ensure_zero = false]
Lookup times: 644596
Structure Name: Error CowData<PhysicalBoneSimulator3D::SimulatedBone>::resize(CowData::Size) [T = PhysicalBoneSimulator3D::SimulatedBone, p_ensure_zero = false]
Lookup times: 738642

I will make benchmarks later.

@Nazarwadim Nazarwadim requested a review from a team as a code owner January 8, 2025 11:00
@AThousandShips AThousandShips added this to the 4.x milestone Jan 8, 2025
@Nazarwadim Nazarwadim force-pushed the use_LocalVector_in_animation branch 3 times, most recently from c728d8a to 94bc137 Compare January 8, 2025 11:40
@Nazarwadim Nazarwadim requested a review from a team as a code owner January 8, 2025 11:52
@Nazarwadim Nazarwadim force-pushed the use_LocalVector_in_animation branch 2 times, most recently from 61c912d to 9da63e2 Compare January 8, 2025 17:45
@clayjohn
Copy link
Member

I tested this with the MRP from #101494 on an M2 MBP. It improved frame time by about 0.1 ms (10.8 -> 10.7) However, there was enough fluctuation that this observed difference could have been a measurement error.

That being said, I am inclined to believe that there was an improvement since Vector showed up in a few places in my earlier profile. I suspect that the larger cost is the allocations rather than the CoW semantics, so this is only showing a small improvement.

Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great effort. None of these needed the CoW semantics, so we were just wasting a lot of effort.

It looks like a lot of code changes, but its just the necessary renaming for switching from Vector to LocalVector.

The only other change is a few places where we switch from checking if (some_int <0 || some_int > max_value) to if (uint32_t(some_int) > max_value) i.e. relying on integer overflow to avoid a check.

Can you rebase? I think this could be merged during the beta cycle

@Nazarwadim Nazarwadim force-pushed the use_LocalVector_in_animation branch from 9da63e2 to 1009da6 Compare June 16, 2025 17:44
@Nazarwadim Nazarwadim requested a review from a team as a code owner June 16, 2025 17:44
@clayjohn
Copy link
Member

Looks like it is failing checks on a signed/unsigned comparison

@Nazarwadim Nazarwadim force-pushed the use_LocalVector_in_animation branch 2 times, most recently from 673a223 to 9ee04ea Compare June 17, 2025 17:23
@akien-mga
Copy link
Member

The only other change is a few places where we switch from checking if (some_int <0 || some_int > max_value) to if (uint32_t(some_int) > max_value) i.e. relying on integer overflow to avoid a check.

Does this measurably impact performance? IMO that worsens readability / communication of the intention of the code.
It forces the reader (me at least) to think about what's the purpose of the cast and it doesn't immediately bring "[0; max[ index check" to mind.

@lawnjelly
Copy link
Member

lawnjelly commented Jun 18, 2025

EDIT : I thought this would be no different either way, but it turned out to not be the case, see below, the behaviour is different depends on the sign, and whether the value is a constant, the compiler will only do the trick under specific circumstances.

https://godbolt.org/z/nTcf86Kos

c++:

#include <stdint.h>

bool func(int32_t input)
{
    if ((input < 0) || (input > 10))
    {
        return true;
    }
    return false;
}

assembly:

func(int):
        cmp     edi, 11 // one comparison
        setae   al // set if carry flag clear
        ret

@Nazarwadim
Copy link
Contributor Author

@lawnjelly Compilers don't know this trick if max_value is an int variable. https://godbolt.org/z/crfKTP333

@Nazarwadim Nazarwadim force-pushed the use_LocalVector_in_animation branch from 9ee04ea to df7835a Compare June 18, 2025 06:10
@lawnjelly
Copy link
Member

lawnjelly commented Jun 18, 2025

@lawnjelly Compilers don't know this trick if max_value is an int variable. https://godbolt.org/z/crfKTP333

Good point actually.

Also on further testing, turns out they also can't do the trick if m_size is unsigned and not a constant, because it could be outside the signed range, so they end up doing two branches like the worst case. 😮

So for it to be confident of the trick I think it has to be sure that both the value and the size are unsigned, otherwise all bets are off it seems.

@Nazarwadim Nazarwadim force-pushed the use_LocalVector_in_animation branch from df7835a to 1964918 Compare June 18, 2025 15:38
@clayjohn clayjohn removed this from the 4.x milestone Sep 11, 2025
@clayjohn clayjohn added this to the 4.6 milestone Sep 11, 2025
@Repiteo Repiteo merged commit 2557b0d into godotengine:master Sep 17, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Sep 17, 2025

Thanks!

@Nazarwadim Nazarwadim deleted the use_LocalVector_in_animation branch September 17, 2025 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Animation system internally should use LocalVector instead of Vector

6 participants

X Tutup