Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Modifying ViewDataDictionary and RouteValueDictionary to copy on write
Browse files Browse the repository at this point in the history
instead of eagerly copying.

Partial fix for #878
  • Loading branch information
pranavkm committed Aug 12, 2014
1 parent 74bb828 commit a0c6d52
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 32 deletions.
146 changes: 146 additions & 0 deletions src/Microsoft.AspNet.Mvc.Common/CopyOnWriteDictionary.cs
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Encodings.cs" />
<Compile Include="CopyOnWriteDictionary.cs" />
<Compile Include="NotNullArgument.cs" />
<Compile Include="PlatformHelper.cs" />
<Compile Include="PropertyActivator.cs" />
Expand Down
38 changes: 19 additions & 19 deletions src/Microsoft.AspNet.Mvc.Core/ViewDataDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ public ViewDataDictionary([NotNull] IModelMetadataProvider metadataProvider)
}

public ViewDataDictionary([NotNull] IModelMetadataProvider metadataProvider,
[NotNull] ModelStateDictionary modelState)
[NotNull] ModelStateDictionary modelState)
: this(metadataProvider,
modelState: modelState,
data: new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase))
{
ModelState = modelState;
TemplateInfo = new TemplateInfo();
_data = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
_metadataProvider = metadataProvider;
}

/// <summary>
Expand All @@ -45,24 +44,25 @@ public ViewDataDictionary([NotNull] ViewDataDictionary source)
/// exceptions a derived class may throw when <see cref="SetModel"/> is called.
/// </summary>
public ViewDataDictionary([NotNull] ViewDataDictionary source, object model)
: this(source.MetadataProvider)
: this(source.MetadataProvider,
new ModelStateDictionary(source.ModelState),
new CopyOnWriteDictionary<string, object>(source, StringComparer.OrdinalIgnoreCase))
{
_modelMetadata = source.ModelMetadata;
TemplateInfo = new TemplateInfo(source.TemplateInfo);

foreach (var entry in source.ModelState)
{
ModelState.Add(entry.Key, entry.Value);
}

foreach (var entry in source)
{
_data.Add(entry.Key, entry.Value);
}

TemplateInfo = new TemplateInfo(source.TemplateInfo);
SetModel(model);
}

private ViewDataDictionary(IModelMetadataProvider metadataProvider,
ModelStateDictionary modelState,
IDictionary<string, object> data)
{
_metadataProvider = metadataProvider;
ModelState = modelState;
_data = data;
TemplateInfo = new TemplateInfo();
}

public object Model
{
get { return _model; }
Expand All @@ -88,7 +88,7 @@ public virtual ModelMetadata ModelMetadata
/// <summary>
/// Provider for subclasses that need it to override <see cref="ModelMetadata"/>.
/// </summary>
protected IModelMetadataProvider MetadataProvider
protected internal IModelMetadataProvider MetadataProvider
{
get { return _metadataProvider; }
}
Expand Down
10 changes: 4 additions & 6 deletions src/Microsoft.AspNet.Mvc.ModelBinding/ModelStateDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ModelStateDictionary : IDictionary<string, ModelState>
{
private readonly IDictionary<string, ModelState> _innerDictionary =
new Dictionary<string, ModelState>(StringComparer.OrdinalIgnoreCase);
private readonly IDictionary<string, ModelState> _innerDictionary;

public ModelStateDictionary()
{
_innerDictionary = new Dictionary<string, ModelState>(StringComparer.OrdinalIgnoreCase);
}

public ModelStateDictionary([NotNull] ModelStateDictionary dictionary)
{
foreach (var entry in dictionary)
{
_innerDictionary.Add(entry.Key, entry.Value);
}
_innerDictionary = new CopyOnWriteDictionary<string, ModelState>(dictionary,
StringComparer.OrdinalIgnoreCase);
}

#region IDictionary properties
Expand Down
104 changes: 104 additions & 0 deletions test/Microsoft.AspNet.Mvc.Core.Test/CopyOnWriteDictionaryTest.cs
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"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="ActionResults\RedirectToActionResultTest.cs" />
<Compile Include="ActionResults\RedirectToRouteResultTest.cs" />
<Compile Include="AntiXsrf\AntiForgeryOptionsTests.cs" />
<Compile Include="CopyOnWriteDictionaryTest.cs" />
<Compile Include="Formatters\TextPlainFormatterTests.cs" />
<Compile Include="Logging\BeginScopeContext.cs" />
<Compile Include="Logging\TestLoggerFactory.cs" />
Expand Down Expand Up @@ -111,6 +112,7 @@
<Compile Include="TypeHelperTest.cs" />
<Compile Include="UrlHelperTest.cs" />
<Compile Include="ViewComponentTests.cs" />
<Compile Include="ViewDataDictionaryTest.cs" />
<Compile Include="ViewResultTest.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
Expand Down
Loading

0 comments on commit a0c6d52

Please sign in to comment.