This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifying ViewDataDictionary and RouteValueDictionary to copy on write
instead of eagerly copying. Partial fix for #878
- Loading branch information
Showing
8 changed files
with
387 additions
and
32 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
src/Microsoft.AspNet.Mvc.Common/CopyOnWriteDictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.AspNet.Mvc | ||
{ | ||
/// <summary> | ||
/// Represents a <see cref="IDictionary{TKey, TValue}"/> that defers creating a shallow copy of the source | ||
/// dictionary until a mutative operation has been performed on it. | ||
/// </summary> | ||
internal class CopyOnWriteDictionary<TKey, TValue> : IDictionary<TKey, TValue> | ||
{ | ||
private readonly IDictionary<TKey, TValue> _sourceDictionary; | ||
private readonly IEqualityComparer<TKey> _comparer; | ||
private IDictionary<TKey, TValue> _innerDictionary; | ||
|
||
public CopyOnWriteDictionary([NotNull] IDictionary<TKey, TValue> sourceDictionary, | ||
[NotNull] IEqualityComparer<TKey> comparer) | ||
{ | ||
_sourceDictionary = sourceDictionary; | ||
_comparer = comparer; | ||
} | ||
|
||
private IDictionary<TKey, TValue> ReadDictionary | ||
{ | ||
get | ||
{ | ||
return _innerDictionary ?? _sourceDictionary; | ||
} | ||
} | ||
|
||
private IDictionary<TKey, TValue> WriteDictionary | ||
{ | ||
get | ||
{ | ||
if (_innerDictionary == null) | ||
{ | ||
_innerDictionary = new Dictionary<TKey, TValue>(_sourceDictionary, _comparer); | ||
} | ||
|
||
return _innerDictionary; | ||
} | ||
} | ||
|
||
public virtual ICollection<TKey> Keys | ||
{ | ||
get | ||
{ | ||
return ReadDictionary.Keys; | ||
} | ||
} | ||
|
||
public virtual ICollection<TValue> Values | ||
{ | ||
get | ||
{ | ||
return ReadDictionary.Values; | ||
} | ||
} | ||
|
||
public virtual int Count | ||
{ | ||
get | ||
{ | ||
return ReadDictionary.Count; | ||
} | ||
} | ||
|
||
public virtual bool IsReadOnly | ||
{ | ||
get | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public virtual TValue this[TKey key] | ||
{ | ||
get | ||
{ | ||
return ReadDictionary[key]; | ||
} | ||
set | ||
{ | ||
WriteDictionary[key] = value; | ||
} | ||
} | ||
|
||
public virtual bool ContainsKey(TKey key) | ||
{ | ||
return ReadDictionary.ContainsKey(key); | ||
} | ||
|
||
public virtual void Add(TKey key, TValue value) | ||
{ | ||
WriteDictionary.Add(key, value); | ||
} | ||
|
||
public virtual bool Remove(TKey key) | ||
{ | ||
return WriteDictionary.Remove(key); | ||
} | ||
|
||
public virtual bool TryGetValue(TKey key, out TValue value) | ||
{ | ||
return ReadDictionary.TryGetValue(key, out value); | ||
} | ||
|
||
public virtual void Add(KeyValuePair<TKey, TValue> item) | ||
{ | ||
WriteDictionary.Add(item); | ||
} | ||
|
||
public virtual void Clear() | ||
{ | ||
WriteDictionary.Clear(); | ||
} | ||
|
||
public virtual bool Contains(KeyValuePair<TKey, TValue> item) | ||
{ | ||
return ReadDictionary.Contains(item); | ||
} | ||
|
||
public virtual void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) | ||
{ | ||
ReadDictionary.CopyTo(array, arrayIndex); | ||
} | ||
|
||
public bool Remove(KeyValuePair<TKey, TValue> item) | ||
{ | ||
return WriteDictionary.Remove(item); | ||
} | ||
|
||
public virtual IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() | ||
{ | ||
return ReadDictionary.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
test/Microsoft.AspNet.Mvc.Core.Test/CopyOnWriteDictionaryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. 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 Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNet.Mvc.Core | ||
{ | ||
public class CopyOnWriteDictionaryTest | ||
{ | ||
[Fact] | ||
public void ReadOperation_DelegatesToSourceDictionary_IfNoMutationsArePerformed() | ||
{ | ||
// Arrange | ||
var values = new List<object>(); | ||
var enumerator = Mock.Of<IEnumerator<KeyValuePair<string, object>>>(); | ||
var sourceDictionary = new Mock<IDictionary<string, object>>(); | ||
sourceDictionary.SetupGet(d => d.Count) | ||
.Returns(100) | ||
.Verifiable(); | ||
sourceDictionary.SetupGet(d => d.Values) | ||
.Returns(values) | ||
.Verifiable(); | ||
sourceDictionary.Setup(d => d.ContainsKey("test-key")) | ||
.Returns(value: true) | ||
.Verifiable(); | ||
sourceDictionary.Setup(d => d.GetEnumerator()) | ||
.Returns(enumerator) | ||
.Verifiable(); | ||
sourceDictionary.Setup(d => d["key2"]) | ||
.Returns("key2-value") | ||
.Verifiable(); | ||
object value; | ||
sourceDictionary.Setup(d => d.TryGetValue("different-key", out value)) | ||
.Returns(false) | ||
.Verifiable(); | ||
|
||
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary.Object, | ||
StringComparer.OrdinalIgnoreCase); | ||
|
||
// Act and Assert | ||
Assert.Equal("key2-value", copyOnWriteDictionary["key2"]); | ||
Assert.Equal(100, copyOnWriteDictionary.Count); | ||
Assert.Same(values, copyOnWriteDictionary.Values); | ||
Assert.True(copyOnWriteDictionary.ContainsKey("test-key")); | ||
Assert.Same(enumerator, copyOnWriteDictionary.GetEnumerator()); | ||
Assert.False(copyOnWriteDictionary.TryGetValue("different-key", out value)); | ||
sourceDictionary.Verify(); | ||
} | ||
|
||
[Fact] | ||
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceAValueIsChanged() | ||
{ | ||
// Arrange | ||
var values = new List<object>(); | ||
var enumerator = new List<KeyValuePair<string, object>>().GetEnumerator(); | ||
var sourceDictionary = new Dictionary<string, object> | ||
{ | ||
{ "key1", "value1" }, | ||
{ "key2", "value2" } | ||
}; | ||
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary, | ||
StringComparer.OrdinalIgnoreCase); | ||
|
||
// Act | ||
copyOnWriteDictionary["key2"] = "value3"; | ||
|
||
|
||
// Assert | ||
Assert.Equal("value2", sourceDictionary["key2"]); | ||
Assert.Equal(2, copyOnWriteDictionary.Count); | ||
Assert.Equal("value1", copyOnWriteDictionary["key1"]); | ||
Assert.Equal("value3", copyOnWriteDictionary["key2"]); | ||
} | ||
|
||
[Fact] | ||
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceDictionaryIsModified() | ||
{ | ||
// Arrange | ||
var values = new List<object>(); | ||
var enumerator = new List<KeyValuePair<string, object>>().GetEnumerator(); | ||
var sourceDictionary = new Dictionary<string, object> | ||
{ | ||
{ "key1", "value1" }, | ||
{ "key2", "value2" } | ||
}; | ||
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary, | ||
StringComparer.OrdinalIgnoreCase); | ||
|
||
// Act | ||
copyOnWriteDictionary.Add("key3", "value3"); | ||
copyOnWriteDictionary.Remove("key1"); | ||
|
||
|
||
// Assert | ||
Assert.Equal(2, sourceDictionary.Count); | ||
Assert.Equal(2, copyOnWriteDictionary.Count); | ||
Assert.Equal("value2", copyOnWriteDictionary["key2"]); | ||
Assert.Equal("value3", copyOnWriteDictionary["key3"]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.