Skip to content

Commit

Permalink
Using HashSet instead of Dictionary multilpe times (#96573)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndsvw committed Jan 7, 2024
1 parent a0e3011 commit 6933285
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ private sealed class ProcessContentSet
private bool _all;
private readonly string _namespaceName;
private readonly XmlCompatibilityReader _reader;
private Dictionary<string, object?>? _names;
private HashSet<string>? _names;

public ProcessContentSet(string namespaceName, XmlCompatibilityReader reader)
{
Expand All @@ -1737,7 +1737,7 @@ public ProcessContentSet(string namespaceName, XmlCompatibilityReader reader)

public bool ShouldProcessContent(string elementName)
{
return _all || (_names != null && _names.ContainsKey(elementName));
return _all || (_names != null && _names.Contains(elementName));
}

public void Add(string elementName)
Expand Down Expand Up @@ -1767,8 +1767,8 @@ public void Add(string elementName)
}
else
{
_names ??= new Dictionary<string, object?>();
_names[elementName] = null; // we don't care about value, just key
_names ??= new HashSet<string>();
_names.Add(elementName);
}
}
}
Expand All @@ -1778,7 +1778,7 @@ private sealed class PreserveItemSet
private bool _all;
private readonly string _namespaceName;
private readonly XmlCompatibilityReader _reader;
private Dictionary<string, string>? _names;
private HashSet<string>? _names;

public PreserveItemSet(string namespaceName, XmlCompatibilityReader reader)
{
Expand All @@ -1788,7 +1788,7 @@ public PreserveItemSet(string namespaceName, XmlCompatibilityReader reader)

public bool ShouldPreserveItem(string itemName)
{
return _all || (_names != null && _names.ContainsKey(itemName));
return _all || (_names != null && _names.Contains(itemName));
}

public void Add(string itemName)
Expand Down Expand Up @@ -1818,8 +1818,8 @@ public void Add(string itemName)
}
else
{
_names ??= new Dictionary<string, string>();
_names.Add(itemName, itemName);
_names ??= new HashSet<string>();
_names.Add(itemName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal sealed class HttpEndPointListener
private readonly HttpListener _listener;
private readonly IPEndPoint _endpoint;
private readonly Socket _socket;
private readonly Dictionary<HttpConnection, HttpConnection> _unregisteredConnections;
private readonly HashSet<HttpConnection> _unregisteredConnections;
private Dictionary<ListenerPrefix, HttpListener> _prefixes;
private List<ListenerPrefix>? _unhandledPrefixes; // host = '*'
private List<ListenerPrefix>? _allPrefixes; // host = '+'
Expand Down Expand Up @@ -70,7 +70,7 @@ public HttpEndPointListener(HttpListener listener, IPAddress addr, int port, boo
Accept(args);

_prefixes = new Dictionary<ListenerPrefix, HttpListener>();
_unregisteredConnections = new Dictionary<HttpConnection, HttpConnection>();
_unregisteredConnections = new HashSet<HttpConnection>();
}

internal HttpListener Listener
Expand Down Expand Up @@ -131,7 +131,7 @@ private void ProcessAccept(SocketAsyncEventArgs args)

lock (_unregisteredConnections)
{
_unregisteredConnections[conn] = conn;
_unregisteredConnections.Add(conn);
}
conn.BeginReadRequest();
}
Expand Down Expand Up @@ -309,7 +309,7 @@ public void Close()
lock (_unregisteredConnections)
{
// Clone the list because RemoveConnection can be called from Close
var connections = new List<HttpConnection>(_unregisteredConnections.Keys);
var connections = new List<HttpConnection>(_unregisteredConnections);

foreach (HttpConnection c in connections)
c.Close(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private enum InitInputType
private long _charactersFromEntities;

// All entities that are currently being processed
private Dictionary<IDtdEntityInfo, IDtdEntityInfo>? _currentEntities;
private HashSet<IDtdEntityInfo>? _currentEntities;

// DOM helpers
private bool _disableUndeclaredEntityCheck;
Expand Down Expand Up @@ -8062,7 +8062,7 @@ private void RegisterEntity(IDtdEntityInfo entity)
// check entity recursion
if (_currentEntities != null)
{
if (_currentEntities.ContainsKey(entity))
if (_currentEntities.Contains(entity))
{
Throw(entity.IsParameterEntity ? SR.Xml_RecursiveParEntity : SR.Xml_RecursiveGenEntity, entity.Name,
_parsingStatesStack![_parsingStatesStackTop].LineNo, _parsingStatesStack[_parsingStatesStackTop].LinePos);
Expand All @@ -8076,9 +8076,9 @@ private void RegisterEntity(IDtdEntityInfo entity)
// register entity for recursion checkes
if (entity != null)
{
_currentEntities ??= new Dictionary<IDtdEntityInfo, IDtdEntityInfo>();
_currentEntities ??= new HashSet<IDtdEntityInfo>();

_currentEntities.Add(entity, entity);
_currentEntities.Add(entity);
}
}

Expand Down

0 comments on commit 6933285

Please sign in to comment.