From 64d4e4cb8a39852485da02d97e6e991e5c1b672c Mon Sep 17 00:00:00 2001 From: Thomas Goulet Date: Fri, 12 Feb 2021 18:05:08 -0500 Subject: [PATCH] Replaced some Dictionaries and Hashtables with Hashsets --- .../internal/FontCache/FontResourceCache.cs | 4 +-- .../internal/Resources/ContentFileHelper.cs | 14 +++++------ .../Automation/Peers/AutomationPeer.cs | 25 ++++++++----------- .../System/Windows/Media/MediaContext.cs | 12 ++++----- .../Ribbon/Primitives/RibbonMenuItemsPanel.cs | 10 ++++---- .../Xaml/Context/ObjectWriterContext.cs | 5 ++-- .../System/Xaml/Context/XamlParserContext.cs | 8 +++--- .../System/Xaml/MS/Impl/XmlNsInfo.cs | 5 ++-- 8 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontResourceCache.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontResourceCache.cs index 5c4090a2001..8e87e644a57 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontResourceCache.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontResourceCache.cs @@ -28,10 +28,10 @@ internal static class FontResourceCache private static void ConstructFontResourceCache(Assembly entryAssembly, Dictionary> folderResourceMap) { // For entryAssembly build a set of mapping from paths to entries that describe each resource. - Dictionary contentFiles = ContentFileHelper.GetContentFiles(entryAssembly); + HashSet contentFiles = ContentFileHelper.GetContentFiles(entryAssembly); if (contentFiles != null) { - foreach (string contentFile in contentFiles.Keys) + foreach (string contentFile in contentFiles) { AddResourceToFolderMap(folderResourceMap, contentFile); } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Resources/ContentFileHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Resources/ContentFileHelper.cs index ed7474ed596..1bb227b76a2 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Resources/ContentFileHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Resources/ContentFileHelper.cs @@ -28,7 +28,7 @@ internal static bool IsContentFile(string partName) if (_contentFiles != null && _contentFiles.Count > 0) { - if (_contentFiles.ContainsKey(partName)) + if (_contentFiles.Contains(partName)) { return true; } @@ -40,9 +40,9 @@ internal static bool IsContentFile(string partName) // // Get a list of Content Files for a given Assembly. // - static internal Dictionary GetContentFiles(Assembly asm) + static internal HashSet GetContentFiles(Assembly asm) { - Dictionary contentFiles = null; + HashSet contentFiles = null; Attribute[] assemblyAttributes; @@ -53,7 +53,7 @@ static internal Dictionary GetContentFiles(Assembly asm) { // If we have no entry assembly return an empty list because // we can't have any content files. - return new Dictionary(); + return new HashSet(); } } @@ -63,20 +63,20 @@ static internal Dictionary GetContentFiles(Assembly asm) if (assemblyAttributes != null && assemblyAttributes.Length > 0) { - contentFiles = new Dictionary(assemblyAttributes.Length, StringComparer.OrdinalIgnoreCase); + contentFiles = new HashSet(assemblyAttributes.Length, StringComparer.OrdinalIgnoreCase); for (int i=0; i _contentFiles; + private static HashSet _contentFiles; } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/AutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/AutomationPeer.cs index d849acd5c65..c0517836bde 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/AutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/AutomationPeer.cs @@ -1761,7 +1761,7 @@ internal void UpdateChildrenInternal(int invalidateLimit) { List oldChildren = _children; List addedChildren = null; - Hashtable ht = null; + HashSet hs = null; _childrenValid = false; EnsureChildren(); @@ -1770,14 +1770,13 @@ internal void UpdateChildrenInternal(int invalidateLimit) if (!EventMap.HasRegisteredEvent(AutomationEvents.StructureChanged)) return; - //store old children in a hashtable + //store old children in a hashset if(oldChildren != null) { - ht = new Hashtable(); + hs = new HashSet(); for(int count = oldChildren.Count, i = 0; i < count; i++) { - if(!ht.Contains(oldChildren[i])) - ht.Add(oldChildren[i], null); + hs.Add(oldChildren[i]); } } @@ -1790,9 +1789,9 @@ internal void UpdateChildrenInternal(int invalidateLimit) for(int count = _children.Count, i = 0; i < count; i++) { AutomationPeer child = _children[i]; - if(ht != null && ht.ContainsKey(child)) + if(hs != null && hs.Contains(child)) { - ht.Remove(child); //same child, nothing to notify + hs.Remove(child); //same child, nothing to notify } else { @@ -1808,9 +1807,9 @@ internal void UpdateChildrenInternal(int invalidateLimit) } } - //now the ht only has "removed" children. If the count does not yet + //now the hs only has "removed" children. If the count does not yet //calls for "bulk" notification, use per-child notification, otherwise use "bulk" - int removedCount = (ht == null ? 0 : ht.Count); + int removedCount = (hs == null ? 0 : hs.Count); if(removedCount + addedCount > invalidateLimit) //bilk invalidation { @@ -1842,11 +1841,9 @@ internal void UpdateChildrenInternal(int invalidateLimit) IRawElementProviderSimple provider = ProviderFromPeerNoDelegation(this); if (provider != null) { - //ht contains removed children by now - foreach (object key in ht.Keys) + //hs contains removed children by now + foreach (AutomationPeer removedChild in hs) { - AutomationPeer removedChild = (AutomationPeer)key; - int[] rid = removedChild.GetRuntimeId(); AutomationInteropProvider.RaiseStructureChangedEvent( @@ -1857,7 +1854,7 @@ internal void UpdateChildrenInternal(int invalidateLimit) } if (addedCount > 0) { - //ht contains removed children by now + //hs contains removed children by now foreach (AutomationPeer addedChild in addedChildren) { //for children added, provider is the child itself diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs index 832a8ec06e9..becac4581ab 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs @@ -223,7 +223,7 @@ internal MediaContext(Dispatcher dispatcher) _contextGuid = Guid.NewGuid(); // Create a dictionary in which we manage the CompositionTargets. - _registeredICompositionTargets = new Dictionary(); + _registeredICompositionTargets = new HashSet(); _renderModeMessage = new DispatcherOperationCallback(InvalidateRenderMode); @@ -367,7 +367,7 @@ private object InvalidateRenderMode(object dontCare) { Debug.Assert(CheckAccess()); - foreach (ICompositionTarget target in _registeredICompositionTargets.Keys) + foreach (ICompositionTarget target in _registeredICompositionTargets) { HwndTarget hwndTarget = target as HwndTarget; @@ -1441,7 +1441,7 @@ public virtual void Dispose() // First make a copy of the dictionarys contents, because ICompositionTarget.Dispose modifies this collection. ICompositionTarget[] registeredVTs = new ICompositionTarget[_registeredICompositionTargets.Count]; - _registeredICompositionTargets.Keys.CopyTo(registeredVTs, 0); + _registeredICompositionTargets.CopyTo(registeredVTs, 0); // Iterate through the ICompositionTargets and dispose them. Be careful, ICompositionTarget.Dispose // removes the ICompositionTargets from the Dictionary. This is why we don't iterate the Dictionary directly. @@ -1519,7 +1519,7 @@ private void RegisterICompositionTargetInternal(ICompositionTarget iv) iv.AddRefOnChannel(channelSet.Channel, channelSet.OutOfBandChannel); } - _registeredICompositionTargets.Add(iv, null); // We use the dictionary just as a set. + _registeredICompositionTargets.Add(iv); } /// @@ -2067,7 +2067,7 @@ private void Render(ICompositionTarget resizedCompositionTarget) // ---------------------------------------------------------------- // 1) Render each registered ICompositionTarget to finish up the batch. - foreach (ICompositionTarget registeredTarget in _registeredICompositionTargets.Keys) + foreach (ICompositionTarget registeredTarget in _registeredICompositionTargets) { DUCE.ChannelSet channelSet; channelSet.Channel = _channelManager.Channel; @@ -2727,7 +2727,7 @@ internal DUCE.ChannelSet GetChannels() /// /// Set of ICompositionTargets that are currently registered with the MediaSystem; /// - private Dictionary _registeredICompositionTargets; + private HashSet _registeredICompositionTargets; /// /// This are the the permissions the Context has to access Visual APIs. diff --git a/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/Primitives/RibbonMenuItemsPanel.cs b/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/Primitives/RibbonMenuItemsPanel.cs index df95aad9bfd..9048ea1043e 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/Primitives/RibbonMenuItemsPanel.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/Primitives/RibbonMenuItemsPanel.cs @@ -219,7 +219,7 @@ protected override Size ArrangeOverride(Size finalSize) // Divide remainingHeight equally among starLayoutProviders remainingHeight = finalSize.Height - totalDesiredHeight; - Dictionary starLayoutTargets = GetStarLayoutProviderTargets(); + HashSet starLayoutTargets = GetStarLayoutProviderTargets(); if (DoubleUtil.GreaterThan(remainingHeight, 0.0) && starLayoutTargets.Count > 0) { surplusHeight = remainingHeight / starLayoutTargets.Count; @@ -230,7 +230,7 @@ protected override Size ArrangeOverride(Size finalSize) for (int i = 0; i < children.Count; i++) { UIElement child = children[i]; - if (starLayoutTargets.ContainsKey(child)) + if (starLayoutTargets.Contains(child)) { // if the child is a StarLayoutProvider, give it the surplusHeight. double availableHeight = child.DesiredSize.Height + surplusHeight; @@ -266,16 +266,16 @@ internal void BringIndexIntoViewInternal(int index) base.BringIndexIntoView(index); } - private Dictionary GetStarLayoutProviderTargets() + private HashSet GetStarLayoutProviderTargets() { - Dictionary targets = new Dictionary(); + HashSet targets = new HashSet(); foreach (IProvideStarLayoutInfoBase starProvider in _registeredStarLayoutProviders) { UIElement starLayoutTarget = starProvider.TargetElement; if (starLayoutTarget != null) { - targets.Add(starLayoutTarget, null); + targets.Add(starLayoutTarget); } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs index 9506bda9d12..6057278ab35 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs @@ -258,7 +258,7 @@ public override string FindNamespaceByPrefix(string prefix) public override IEnumerable GetNamespacePrefixes() { ObjectWriterFrame frame = _stack.CurrentFrame; - Dictionary keys = new Dictionary(); + HashSet keys = new HashSet(); while (frame.Depth > 0) { @@ -266,9 +266,8 @@ public override IEnumerable GetNamespacePrefixes() { foreach (NamespaceDeclaration namespaceDeclaration in frame.GetNamespacePrefixes()) { - if (!keys.ContainsKey(namespaceDeclaration.Prefix)) + if (keys.Add(namespaceDeclaration.Prefix)) { - keys.Add(namespaceDeclaration.Prefix, null); yield return namespaceDeclaration; } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlParserContext.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlParserContext.cs index ef20b24da16..b6ce5d91b0d 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlParserContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlParserContext.cs @@ -78,16 +78,15 @@ public override string FindNamespaceByPrefix(String prefix) public override IEnumerable GetNamespacePrefixes() { XamlParserFrame frame = _stack.CurrentFrame; - Dictionary keys = new Dictionary(); + HashSet keys = new HashSet(); while (frame.Depth > 0) { if (frame._namespaces != null) { foreach (NamespaceDeclaration namespaceDeclaration in frame.GetNamespacePrefixes()) { - if (!keys.ContainsKey(namespaceDeclaration.Prefix)) + if (keys.Add(namespaceDeclaration.Prefix)) { - keys.Add(namespaceDeclaration.Prefix, null); yield return namespaceDeclaration; } } @@ -99,9 +98,8 @@ public override IEnumerable GetNamespacePrefixes() { foreach (KeyValuePair kvp in _prescopeNamespaces) { - if (!keys.ContainsKey(kvp.Key)) + if (keys.Add(kvp.Key)) { - keys.Add(kvp.Key, null); yield return new NamespaceDeclaration(kvp.Value, kvp.Key); } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/XmlNsInfo.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/XmlNsInfo.cs index aaa1d003120..d30ddbe68d8 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/XmlNsInfo.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/XmlNsInfo.cs @@ -436,7 +436,7 @@ public NamespaceComparer(XmlNsInfo nsInfo, Assembly assembly) // Calculate the subsume count upfront, since this also serves as our cycle detection _subsumeCount = new Dictionary(nsInfo.OldToNewNs.Count); - Dictionary visited = new Dictionary(); + HashSet visited = new HashSet(); // for every XmlnsCompatAttribute foreach (string newNs in nsInfo.OldToNewNs.Values) @@ -447,11 +447,10 @@ public NamespaceComparer(XmlNsInfo nsInfo, Assembly assembly) string ns = newNs; do { - if (visited.ContainsKey(ns)) + if (!visited.Add(ns)) { throw new XamlSchemaException(SR.Get(SRID.XmlnsCompatCycle, assembly.FullName, ns)); } - visited.Add(ns, null); IncrementSubsumeCount(ns); ns = GetNewNs(ns); }