Skip to content

Commit 6933285

Browse files
authored
Using HashSet instead of Dictionary multilpe times (#96573)
1 parent a0e3011 commit 6933285

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/libraries/System.IO.Packaging/src/System/IO/Packaging/XmlCompatibilityReader.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ private sealed class ProcessContentSet
17271727
private bool _all;
17281728
private readonly string _namespaceName;
17291729
private readonly XmlCompatibilityReader _reader;
1730-
private Dictionary<string, object?>? _names;
1730+
private HashSet<string>? _names;
17311731

17321732
public ProcessContentSet(string namespaceName, XmlCompatibilityReader reader)
17331733
{
@@ -1737,7 +1737,7 @@ public ProcessContentSet(string namespaceName, XmlCompatibilityReader reader)
17371737

17381738
public bool ShouldProcessContent(string elementName)
17391739
{
1740-
return _all || (_names != null && _names.ContainsKey(elementName));
1740+
return _all || (_names != null && _names.Contains(elementName));
17411741
}
17421742

17431743
public void Add(string elementName)
@@ -1767,8 +1767,8 @@ public void Add(string elementName)
17671767
}
17681768
else
17691769
{
1770-
_names ??= new Dictionary<string, object?>();
1771-
_names[elementName] = null; // we don't care about value, just key
1770+
_names ??= new HashSet<string>();
1771+
_names.Add(elementName);
17721772
}
17731773
}
17741774
}
@@ -1778,7 +1778,7 @@ private sealed class PreserveItemSet
17781778
private bool _all;
17791779
private readonly string _namespaceName;
17801780
private readonly XmlCompatibilityReader _reader;
1781-
private Dictionary<string, string>? _names;
1781+
private HashSet<string>? _names;
17821782

17831783
public PreserveItemSet(string namespaceName, XmlCompatibilityReader reader)
17841784
{
@@ -1788,7 +1788,7 @@ public PreserveItemSet(string namespaceName, XmlCompatibilityReader reader)
17881788

17891789
public bool ShouldPreserveItem(string itemName)
17901790
{
1791-
return _all || (_names != null && _names.ContainsKey(itemName));
1791+
return _all || (_names != null && _names.Contains(itemName));
17921792
}
17931793

17941794
public void Add(string itemName)
@@ -1818,8 +1818,8 @@ public void Add(string itemName)
18181818
}
18191819
else
18201820
{
1821-
_names ??= new Dictionary<string, string>();
1822-
_names.Add(itemName, itemName);
1821+
_names ??= new HashSet<string>();
1822+
_names.Add(itemName);
18231823
}
18241824
}
18251825
}

src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpEndPointListener.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal sealed class HttpEndPointListener
4242
private readonly HttpListener _listener;
4343
private readonly IPEndPoint _endpoint;
4444
private readonly Socket _socket;
45-
private readonly Dictionary<HttpConnection, HttpConnection> _unregisteredConnections;
45+
private readonly HashSet<HttpConnection> _unregisteredConnections;
4646
private Dictionary<ListenerPrefix, HttpListener> _prefixes;
4747
private List<ListenerPrefix>? _unhandledPrefixes; // host = '*'
4848
private List<ListenerPrefix>? _allPrefixes; // host = '+'
@@ -70,7 +70,7 @@ public HttpEndPointListener(HttpListener listener, IPAddress addr, int port, boo
7070
Accept(args);
7171

7272
_prefixes = new Dictionary<ListenerPrefix, HttpListener>();
73-
_unregisteredConnections = new Dictionary<HttpConnection, HttpConnection>();
73+
_unregisteredConnections = new HashSet<HttpConnection>();
7474
}
7575

7676
internal HttpListener Listener
@@ -131,7 +131,7 @@ private void ProcessAccept(SocketAsyncEventArgs args)
131131

132132
lock (_unregisteredConnections)
133133
{
134-
_unregisteredConnections[conn] = conn;
134+
_unregisteredConnections.Add(conn);
135135
}
136136
conn.BeginReadRequest();
137137
}
@@ -309,7 +309,7 @@ public void Close()
309309
lock (_unregisteredConnections)
310310
{
311311
// Clone the list because RemoveConnection can be called from Close
312-
var connections = new List<HttpConnection>(_unregisteredConnections.Keys);
312+
var connections = new List<HttpConnection>(_unregisteredConnections);
313313

314314
foreach (HttpConnection c in connections)
315315
c.Close(true);

src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private enum InitInputType
260260
private long _charactersFromEntities;
261261

262262
// All entities that are currently being processed
263-
private Dictionary<IDtdEntityInfo, IDtdEntityInfo>? _currentEntities;
263+
private HashSet<IDtdEntityInfo>? _currentEntities;
264264

265265
// DOM helpers
266266
private bool _disableUndeclaredEntityCheck;
@@ -8062,7 +8062,7 @@ private void RegisterEntity(IDtdEntityInfo entity)
80628062
// check entity recursion
80638063
if (_currentEntities != null)
80648064
{
8065-
if (_currentEntities.ContainsKey(entity))
8065+
if (_currentEntities.Contains(entity))
80668066
{
80678067
Throw(entity.IsParameterEntity ? SR.Xml_RecursiveParEntity : SR.Xml_RecursiveGenEntity, entity.Name,
80688068
_parsingStatesStack![_parsingStatesStackTop].LineNo, _parsingStatesStack[_parsingStatesStackTop].LinePos);
@@ -8076,9 +8076,9 @@ private void RegisterEntity(IDtdEntityInfo entity)
80768076
// register entity for recursion checkes
80778077
if (entity != null)
80788078
{
8079-
_currentEntities ??= new Dictionary<IDtdEntityInfo, IDtdEntityInfo>();
8079+
_currentEntities ??= new HashSet<IDtdEntityInfo>();
80808080

8081-
_currentEntities.Add(entity, entity);
8081+
_currentEntities.Add(entity);
80828082
}
80838083
}
80848084

0 commit comments

Comments
 (0)