Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dotNET Platform
runtime
提交
3e03eda5
R
runtime
项目概览
dotNET Platform
/
runtime
大约 1 年 前同步成功
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
runtime
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
3e03eda5
编写于
8月 08, 2022
作者:
E
Eirik Tsarpalis
提交者:
GitHub
8月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Recoup some of the perf losses in cold start serialization. (#73497)
上级
4734ee0a
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
79 addition
and
38 deletion
+79
-38
src/libraries/System.Text.Json/src/System/ReflectionExtensions.cs
...aries/System.Text.Json/src/System/ReflectionExtensions.cs
+7
-3
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs
....Json/src/System/Text/Json/Serialization/JsonConverter.cs
+16
-0
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
...on/src/System/Text/Json/Serialization/JsonConverterOfT.cs
+13
-0
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/CustomJsonTypeInfoOfT.cs
...Text/Json/Serialization/Metadata/CustomJsonTypeInfoOfT.cs
+0
-4
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.cs
...son/Serialization/Metadata/DefaultJsonTypeInfoResolver.cs
+19
-13
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs
...em/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs
+5
-10
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
...c/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
+19
-6
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionJsonTypeInfoOfT.cs
.../Json/Serialization/Metadata/ReflectionJsonTypeInfoOfT.cs
+0
-2
未找到文件。
src/libraries/System.Text.Json/src/System/ReflectionExtensions.cs
浏览文件 @
3e03eda5
...
...
@@ -91,15 +91,19 @@ private static bool HasCustomAttributeWithName(this ICustomAttributeProvider mem
/// <summary>
/// Polyfill for BindingFlags.DoNotWrapExceptions
/// </summary>
public
static
object
?
InvokeNoWrapExceptions
(
this
MethodInfo
methodInfo
,
object
?
obj
,
object
?[]
parameters
)
public
static
object
?
CreateInstanceNoWrapExceptions
(
[
DynamicallyAccessedMembers
(
DynamicallyAccessedMemberTypes
.
NonPublicConstructors
)]
this
Type
type
,
Type
[]
parameterTypes
,
object
?[]
parameters
)
{
ConstructorInfo
ctorInfo
=
type
.
GetConstructor
(
BindingFlags
.
NonPublic
|
BindingFlags
.
Instance
,
null
,
parameterTypes
,
null
)!;
#if NETCOREAPP
return
methodInfo
.
Invoke
(
obj
,
BindingFlags
.
DoNotWrapExceptions
,
null
,
parameters
,
null
);
return
ctorInfo
.
Invoke
(
BindingFlags
.
DoNotWrapExceptions
,
null
,
parameters
,
null
);
#else
object
?
result
=
null
;
try
{
result
=
methodInfo
.
Invoke
(
obj
,
parameters
);
result
=
ctorInfo
.
Invoke
(
parameters
);
}
catch
(
TargetInvocationException
ex
)
{
...
...
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.cs
浏览文件 @
3e03eda5
...
...
@@ -69,6 +69,22 @@ public abstract partial class JsonConverter
throw
new
InvalidOperationException
();
}
[
RequiresDynamicCode
(
JsonSerializer
.
SerializationRequiresDynamicCodeMessage
)]
[
RequiresUnreferencedCode
(
JsonSerializer
.
SerializationUnreferencedCodeMessage
)]
internal
virtual
JsonTypeInfo
CreateReflectionJsonTypeInfo
(
JsonSerializerOptions
options
)
{
Debug
.
Fail
(
"Should not be reachable."
);
throw
new
InvalidOperationException
();
}
internal
virtual
JsonTypeInfo
CreateCustomJsonTypeInfo
(
JsonSerializerOptions
options
)
{
Debug
.
Fail
(
"Should not be reachable."
);
throw
new
InvalidOperationException
();
}
internal
abstract
JsonParameterInfo
CreateJsonParameterInfo
();
internal
abstract
JsonConverter
<
TTarget
>
CreateCastingConverter
<
TTarget
>();
...
...
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
浏览文件 @
3e03eda5
...
...
@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using
System.Diagnostics
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Text.Json.Serialization.Converters
;
using
System.Text.Json.Serialization.Metadata
;
...
...
@@ -67,6 +68,18 @@ public override bool CanConvert(Type typeToConvert)
internal
override
ConverterStrategy
ConverterStrategy
=>
ConverterStrategy
.
Value
;
[
RequiresDynamicCode
(
JsonSerializer
.
SerializationRequiresDynamicCodeMessage
)]
[
RequiresUnreferencedCode
(
JsonSerializer
.
SerializationUnreferencedCodeMessage
)]
internal
sealed
override
JsonTypeInfo
CreateReflectionJsonTypeInfo
(
JsonSerializerOptions
options
)
{
return
new
ReflectionJsonTypeInfo
<
T
>(
this
,
options
);
}
internal
sealed
override
JsonTypeInfo
CreateCustomJsonTypeInfo
(
JsonSerializerOptions
options
)
{
return
new
CustomJsonTypeInfo
<
T
>(
this
,
options
);
}
internal
sealed
override
JsonParameterInfo
CreateJsonParameterInfo
()
{
return
new
JsonParameterInfo
<
T
>();
...
...
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/CustomJsonTypeInfoOfT.cs
浏览文件 @
3e03eda5
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Reflection
;
using
System.Text.Json.Serialization.Converters
;
namespace
System.Text.Json.Serialization.Metadata
{
...
...
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.cs
浏览文件 @
3e03eda5
...
...
@@ -2,8 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Reflection
;
using
System.Text.Json.Reflection
;
using
System.Threading
;
...
...
@@ -85,20 +85,26 @@ public virtual JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options
[
RequiresDynamicCode
(
JsonSerializer
.
SerializationRequiresDynamicCodeMessage
)]
private
static
JsonTypeInfo
CreateJsonTypeInfo
(
Type
type
,
JsonSerializerOptions
options
)
{
s_createReflectionJsonTypeInfoMethodInfo
??=
typeof
(
DefaultJsonTypeInfoResolver
).
GetMethod
(
nameof
(
CreateReflectionJsonTypeInfo
),
BindingFlags
.
NonPublic
|
BindingFlags
.
Static
)!;
return
(
JsonTypeInfo
)
s_createReflectionJsonTypeInfoMethodInfo
.
MakeGenericMethod
(
type
)
.
InvokeNoWrapExceptions
(
null
,
new
object
[]
{
options
})!;
}
JsonTypeInfo
jsonTypeInfo
;
JsonConverter
converter
=
GetConverterForType
(
type
,
options
);
[
RequiresUnreferencedCode
(
JsonSerializer
.
SerializationUnreferencedCodeMessage
)]
[
RequiresDynamicCode
(
JsonSerializer
.
SerializationRequiresDynamicCodeMessage
)]
private
static
JsonTypeInfo
<
T
>
CreateReflectionJsonTypeInfo
<
T
>(
JsonSerializerOptions
options
)
{
JsonConverter
converter
=
GetConverterForType
(
typeof
(
T
),
options
);
return
new
ReflectionJsonTypeInfo
<
T
>(
converter
,
options
);
}
if
(
converter
.
TypeToConvert
==
type
)
{
// For performance, avoid doing a reflection-based instantiation
// if the converter type matches that of the declared type.
jsonTypeInfo
=
converter
.
CreateReflectionJsonTypeInfo
(
options
);
}
else
{
Type
jsonTypeInfoType
=
typeof
(
ReflectionJsonTypeInfo
<>).
MakeGenericType
(
type
);
jsonTypeInfo
=
(
JsonTypeInfo
)
jsonTypeInfoType
.
CreateInstanceNoWrapExceptions
(
parameterTypes
:
new
Type
[]
{
typeof
(
JsonConverter
),
typeof
(
JsonSerializerOptions
)
},
parameters
:
new
object
[]
{
converter
,
options
})!;
}
private
static
MethodInfo
?
s_createReflectionJsonTypeInfoMethodInfo
;
Debug
.
Assert
(
jsonTypeInfo
.
Type
==
type
);
return
jsonTypeInfo
;
}
/// <summary>
/// Gets a list of user-defined callbacks that can be used to modify the initial contract.
...
...
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs
浏览文件 @
3e03eda5
...
...
@@ -4,7 +4,6 @@
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Reflection
;
using
System.Runtime.CompilerServices
;
using
System.Runtime.InteropServices
;
using
System.Text.Json.Reflection
;
...
...
@@ -56,7 +55,7 @@ public abstract partial class JsonTypeInfo
[
RequiresUnreferencedCode
(
JsonSerializer
.
SerializationUnreferencedCodeMessage
)]
internal
JsonPropertyInfo
CreatePropertyUsingReflection
(
Type
propertyType
)
{
JsonPropertyInfo
?
jsonPropertyInfo
;
JsonPropertyInfo
jsonPropertyInfo
;
if
(
Options
.
TryGetTypeInfoCached
(
propertyType
,
out
JsonTypeInfo
?
jsonTypeInfo
))
{
...
...
@@ -69,9 +68,10 @@ internal JsonPropertyInfo CreatePropertyUsingReflection(Type propertyType)
{
// Metadata for `propertyType` has not been registered yet.
// Use reflection to instantiate the correct JsonPropertyInfo<T>
s_createJsonPropertyInfo
??=
typeof
(
JsonTypeInfo
).
GetMethod
(
nameof
(
CreateJsonPropertyInfo
),
BindingFlags
.
NonPublic
|
BindingFlags
.
Static
)!;
jsonPropertyInfo
=
(
JsonPropertyInfo
)
s_createJsonPropertyInfo
.
MakeGenericMethod
(
propertyType
)
.
InvokeNoWrapExceptions
(
null
,
new
object
[]
{
this
,
Options
})!;
Type
propertyInfoType
=
typeof
(
JsonPropertyInfo
<>).
MakeGenericType
(
propertyType
);
jsonPropertyInfo
=
(
JsonPropertyInfo
)
propertyInfoType
.
CreateInstanceNoWrapExceptions
(
parameterTypes
:
new
Type
[]
{
typeof
(
Type
),
typeof
(
JsonTypeInfo
),
typeof
(
JsonSerializerOptions
)
},
parameters
:
new
object
[]
{
Type
,
this
,
Options
})!;
}
Debug
.
Assert
(
jsonPropertyInfo
.
PropertyType
==
propertyType
);
...
...
@@ -83,11 +83,6 @@ internal JsonPropertyInfo CreatePropertyUsingReflection(Type propertyType)
/// </summary>
private
protected
abstract
JsonPropertyInfo
CreateJsonPropertyInfo
(
JsonTypeInfo
declaringTypeInfo
,
JsonSerializerOptions
options
);
private
static
JsonPropertyInfo
CreateJsonPropertyInfo
<
T
>(
JsonTypeInfo
declaringTypeInfo
,
JsonSerializerOptions
options
)
=>
new
JsonPropertyInfo
<
T
>(
declaringTypeInfo
.
Type
,
declaringTypeInfo
,
options
);
private
static
MethodInfo
?
s_createJsonPropertyInfo
;
// AggressiveInlining used although a large method it is only called from one location and is on a hot path.
[
MethodImpl
(
MethodImplOptions
.
AggressiveInlining
)]
internal
JsonPropertyInfo
GetProperty
(
...
...
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs
浏览文件 @
3e03eda5
...
...
@@ -4,7 +4,6 @@
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Reflection
;
using
System.Runtime.CompilerServices
;
using
System.Runtime.ExceptionServices
;
using
System.Text.Json.Reflection
;
...
...
@@ -634,8 +633,6 @@ public static JsonTypeInfo<T> CreateJsonTypeInfo<T>(JsonSerializerOptions option
return
new
CustomJsonTypeInfo
<
T
>(
converter
,
options
);
}
private
static
MethodInfo
?
s_createJsonTypeInfo
;
/// <summary>
/// Creates a blank <see cref="JsonTypeInfo"/> instance.
/// </summary>
...
...
@@ -673,9 +670,25 @@ public static JsonTypeInfo CreateJsonTypeInfo(Type type, JsonSerializerOptions o
ThrowHelper
.
ThrowArgumentException_CannotSerializeInvalidType
(
nameof
(
type
),
type
,
null
,
null
);
}
s_createJsonTypeInfo
??=
typeof
(
JsonTypeInfo
).
GetMethod
(
nameof
(
CreateJsonTypeInfo
),
new
Type
[]
{
typeof
(
JsonSerializerOptions
)
})!;
return
(
JsonTypeInfo
)
s_createJsonTypeInfo
.
MakeGenericMethod
(
type
)
.
InvokeNoWrapExceptions
(
null
,
new
object
[]
{
options
})!;
JsonTypeInfo
jsonTypeInfo
;
JsonConverter
converter
=
DefaultJsonTypeInfoResolver
.
GetConverterForType
(
type
,
options
,
resolveJsonConverterAttribute
:
false
);
if
(
converter
.
TypeToConvert
==
type
)
{
// For performance, avoid doing a reflection-based instantiation
// if the converter type matches that of the declared type.
jsonTypeInfo
=
converter
.
CreateCustomJsonTypeInfo
(
options
);
}
else
{
Type
jsonTypeInfoType
=
typeof
(
CustomJsonTypeInfo
<>).
MakeGenericType
(
type
);
jsonTypeInfo
=
(
JsonTypeInfo
)
jsonTypeInfoType
.
CreateInstanceNoWrapExceptions
(
parameterTypes
:
new
Type
[]
{
typeof
(
JsonConverter
),
typeof
(
JsonSerializerOptions
)
},
parameters
:
new
object
[]
{
converter
,
options
})!;
}
Debug
.
Assert
(
jsonTypeInfo
.
Type
==
type
);
return
jsonTypeInfo
;
}
/// <summary>
...
...
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/ReflectionJsonTypeInfoOfT.cs
浏览文件 @
3e03eda5
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using
System.Collections
;
using
System.Collections.Generic
;
using
System.Diagnostics
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Reflection
;
using
System.Runtime.CompilerServices
;
using
System.Text.Json.Reflection
;
namespace
System.Text.Json.Serialization.Metadata
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录