From e2cb2920e3f3224081d5e83c5304fa1647237fcb Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Sat, 22 Aug 2026 17:02:45 +0800 Subject: [PATCH] Apply changes from https://github.com/dotnet/wpf/pull/10719 Source-PR: https://github.com/dotnet/wpf/pull/10719 Source-Head-SHA: 93e465de8507d030d9b31504b08e6a6b42d88c60 --- .../Serialization/TreeWalkProgress.cs | 72 ++++++------------- 1 file changed, 21 insertions(+), 51 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/TreeWalkProgress.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/TreeWalkProgress.cs index cb6a90c77..1bd91cac1 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/TreeWalkProgress.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/TreeWalkProgress.cs @@ -2,60 +2,30 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -/*++ - - Abstract: - This file implements the TreeWalkProgress - used by the Xps Serialization APIs for tracking cycles in a visual tree. ---*/ -using System; -using System.Collections; -using System.Collections.Specialized; using System.Collections.Generic; -using System.ComponentModel; -using System.IO; -using System.Text; using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Xps.Packaging; -namespace System.Windows.Xps.Serialization +namespace System.Windows.Xps.Serialization; + +/// +/// This class is used by the Xps Serialization APIs for tracking cycles in a visual tree. +/// +internal sealed class TreeWalkProgress { - /// - /// This class is used by the Xps Serialization APIs for tracking cycles in a visual tree. - /// - internal class TreeWalkProgress - { - public bool EnterTreeWalk(ICyclicBrush brush) - { - if(this._cyclicBrushes.ContainsKey(brush)) - { - return false; - } - - this._cyclicBrushes.Add(brush, EmptyStruct.Default); - return true; - } - - public void ExitTreeWalk(ICyclicBrush brush) - { - this._cyclicBrushes.Remove(brush); - } - - public bool IsTreeWalkInProgress(ICyclicBrush brush) - { - return this._cyclicBrushes.ContainsKey(brush); - } - - // We use the keys of this dictionary to simulate a set - // We do not use HashSet to avoid a perf regression by loading System.Core.dll - // It also makes the fix easier to backport to pre .net 3.5 releases - private IDictionary _cyclicBrushes = new Dictionary(); - - // A struct that when optimized does not consume per instance heap\stack space - private struct EmptyStruct - { - public static EmptyStruct Default = new EmptyStruct(); - } + private readonly HashSet _cyclicBrushes = new(); + + public bool EnterTreeWalk(ICyclicBrush brush) + { + return _cyclicBrushes.Add(brush); + } + + public void ExitTreeWalk(ICyclicBrush brush) + { + _cyclicBrushes.Remove(brush); + } + + public bool IsTreeWalkInProgress(ICyclicBrush brush) + { + return _cyclicBrushes.Contains(brush); } }