Skip to content

Swap Dictionary<K, V> with HashSet<ICyclicBrush> in TreeWalkProgress #10719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,51 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

/*++

Abstract:
This file implements the TreeWalkProgress
used by the Xps Serialization APIs for tracking cycles in a visual tree.
--*/
using System.Windows.Media;

namespace System.Windows.Xps.Serialization
namespace System.Windows.Xps.Serialization;

/// <summary>
/// This class is used by the Xps Serialization APIs for tracking cycles in a visual tree.
/// </summary>
internal sealed class TreeWalkProgress
{
/// <summary>
/// This class is used by the Xps Serialization APIs for tracking cycles in a visual tree.
/// </summary>
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<K,V> 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<ICyclicBrush, EmptyStruct> _cyclicBrushes = new Dictionary<ICyclicBrush, EmptyStruct>();

// 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<ICyclicBrush> _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);
}
}