[CSHARP-4300] Reduce memory traffic associated with Hasher.cs Created: 19/Aug/22  Updated: 24/Aug/23

Status: Backlog
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Unknown
Reporter: Boris Dogadov Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-4292 Performance: Eliminate allocations in... Closed

 Description   

Follow up for https://jira.mongodb.org/browse/CSHARP-4292

  • Make Hasher a struct, to reduce memory traffic in associated GetHashCode.
  • Add public 

    Hasher Hash<T>(T obj) where T : struct

     to eliminate boxing for value types (BsonArray.GetHashCode, Hasher.Hash(BsonType))



 Comments   
Comment by Daniel Hegener [ 24/Aug/23 ]

Turning Hasher into a struct isn't trivial (but feasible) because of the fluent API that it contains. Passing references back to "this" will cause the struct to be copied every time.

Also, there is some more boxing-induced allocation going on in the current implementation, e.g. when passing a List<T> into 

public Hasher HashElements<T>(IEnumerable<T> sequence)

How about switching to System.HashCode altogether? This would remove all allocations and arguably provide even better hash codes.

Generated at Wed Feb 07 21:47:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.