-Refactor
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net46</TargetFramework>
|
||||
<TargetFrameworks>net46;netcoreapp1.1</TargetFrameworks>
|
||||
<RootNamespace>Newtonsoft.Json.TestConsole</RootNamespace>
|
||||
<AssemblyName>Newtonsoft.Json.TestConsole</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -26,7 +26,9 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using BenchmarkDotNet.Running;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Tests.Benchmarks;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.TestConsole
|
||||
{
|
||||
@@ -34,8 +36,8 @@ namespace Newtonsoft.Json.TestConsole
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string version = FileVersionInfo.GetVersionInfo(typeof(JsonConvert).Assembly.Location).FileVersion;
|
||||
Console.WriteLine("Json.NET Version: " + version);
|
||||
var attribute = (AssemblyFileVersionAttribute)typeof(JsonConvert).GetTypeInfo().Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute));
|
||||
Console.WriteLine("Json.NET Version: " + attribute.Version);
|
||||
|
||||
new BenchmarkSwitcher(new [] { typeof(LowLevelBenchmarks) }).Run(new[] { "*" });
|
||||
}
|
||||
|
||||
@@ -62,13 +62,24 @@ namespace Newtonsoft.Json.Tests.Benchmarks
|
||||
[Benchmark]
|
||||
public void DictionaryGet()
|
||||
{
|
||||
NormalDictionary.TryGetValue("one", out object _);
|
||||
NormalDictionary.TryGetValue("1", out object _);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ConcurrentDictionaryGet()
|
||||
{
|
||||
ConcurrentDictionary.TryGetValue("one", out object _);
|
||||
ConcurrentDictionary.TryGetValue("1", out object _);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ConcurrentDictionaryGetOrCreate()
|
||||
{
|
||||
ConcurrentDictionary.GetOrAdd("1", Dummy);
|
||||
}
|
||||
|
||||
private object Dummy(string arg)
|
||||
{
|
||||
throw new Exception("Should never get here.");
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
|
||||
@@ -24,9 +24,6 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
#if HAVE_CONCURRENT_DICTIONARY
|
||||
using System.Collections.Concurrent;
|
||||
#endif
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
@@ -72,7 +69,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
private static readonly object TypeContractCacheLock = new object();
|
||||
private static readonly PropertyNameTable NameTable = new PropertyNameTable();
|
||||
private static readonly ConcurrentDictionary<ResolverContractKey, JsonContract> _contractCache = new ConcurrentDictionary<ResolverContractKey, JsonContract>();
|
||||
private static Dictionary<ResolverContractKey, JsonContract> _contractCache;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CamelCasePropertyNamesContractResolver"/> class.
|
||||
@@ -99,8 +96,27 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
|
||||
// for backwards compadibility the CamelCasePropertyNamesContractResolver shares contracts between instances
|
||||
JsonContract contract;
|
||||
ResolverContractKey key = new ResolverContractKey(GetType(), type);
|
||||
return _contractCache.GetOrAdd(key, k => CreateContract(type));
|
||||
Dictionary<ResolverContractKey, JsonContract> cache = _contractCache;
|
||||
if (cache == null || !cache.TryGetValue(key, out contract))
|
||||
{
|
||||
contract = CreateContract(type);
|
||||
|
||||
// avoid the possibility of modifying the cache dictionary while another thread is accessing it
|
||||
lock (TypeContractCacheLock)
|
||||
{
|
||||
cache = _contractCache;
|
||||
Dictionary<ResolverContractKey, JsonContract> updatedCache = (cache != null)
|
||||
? new Dictionary<ResolverContractKey, JsonContract>(cache)
|
||||
: new Dictionary<ResolverContractKey, JsonContract>();
|
||||
updatedCache[key] = contract;
|
||||
|
||||
_contractCache = updatedCache;
|
||||
}
|
||||
}
|
||||
|
||||
return contract;
|
||||
}
|
||||
|
||||
internal override PropertyNameTable GetNameTable()
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
private readonly object _typeContractCacheLock = new object();
|
||||
private readonly PropertyNameTable _nameTable = new PropertyNameTable();
|
||||
|
||||
private readonly ConcurrentDictionary<Type, JsonContract> _contractCache = new ConcurrentDictionary<Type, JsonContract>();
|
||||
private readonly ThreadSafeStore<Type, JsonContract> _contractCache;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether members are being get and set using dynamic code generation.
|
||||
@@ -165,6 +165,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
#pragma warning disable 618
|
||||
DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public;
|
||||
#pragma warning restore 618
|
||||
|
||||
_contractCache = new ThreadSafeStore<Type, JsonContract>(CreateContract);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -179,7 +181,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
throw new ArgumentNullException(nameof(type));
|
||||
}
|
||||
|
||||
return _contractCache.GetOrAdd(type, CreateContract);
|
||||
return _contractCache.Get(type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !HAVE_CONCURRENT_DICTIONARY
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
internal sealed class ConcurrentDictionary<TKey, TValue>
|
||||
{
|
||||
private readonly object _lock = new object();
|
||||
private Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
|
||||
|
||||
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
|
||||
{
|
||||
TValue value;
|
||||
if (!_dictionary.TryGetValue(key, out value))
|
||||
{
|
||||
return AddValue(key, valueFactory);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private TValue AddValue(TKey key, Func<TKey, TValue> valueFactory)
|
||||
{
|
||||
TValue value = valueFactory(key);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
// double check locking
|
||||
TValue checkValue;
|
||||
if (_dictionary.TryGetValue(key, out checkValue))
|
||||
{
|
||||
return checkValue;
|
||||
}
|
||||
|
||||
Dictionary<TKey, TValue> newDictionary = new Dictionary<TKey, TValue>(_dictionary);
|
||||
newDictionary[key] = value;
|
||||
|
||||
#if HAVE_MEMORY_BARRIER
|
||||
Thread.MemoryBarrier();
|
||||
#endif
|
||||
_dictionary = newDictionary;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -24,32 +24,88 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if !HAVE_LINQ
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#endif
|
||||
#if HAVE_CONCURRENT_DICTIONARY
|
||||
using System.Collections.Concurrent;
|
||||
#endif
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal class ThreadSafeStore<TKey, TValue>
|
||||
{
|
||||
private readonly ConcurrentDictionary<TKey, TValue> _store;
|
||||
#if HAVE_CONCURRENT_DICTIONARY
|
||||
private ConcurrentDictionary<TKey, TValue> _concurrentStore;
|
||||
#else
|
||||
private readonly object _lock = new object();
|
||||
private Dictionary<TKey, TValue> _store;
|
||||
#endif
|
||||
private readonly Func<TKey, TValue> _creator;
|
||||
|
||||
public ThreadSafeStore(Func<TKey, TValue> creator)
|
||||
{
|
||||
if (creator == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(creator));
|
||||
}
|
||||
ValidationUtils.ArgumentNotNull(creator, nameof(creator));
|
||||
|
||||
_creator = creator;
|
||||
_store = new ConcurrentDictionary<TKey, TValue>();
|
||||
#if HAVE_CONCURRENT_DICTIONARY
|
||||
_concurrentStore = new ConcurrentDictionary<TKey, TValue>();
|
||||
#else
|
||||
_store = new Dictionary<TKey, TValue>();
|
||||
#endif
|
||||
}
|
||||
|
||||
public TValue Get(TKey key)
|
||||
{
|
||||
return _store.GetOrAdd(key, _creator);
|
||||
#if HAVE_CONCURRENT_DICTIONARY
|
||||
return _concurrentStore.GetOrAdd(key, _creator);
|
||||
#else
|
||||
TValue value;
|
||||
if (!_store.TryGetValue(key, out value))
|
||||
{
|
||||
return AddValue(key);
|
||||
}
|
||||
|
||||
return value;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !HAVE_CONCURRENT_DICTIONARY
|
||||
private TValue AddValue(TKey key)
|
||||
{
|
||||
TValue value = _creator(key);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_store == null)
|
||||
{
|
||||
_store = new Dictionary<TKey, TValue>();
|
||||
_store[key] = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
// double check locking
|
||||
TValue checkValue;
|
||||
if (_store.TryGetValue(key, out checkValue))
|
||||
{
|
||||
return checkValue;
|
||||
}
|
||||
|
||||
Dictionary<TKey, TValue> newStore = new Dictionary<TKey, TValue>(_store);
|
||||
newStore[key] = value;
|
||||
|
||||
#if HAVE_MEMORY_BARRIER
|
||||
Thread.MemoryBarrier();
|
||||
#endif
|
||||
_store = newStore;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user