Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Make AnimationNodeBlendTree use OrderedHashMap insteads Map #79598

Merged
merged 1 commit into from
Aug 18, 2023

Conversation

TokageItLab
Copy link
Member

@TokageItLab TokageItLab commented Jul 18, 2023

3.x version of #79595. Supersedes #79531.

An ordered map must be used.

...I am not familiar with OrderedHashMap, does its order depend on the key? If it does not depend, then StringName is sufficient without making to be a String.

@TokageItLab TokageItLab added this to the 3.x milestone Jul 18, 2023
@TokageItLab TokageItLab requested a review from a team as a code owner July 18, 2023 02:59
@TokageItLab TokageItLab force-pushed the orderdhashmap-blendtree branch 10 times, most recently from f4d1488 to 7a236fa Compare July 18, 2023 05:48
@AThousandShips
Copy link
Member

AThousandShips commented Jul 18, 2023

You can use StringName::AlphCompare here as well, check the template for the map

Edit: the "ordered" part seems to mean that it preserves insertion order, not comparison, I think

I'd suggest Map<StringName, Node, StringName::AlphCompare>

@TokageItLab
Copy link
Member Author

TokageItLab commented Jul 18, 2023

It is sufficient if the order of the inserted order, or the order of the index list of the map depending on the key is unique.

At least from testing #79595 it seems to work well, but does unique comparison results mean that the order of the index list is unique? @AThousandShips

@AThousandShips
Copy link
Member

AThousandShips commented Jul 18, 2023

Pointer comparison means unique order, in one run, and unless the names are freed and reallocated they'll remain ordered, the order doesn't persist between runs though

But in cases where the nose is removed and re-added it'll have a new order, if using hash map, and it'll have if the node is removed, and the string name is unallocated because it is freed

Using pointer comparison is more performant, so if possible it'd be better

I suspect the issue with pointers is only that it doesn't remain consistent between runs, and isn't consistent if the StringName is freed (completely, i.e. ref-count reaches zero)

So if you add "ABC" and "BCD" they'll have one order, if you then remove "ABC" and re-add it, after "ABC" is freed, it'll be unknown where it'll end up

Sorry if I gave a confusing answer 😅

@TokageItLab TokageItLab removed request for a team July 18, 2023 08:40
@TokageItLab
Copy link
Member Author

TokageItLab commented Jul 18, 2023

Looking at the code for Map/RBMap, I see that the comparison by Comparator uniquely determines the index to be stored.

When StringName is used as a key of the Map/RBMap:

  • When using AlphCompare, the index is uniquely determined by the string
  • If nothing is specified, the index is determined by comparison of memory address, so if the memory address changes due to restart, etc., the index will change

@AThousandShips Is this correct?

@AThousandShips
Copy link
Member

That sounds correct from my experience and reading of the code

My suggestion would be then:

  • If the order between runs is important (like seems to be the case in 4.x) AlphCompare is good
  • Otherwise pointer comparison should be preferred as it is significantly more performant

@TokageItLab TokageItLab force-pushed the orderdhashmap-blendtree branch 2 times, most recently from 231d599 to 417c015 Compare July 18, 2023 08:53
Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

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

LGTM, haven't tested the code but usage is correct

@akien-mga akien-mga modified the milestones: 3.x, 3.6 Aug 18, 2023
@akien-mga akien-mga merged commit cf4ccdf into godotengine:3.x Aug 18, 2023
13 checks passed
@akien-mga
Copy link
Member

Thanks!

@TokageItLab TokageItLab deleted the orderdhashmap-blendtree branch February 14, 2024 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants