Skip to content

Commit 72a8ed4

Browse files
committed
Fix 'ReferenceEqualityComparer' hash collisions
1 parent acd8948 commit 72a8ed4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Engine/ProtoCore/FFI/CLRObjectMarshaler.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Reflection;
8+
using System.Runtime.CompilerServices;
89
using System.Xml;
910
using Autodesk.DesignScript.Interfaces;
1011
using DesignScript.Builtin;
@@ -1394,8 +1395,11 @@ void core_Dispose(ProtoCore.RuntimeCore sender)
13941395
}
13951396

13961397
/// <summary>
1397-
/// This class compares two CLR objects. It is used in CLRObjectMap to
1398-
/// avoid hash collision.
1398+
/// This class compares two CLR objects using reference equality. It is used in CLRObjectMap
1399+
/// to map CLR object instances to their marshaled StackValue representations. Uses
1400+
/// RuntimeHelpers.GetHashCode to get the object's identity hash code, which ensures
1401+
/// well-distributed hash codes even when objects have value-based hash codes that collide
1402+
/// (e.g., Point objects with identical coordinates).
13991403
/// </summary>
14001404
public class ReferenceEqualityComparer: IEqualityComparer<object>
14011405
{
@@ -1406,7 +1410,7 @@ bool IEqualityComparer<object>.Equals(object x, object y)
14061410

14071411
public int GetHashCode(object obj)
14081412
{
1409-
return obj.GetHashCode();
1413+
return RuntimeHelpers.GetHashCode(obj);
14101414
}
14111415
}
14121416

0 commit comments

Comments
 (0)