From af98672b17ca17c8fbe28a981f25fa0071a49224 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Tue, 13 Feb 2018 19:52:44 +0000 Subject: [PATCH] Remove redundant WeakValueDictionary --- .../RenderTree/RenderTreeDiffBuilder.cs | 1 - .../Rendering/WeakValueDictionary.cs | 61 ------------------- 2 files changed, 62 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Blazor/Rendering/WeakValueDictionary.cs diff --git a/src/Microsoft.AspNetCore.Blazor/RenderTree/RenderTreeDiffBuilder.cs b/src/Microsoft.AspNetCore.Blazor/RenderTree/RenderTreeDiffBuilder.cs index 72685e8523..14472b136c 100644 --- a/src/Microsoft.AspNetCore.Blazor/RenderTree/RenderTreeDiffBuilder.cs +++ b/src/Microsoft.AspNetCore.Blazor/RenderTree/RenderTreeDiffBuilder.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Reflection; using Microsoft.AspNetCore.Blazor.Components; using Microsoft.AspNetCore.Blazor.Rendering; diff --git a/src/Microsoft.AspNetCore.Blazor/Rendering/WeakValueDictionary.cs b/src/Microsoft.AspNetCore.Blazor/Rendering/WeakValueDictionary.cs deleted file mode 100644 index bbea21d247..0000000000 --- a/src/Microsoft.AspNetCore.Blazor/Rendering/WeakValueDictionary.cs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Microsoft.AspNetCore.Blazor.Rendering -{ - internal class WeakValueDictionary where TValue : class - { - private IDictionary> _store - = new Dictionary>(); - private int _cullThreshold = 10; - - public bool TryGetValue(TKey key, out TValue value) - { - if (_store.TryGetValue(key, out var existingWeakRef)) - { - if (existingWeakRef.TryGetTarget(out value)) - { - return true; - } - - // Since we know it's not there, we might as well drop the entry now - _store.Remove(key); - } - - value = default(TValue); - return false; - } - - public void Add(TKey key, TValue value) - { - if (_store.TryGetValue(key, out _)) - { - throw new ArgumentException($"The given key was already present in the {nameof(WeakValueDictionary)}. Key: {key}"); - } - - _store[key] = new WeakReference(value); - CullIfApplicable(); - } - - private void CullIfApplicable() - { - if (_store.Count > _cullThreshold) - { - var itemsToRemove = _store.Where(x => !x.Value.TryGetTarget(out _)).ToList(); - foreach (var itemToRemove in itemsToRemove) - { - _store.Remove(itemToRemove.Key); - } - - if (_store.Count > (_cullThreshold / 2)) - { - _cullThreshold *= 2; - } - } - } - } -}