X Tutup
Skip to content

Optimize Animation::_track_update_hash#110400

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
Ryan-000:Optimize-Animation_track_update_hash
Oct 27, 2025
Merged

Optimize Animation::_track_update_hash#110400
Repiteo merged 1 commit intogodotengine:masterfrom
Ryan-000:Optimize-Animation_track_update_hash

Conversation

@Ryan-000
Copy link
Contributor

While I was profiling, I found that Animation::_track_update_hash spent significant time allocating due to string concatenation, NodePath::get_concatenated_names(), NodePath::get_concatenated_subnames() internals and constructing a StringName had some overhead.

I rewrote the method to resolve these issues.

@Ryan-000 Ryan-000 requested a review from a team as a code owner September 11, 2025 02:36
Copy link
Member

@TokageItLab TokageItLab left a comment

Choose a reason for hiding this comment

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

Considering that hashes are fine as long as they don't conflict, and given that existing code is working with this, I assume it should be fine.

However, since I'm not an expert of hash algorithm, it might be better to have someone with more detailed knowledge review it.

The literature I found on the internet was as follows:
https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector

@TokageItLab TokageItLab requested a review from a team September 11, 2025 02:48
@TokageItLab TokageItLab added this to the 4.6 milestone Sep 11, 2025
@TokageItLab TokageItLab moved this to Approved, Waiting for Production in Animation Team Issue Triage Sep 11, 2025
@Ryan-000 Ryan-000 force-pushed the Optimize-Animation_track_update_hash branch from a7d43b7 to 0a50c8e Compare September 11, 2025 02:52
@Ryan-000 Ryan-000 force-pushed the Optimize-Animation_track_update_hash branch from 0a50c8e to 8c1ef2e Compare September 11, 2025 03:13
@Ryan-000 Ryan-000 force-pushed the Optimize-Animation_track_update_hash branch from 06d1b4d to b9e9dd6 Compare September 11, 2025 03:55
@beicause
Copy link
Contributor

Considering that hashes are fine as long as they don't conflict, and given that existing code is working with this, I assume it should be fine.

However, since I'm not an expert of hash algorithm, it might be better to have someone with more detailed knowledge review it.

The literature I found on the internet was as follows: https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector

Any hash algorithm may have collisions. If a value with the same hash already exists in the container, we should handle hash collisions (comparing if the values are actually equal always, placing elements with the same hash into a bucket when inserting).

It seems we are not doing so, using hash directly as a key seems problematic, which may be a separate issue.

AHashMap<Animation::TypeHash, TrackCache *, HashHasher> track_cache;

@TokageItLab
Copy link
Member

TokageItLab commented Sep 11, 2025

It's true that it occurs with astronomical probability. If we want to add avoidance, I think adding it is nice. The track hash was originally implemented in #86687 to identify blendable track counts, so as long as it can identify them, it's fine.

@beicause
Copy link
Contributor

I think we just need to replace TypeHash with a struct as key, and implement its hasher and comparator, so we can completely avoid hash collisions.

@mrjustaguy
Copy link
Contributor

Benchmarking would be nice, you can use the MRP of #101494 if you don't have an MRP of your own to share

@Ryan-000
Copy link
Contributor Author

I think we just need to replace TypeHash with a struct as key, and implement its hasher and comparator, so we can completely avoid hash collisions.

This purpose of this PR is to make the current hashing implementation more efficient, not to rewrite the hashing backend. If you’d like to take it on, feel free to open a separate PR.

@Ryan-000
Copy link
Contributor Author

Benchmarking would be nice, you can use the MRP of #101494 if you don't have an MRP of your own to share

I reviewed the MRP, but it is not suitable to benchmark this PR, as there are not enough animations or tracks to reveal the impact during the initial load (only 3 animations, with 4 tracks).

If you would like to benchmark this PR, you would need a project with a much more animations/track counts (for reference, my game has 27,723 tracks across 375 skeletal animations).

@mrjustaguy
Copy link
Contributor

Yea wasn't sure if it'd be suitable, but I'm not aware of any other more appropriate benchmarks (hence the if you don't have your own MRP remark), and wanted to say that some Benchmarking would be nice.

It's fine though if you're unable to share it, but results would still be welcome.

Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

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

Looks good to me. Using the dedicated hash functions instead of hashing the string should work and will solve the performance issue.

I agree using the hash as keys should be fixed, but I also agree it's out of scope for this PR.

Co-Authored-By: Silc Lizard (Tokage) Renew <tokage.it.lab@gmail.com>
Co-Authored-By: Luo Zhihao <luo_zhihao@outlook.com>
Co-Authored-By: Lukas Tenbrink <lukas.tenbrink@gmail.com>
@Ryan-000 Ryan-000 force-pushed the Optimize-Animation_track_update_hash branch from 44d0609 to 44856c8 Compare October 24, 2025 19:04
@Repiteo Repiteo merged commit b729375 into godotengine:master Oct 27, 2025
20 checks passed
@github-project-automation github-project-automation bot moved this from Approved, Waiting for Production to Done in Animation Team Issue Triage Oct 27, 2025
@Repiteo
Copy link
Contributor

Repiteo commented Oct 27, 2025

Thanks!

@Ryan-000 Ryan-000 deleted the Optimize-Animation_track_update_hash branch October 28, 2025 19:52
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.

6 participants

X Tutup