Skip to content

Commit dac3c0c

Browse files
authored
Avoid int boxing in HashObjectMap (#4761)
Dictionary is also faster than Hashmap.
1 parent 381ed43 commit dac3c0c

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

src/Microsoft.DotNet.Wpf/src/Shared/MS/Utility/FrugalMap.cs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
using System;
66
using System.Diagnostics;
77
using System.Collections;
8+
using System.Collections.Generic;
89
using System.Windows;
910

1011
#if WINDOWS_BASE
11-
using MS.Internal.WindowsBase;
12+
using MS.Internal.WindowsBase;
1213
#elif PRESENTATION_CORE
1314
using MS.Internal.PresentationCore;
1415
#elif PRESENTATIONFRAMEWORK
@@ -1575,7 +1576,7 @@ public override FrugalMapStoreState InsertEntry(int key, Object value)
15751576
}
15761577
else
15771578
{
1578-
_entries = new Hashtable(MINSIZE);
1579+
_entries = new Dictionary<int, object>(MINSIZE);
15791580
}
15801581

15811582
_entries[key] = ((value != NullValue) && (value != null)) ? value : NullValue;
@@ -1589,9 +1590,9 @@ public override void RemoveEntry(int key)
15891590

15901591
public override Object Search(int key)
15911592
{
1592-
object value = _entries[key];
1593-
1594-
return ((value != NullValue) && (value != null)) ? value : DependencyProperty.UnsetValue;
1593+
return _entries.TryGetValue(key, out object value) && (value != NullValue) ?
1594+
value :
1595+
DependencyProperty.UnsetValue;
15951596
}
15961597

15971598
public override void Sort()
@@ -1603,7 +1604,7 @@ public override void GetKeyValuePair(int index, out int key, out Object value)
16031604
{
16041605
if (index < _entries.Count)
16051606
{
1606-
IDictionaryEnumerator myEnumerator = _entries.GetEnumerator();
1607+
Dictionary<int, object>.Enumerator myEnumerator = _entries.GetEnumerator();
16071608

16081609
// Move to first valid value
16091610
myEnumerator.MoveNext();
@@ -1612,15 +1613,12 @@ public override void GetKeyValuePair(int index, out int key, out Object value)
16121613
{
16131614
myEnumerator.MoveNext();
16141615
}
1615-
key = (int)myEnumerator.Key;
1616-
if ((myEnumerator.Value != NullValue) && (myEnumerator.Value != null))
1617-
{
1618-
value = myEnumerator.Value;
1619-
}
1620-
else
1621-
{
1622-
value = DependencyProperty.UnsetValue;
1623-
}
1616+
1617+
KeyValuePair<int, object> current = myEnumerator.Current;
1618+
key = current.Key;
1619+
value = (current.Value != NullValue) ?
1620+
current.Value :
1621+
DependencyProperty.UnsetValue;
16241622
}
16251623
else
16261624
{
@@ -1632,22 +1630,13 @@ public override void GetKeyValuePair(int index, out int key, out Object value)
16321630

16331631
public override void Iterate(ArrayList list, FrugalMapIterationCallback callback)
16341632
{
1635-
IDictionaryEnumerator myEnumerator = _entries.GetEnumerator();
1636-
1637-
while (myEnumerator.MoveNext())
1633+
foreach (KeyValuePair<int, object> entry in _entries)
16381634
{
1639-
int key = (int)myEnumerator.Key;
1640-
object value;
1641-
if ((myEnumerator.Value != NullValue) && (myEnumerator.Value != null))
1642-
{
1643-
value = myEnumerator.Value;
1644-
}
1645-
else
1646-
{
1647-
value = DependencyProperty.UnsetValue;
1648-
}
1635+
object value = (entry.Value != NullValue) ?
1636+
entry.Value :
1637+
DependencyProperty.UnsetValue;
16491638

1650-
callback(list, key, value);
1639+
callback(list, entry.Key, value);
16511640
}
16521641
}
16531642

@@ -1674,7 +1663,7 @@ public override int Count
16741663
// two cases we insert NullValue instead of null.
16751664
private static object NullValue = new object();
16761665

1677-
internal Hashtable _entries;
1666+
internal Dictionary<int, object> _entries;
16781667
}
16791668

16801669
[FriendAccessAllowed]

0 commit comments

Comments
 (0)