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

Use a CacheKey struct for the ObjectCreator cache #2317

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

scottfavre
Copy link

This is a candidate fix for #2315

The implementation of Dictionary will use hash codes under the hood, but handle collisions correctly.

By using a hashcode for the dictionary key the existing code prevents the .NET class from doing the right thing.

Notes:
This may degrade performance because cache hits need to do the full Equals call to verify that it is not a hash collision.
This may degrade performance because of changes in inlining behavior.

The implementation of Dictionary will use hash codes under the hood, but handle collisions correctly. By using a hashcode for the dictionary key we're preventing the .NET class from doing the right thing.

This may degrade performance because cache hits need to do the full Equals call to verify that it is not a hash collision.

This may degrade performance because of changes in inlining behavior.

public override int GetHashCode()
{
var hashCode = new HashCode();
Copy link
Contributor

Choose a reason for hiding this comment

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

Just swinging by from a performance point, can you make the properties private fields? (The Equals would still see them) Then you could store this value after first calculation?

Copy link
Author

Choose a reason for hiding this comment

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

I've moved Type and Args to be private, readonly fields so that the compiler can do as much optimization with them as it can.

However, I don't think we'd actually get any performance boost from storing the hash code. The implementation of Dictionary already stores the hashcode on the entry struct, so each item added to the dictionary only has GetHashCode called once.

See https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs#L1754

public bool Equals(CacheKey other)
{
if (other.Args.Length != Args.Length) return false;
if (other.Type != Type) return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suspect the type would vary more often then the number of arguments. Think of all the false positives you will get with default constructors.

Copy link
Author

Choose a reason for hiding this comment

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

Good point, done.

- use private fields instead of private fields instead of public properties
- check type first instead of argument type (default constructors will all have 0 arguments, so it's not a good first check)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants