It's currently using a Hashtable with Int32 keys, which means every operation on the Hashtable boxes the int.  Switching to a Dictionary removes those costs.  It also lets use use TryGetValue to perform a single lookup to get a value rather than first calling ContainsKey and then using the indexer, and avoiding multiple virtual calls (most APIs on Hashtable are virtual) when we're only using the base class.
This commit is contained in:
Stephen Toub 2021-08-17 12:33:30 -04:00 коммит произвёл GitHub
Родитель 745c14933f
Коммит 3ca050a987
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 5 добавлений и 9 удалений

Просмотреть файл

@ -12,8 +12,7 @@
//
using System;
using System.Collections;
using System.Security;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
@ -145,13 +144,12 @@ namespace MS.Internal.Automation
{
if (_eventsTable == null)
{
_eventsTable = new Hashtable(20, .1f);
_eventsTable = new Dictionary<int, EventInfo>(20);
firstEvent = true;
}
if (_eventsTable.ContainsKey(idEvent))
if (_eventsTable.TryGetValue(idEvent, out EventInfo info))
{
EventInfo info = (EventInfo)_eventsTable[idEvent];
info.NumberOfListeners++;
}
else
@ -176,10 +174,8 @@ namespace MS.Internal.Automation
if (_eventsTable != null)
{
// Decrement the count of listeners for this event
if (_eventsTable.ContainsKey(idEvent))
if (_eventsTable.TryGetValue(idEvent, out EventInfo info))
{
EventInfo info = (EventInfo)_eventsTable[idEvent];
// Update or remove the entry based on remaining listeners
info.NumberOfListeners--;
if (info.NumberOfListeners <= 0)
@ -293,7 +289,7 @@ namespace MS.Internal.Automation
return null;
}
private static Hashtable _eventsTable; // key=event id, data=listener count
private static Dictionary<int, EventInfo> _eventsTable; // key=event id, data=listener count
private readonly static object _lock = new object();
}
}