- Notifications
You must be signed in to change notification settings - Fork7.7k
Fix PSMethodInvocationConstraints.GetHashCode method#24965
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation
This comment was marked as duplicate.
This comment was marked as duplicate.
Uh oh!
There was an error while loading.Please reload this page.
daxian-dbw commentedFeb 11, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@crazyjncsu Thank you for reporting this issue! [UPDATE] I reverted the code back to the original hash code calculation with the addition of |
/azp run |
Azure Pipelines successfully started running 2 pipeline(s). |
Looks great for my purposes ;) |
iSazonov commentedFeb 12, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@daxian-dbw Could you please explain why we cannot use HashCode.Combine() with SequenceGetHashCode() as@crazyjncsu proposed? I think old custom algorithm is not best choice. Or are you thinking about performance? |
// algorithm based on https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode | ||
unchecked | ||
{ | ||
int result = 61; | ||
result = result * 397 + (MethodTargetType != null ? MethodTargetType.GetHashCode() : 0); | ||
result = result * 397 + ParameterTypes.SequenceGetHashCode(); | ||
result = result * 397 + GenericTypeParameters.SequenceGetHashCode(); | ||
return result; | ||
} |
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.
I would go with this since it seems to be the preferred API now. But I also don't know enough about the specifics of hashcodes to know why one is or isn't better (partially why I'd personally stick to the API that does it "properly" for you)
// algorithm based on https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode | |
unchecked | |
{ | |
intresult=61; | |
result=result*397+(MethodTargetType!=null?MethodTargetType.GetHashCode():0); | |
result=result*397+ParameterTypes.SequenceGetHashCode(); | |
result=result*397+GenericTypeParameters.SequenceGetHashCode(); | |
returnresult; | |
} | |
HashCodehashCode=new(); | |
hashCode.Add(MethodTargetType); | |
if(ParameterTypesis notnull) | |
{ | |
foreach(TypeparameterTypeinParameterTypes) | |
{ | |
hashCode.Add(parameterType); | |
} | |
} | |
else | |
{ | |
hashCode.Add(0); | |
} | |
if(GenericTypeParametersisnull) | |
{ | |
hashCode.Add(0); | |
returnhashCode.ToHashCode(); | |
} | |
foreach(TypegenericParameterinGenericTypeParameters) | |
{ | |
hashCode.Add(genericParameter); | |
} | |
returnhashCode.ToHashCode(); |
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.
HashCode.Combine
callsGetHashCode
on the passed-in values (seeits source code here), butInt32.GetHashCode()
simply returns the integer itself, soHashCode.Combine(MethodTargetType, ParameterTypes.SequenceGetHashCode(), GenericTypeParameters.SequenceGetHashCode())
should work as expected.
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.
but Int32.GetHashCode() simply returns the integer itself
Also given this, I guess we now can replace the utility methodUtils.CombineHashCodes
in PowerShell withHashCode.Combine
.
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.
But I also don't know enough about the specifics of hashcodes to know why one is or isn't better (partially why I'd personally stick to the API that does it "properly" for you)
Better:
- Less collisions.
- Random seed per process (protects against cache attacks).
daxian-dbw commentedFeb 12, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
My bad. I missed the fact that I have removed my commit. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
14fb6f2
intoPowerShell:masterUh oh!
There was an error while loading.Please reload this page.
microsoft-github-policy-servicebot commentedFeb 13, 2025 • edited by unfurl-linksbot
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by unfurl-linksbot
Uh oh!
There was an error while loading.Please reload this page.
📣 Hey@crazyjncsu, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗https://aka.ms/PSRepoFeedback |
iSazonov commentedFeb 13, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@crazyjncsu Thanks for you contribution! @ArmaanMcleod Have you an interest to migrate the SequenceGetHashCode() method to new HashCode.Add() API? |
There is a difference between GetHashCode for array object itself and cumulative GetHashCode based on hashes of the array members.
@PowerShell/powershell-maintainers triage decision - wait until more data from a 7.5 release |
There is a difference between GetHashCode for array object itself and cumulative GetHashCode based on hashes of the array members.
Uh oh!
There was an error while loading.Please reload this page.
PR Summary
Fix#24964
PR Context
The issue comes from#12412. The bottom line is that we need to calculate the hash forall array members, not for thearray object itself.
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.- [ ] Issue filed:
(which runs in a different PS Host).