Skip to content

Commit 4c43a43

Browse files
committed
Optimize deque.GetHashCode
1 parent 7b3bb61 commit 4c43a43

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/core/IronPython.Modules/_collections.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,27 @@ public int __length_hint__() {
818818

819819
#region private members
820820

821+
private object[] GetObjectArray() {
822+
lock (_lockObj) {
823+
if (_itemCnt == 0) return [];
824+
825+
object[] arr = new object[_itemCnt];
826+
int cnt1, cnt2;
827+
if (_head >= _tail) {
828+
cnt1 = _data.Length - _head;
829+
cnt2 = _itemCnt - cnt1;
830+
} else {
831+
cnt1 = _itemCnt;
832+
cnt2 = 0;
833+
}
834+
835+
Array.Copy(_data, _head, arr, 0, cnt1);
836+
Array.Copy(_data, 0, arr, cnt1, cnt2);
837+
return arr;
838+
}
839+
}
840+
841+
821842
private void GrowArray() {
822843
// do nothing if array is already at its max length
823844
if (_data.Length == _maxLen) return;
@@ -967,7 +988,7 @@ int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) {
967988
int res;
968989
CompareUtil.Push(this);
969990
try {
970-
res = ((IStructuralEquatable)new PythonTuple(this)).GetHashCode(comparer);
991+
res = ((IStructuralEquatable)new PythonTuple(GetObjectArray())).GetHashCode(comparer);
971992
} finally {
972993
CompareUtil.Pop(this);
973994
}

0 commit comments

Comments
 (0)