You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having HashCode directly accept spans of values would greatly simplify such code.
Calling it a "great" simplification is a bit of a stretch. You're removing what is 3-6 lines for a one liner, that is removing a for loop for something which does a for loop. There's also nuances in that Add/Combine will do more work than what you've referenced which may or may not be acceptable depending on the scenario.
Given that this is just a one liner for a for loop, I'd normally close this as not something we're interested in provided. That being said, there is some minor benefit here in that we can make the main loop avoid the redundant checking of whether position is 0, 1, 2, or 3 every iteration and provide some marginal perf speed up instead so it "might" be worth keeping open.
-- The alternative, which would still provide such a speedup, is to expose Add overloads which mirror the Combine overloads allowing the same optimizations to be manually implemented.
now you cannot distinguish between field1 = [], field2 = [1] and field1 = [1], field2 = [] - they would get the same hashcode.
For this reason, I always include the length in the hashcode first, and then all of the items - this makes the collision harder in my experience (ofc it's still possible, but not so trivially on accident by just moving elements from one field to another in the same order).
Personally, I just write this as an extension in the stuff where I need it.
@MichalPetryka, please update the proposal to focus on the HashCode.Add overloads suggested by @tannergooding, including the intended performance rationale and semantic considerations discussed above.
Activity
dotnet-policy-service commentedon Sep 8, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Sep 8, 2026
Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.
tannergooding commentedon Sep 8, 2026
Calling it a "great" simplification is a bit of a stretch. You're removing what is 3-6 lines for a one liner, that is removing a for loop for something which does a for loop. There's also nuances in that
Add/Combinewill do more work than what you've referenced which may or may not be acceptable depending on the scenario.Given that this is just a one liner for a
for loop, I'd normally close this as not something we're interested in provided. That being said, there is some minor benefit here in that we can make the main loop avoid the redundant checking of whetherpositionis0, 1, 2, or 3every iteration and provide some marginal perf speed up instead so it "might" be worth keeping open.-- The alternative, which would still provide such a speedup, is to expose
Addoverloads which mirror theCombineoverloads allowing the same optimizations to be manually implemented.hamarb123 commentedon Sep 8, 2026
this would not be an appropriate general implementation imo; consider I had something like this:
now you cannot distinguish between
field1 = [], field2 = [1]andfield1 = [1], field2 = []- they would get the same hashcode.For this reason, I always include the length in the hashcode first, and then all of the items - this makes the collision harder in my experience (ofc it's still possible, but not so trivially on accident by just moving elements from one field to another in the same order).
Personally, I just write this as an extension in the stuff where I need it.
jeffhandley commentedon Sep 9, 2026
@MichalPetryka, please update the proposal to focus on the
HashCode.Addoverloads suggested by @tannergooding, including the intended performance rationale and semantic considerations discussed above.