-
-
Notifications
You must be signed in to change notification settings - Fork 22.3k
[.NET] Avoid heap alloc when using StringNames as key in a Dictionary #106133
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing to the .NET module!
The movable
types are only meant to be used to store them in classes (like StringName
) where we can't store ref structs. None of them implement any API other than for conversion. So I think it was intentional to not implement GetHashCode
.
However, as you noticed, we likely meant to call the godot_string_name.GetHashCode
method (which is implemented) from the StringName.GetHashCode
implementation. So the fix should be something like this:
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringName.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringName.cs
index 21d9ada127..a28230636f 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringName.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringName.cs
@@ -161,7 +161,7 @@ namespace Godot
public override int GetHashCode()
{
- return NativeValue.GetHashCode();
+ return NativeValue.DangerousSelfRef.GetHashCode();
}
}
}
That makes sense! I removed the GetHashCode method from movable and changed StringName to use the existing version in |
There was a problem hiding this 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, thanks!
Looks great! The final step before merging is to squash all the commits together so that the whole PR only contains 1 big commit with all your changes. We like to merge one commit at a time to keep the git history clean and navigable. If you don't know how to do that, we have a helpful tutorial in the official documentation https://docs.godotengine.org/en/latest/community/contributing/pr_workflow.html#the-interactive-rebase |
…ction.Dictionary. Changed StringName GetHashCode to call godot_string_name.GetHashCode instead of godot_string_name's (which was not overridden) as this otherwise leads to heap allocations when e.g. calling the indexer in a Dictionary with `StringName` type as Key.
8e3946c
to
01056f3
Compare
StringNames
in Godot C# are IEquatable, and overridesGetHashCode()
, which callsgodot_string_name.movable
's version of that method, which was not overridden. This led to heap allocations when e.g. calling the indexer in a Dictionary withStringName
type as Key.This PR just adds the (assuming) missing method, which seems to fix the issue.