-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Add nullable type reflection cache #2715
Draft
israellot
wants to merge
1
commit into
JamesNK:master
Choose a base branch
from
israellot:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
Src/Newtonsoft.Json.Tests/Benchmarks/DeserializeNullableBenchmarks.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,92 @@ | ||
#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_BENCHMARKS | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using BenchmarkDotNet.Attributes; | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Tests.TestObjects; | ||
using Newtonsoft.Json.Converters; | ||
using BenchmarkDotNet.Attributes.Jobs; | ||
|
||
namespace Newtonsoft.Json.Tests.Benchmarks | ||
{ | ||
|
||
public class DeserializeNullableBenchmarks | ||
{ | ||
private static readonly string JsonText; | ||
private static readonly JsonSerializerSettings SerializerSettings; | ||
|
||
|
||
static DeserializeNullableBenchmarks() | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.Append('['); | ||
for (var i = 0; i <= 1000; i++) | ||
{ | ||
sb.Append("{\"a\":1,\"b\":\"c\",\"c\":true},"); | ||
} | ||
sb.Append(']'); | ||
|
||
JsonText = sb.ToString(); | ||
|
||
|
||
SerializerSettings = new JsonSerializerSettings(); | ||
SerializerSettings.Converters.Add(new StringEnumConverter()); | ||
} | ||
|
||
|
||
[Benchmark] | ||
public List<NullablePropertiesTestClass> DeserializeTypeWithNullables() | ||
{ | ||
return JsonConvert.DeserializeObject<List<NullablePropertiesTestClass>>(JsonText, SerializerSettings); | ||
} | ||
|
||
public class NullablePropertiesTestClass | ||
{ | ||
public int? a { get; set; } | ||
|
||
public TestEnum? b { get; set; } | ||
|
||
public bool? c { get; set; } | ||
|
||
public enum TestEnum | ||
{ | ||
a, | ||
b, | ||
c | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
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
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
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,16 +24,22 @@ | |||||
#endregion | ||||||
|
||||||
using System; | ||||||
|
||||||
using System.Collections.Generic; | ||||||
#if HAVE_BIG_INTEGER | ||||||
using System.Numerics; | ||||||
#endif | ||||||
using System.Reflection; | ||||||
using System.Collections; | ||||||
#if HAVE_CONCURRENT_COLLECTIONS | ||||||
using System.Collections.Concurrent; | ||||||
#endif | ||||||
using System.Globalization; | ||||||
using System.Text; | ||||||
using System.Runtime.CompilerServices; | ||||||
using System.Diagnostics.CodeAnalysis; | ||||||
using System.Threading; | ||||||
|
||||||
#if !HAVE_LINQ | ||||||
using Newtonsoft.Json.Utilities.LinqBridge; | ||||||
#else | ||||||
|
@@ -275,16 +281,17 @@ public static bool IsNullable(Type t) | |||||
|
||||||
public static bool IsNullableType(Type t) | ||||||
{ | ||||||
ValidationUtils.ArgumentNotNull(t, nameof(t)); | ||||||
return ReflectionUtilsCache.GetTypeInfo(t).IsNullable; | ||||||
} | ||||||
|
||||||
return (t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(Nullable<>)); | ||||||
public static Type GetUnderlyingTypeIfNullable(Type t) | ||||||
{ | ||||||
return ReflectionUtilsCache.GetTypeInfo(t).UnderlyingType; | ||||||
} | ||||||
|
||||||
public static Type EnsureNotNullableType(Type t) | ||||||
{ | ||||||
return (IsNullableType(t)) | ||||||
? Nullable.GetUnderlyingType(t)! | ||||||
: t; | ||||||
return ReflectionUtilsCache.GetTypeInfo(t).UnderlyingType; | ||||||
} | ||||||
|
||||||
public static Type EnsureNotByRefType(Type t) | ||||||
|
@@ -301,7 +308,7 @@ public static bool IsGenericDefinition(Type type, Type genericInterfaceDefinitio | |||||
return false; | ||||||
} | ||||||
|
||||||
Type t = type.GetGenericTypeDefinition(); | ||||||
Type t = ReflectionUtilsCache.GetTypeInfo(type).GenericTypeDefinition!; | ||||||
return (t == genericInterfaceDefinition); | ||||||
} | ||||||
|
||||||
|
@@ -375,7 +382,7 @@ private static bool InheritsGenericDefinitionInternal(Type type, Type genericCla | |||||
Type? currentType = type; | ||||||
do | ||||||
{ | ||||||
if (currentType.IsGenericType() && genericClassDefinition == currentType.GetGenericTypeDefinition()) | ||||||
if (currentType.IsGenericType() && genericClassDefinition == ReflectionUtilsCache.GetTypeInfo(type).GenericTypeDefinition) | ||||||
{ | ||||||
implementingType = currentType; | ||||||
return true; | ||||||
|
@@ -1109,4 +1116,61 @@ public static bool IsMethodOverridden(Type currentType, Type methodDeclaringType | |||||
return Activator.CreateInstance(type); | ||||||
} | ||||||
} | ||||||
|
||||||
internal static class ReflectionUtilsCache | ||||||
{ | ||||||
public readonly struct NullableTypeReflectionInfo | ||||||
{ | ||||||
public readonly bool IsNullable; | ||||||
|
||||||
public readonly Type UnderlyingType; | ||||||
|
||||||
public readonly Type? GenericTypeDefinition; | ||||||
|
||||||
public NullableTypeReflectionInfo( | ||||||
bool isNullable, | ||||||
Type underlyingType, | ||||||
Type? genericTypeDefinition) | ||||||
{ | ||||||
IsNullable = isNullable; | ||||||
UnderlyingType = underlyingType; | ||||||
GenericTypeDefinition = genericTypeDefinition; | ||||||
} | ||||||
} | ||||||
|
||||||
#if HAVE_CONCURRENT_COLLECTIONS | ||||||
private readonly static ConcurrentDictionary<Type, NullableTypeReflectionInfo> NullableInfoCache = new ConcurrentDictionary<Type, NullableTypeReflectionInfo>(); | ||||||
#else | ||||||
private readonly static Dictionary<Type, NullableTypeReflectionInfo> NullableInfoCache = new Dictionary<Type, NullableTypeReflectionInfo>(); | ||||||
#endif | ||||||
|
||||||
public static NullableTypeReflectionInfo GetTypeInfo(Type t) | ||||||
{ | ||||||
ValidationUtils.ArgumentNotNull(t, nameof(t)); | ||||||
|
||||||
if (!t.IsGenericType() || !t.IsValueType()) | ||||||
return new NullableTypeReflectionInfo(false, t, null); | ||||||
|
||||||
#if !HAVE_CONCURRENT_DICTIONARY | ||||||
lock(NullableInfoCache){ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#endif | ||||||
if (!NullableInfoCache.TryGetValue(t, out var info)) | ||||||
{ | ||||||
var genericTypeDefinition = t.GetGenericTypeDefinition(); | ||||||
var isNullable = (genericTypeDefinition == typeof(Nullable<>)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
var underlyingType = Nullable.GetUnderlyingType(t) ?? t; | ||||||
info = new NullableTypeReflectionInfo(isNullable, underlyingType, genericTypeDefinition); | ||||||
|
||||||
NullableInfoCache[t] = info; | ||||||
} | ||||||
|
||||||
return info; | ||||||
|
||||||
#if !HAVE_CONCURRENT_DICTIONARY | ||||||
} | ||||||
#endif | ||||||
|
||||||
} | ||||||
} | ||||||
|
||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License