Skip to content

Commit f4ec8c3

Browse files
committed
Use runtime JIT capability detection for built-in serializer logics.
1 parent a7f5afc commit f4ec8c3

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

src/MsgPack/Serialization/CollectionSerializers/DictionaryMessagePackSerializer`3.cs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,36 @@ protected DictionaryMessagePackSerializer( SerializationContext ownerContext, Po
8383
/// <returns>The count of the <paramref name="dictionary"/>.</returns>
8484
protected override int GetCount( TDictionary dictionary )
8585
{
86-
#if ( !UNITY ) || AOT_CHECK
86+
#if !NETSTANDARD2_0
8787
return dictionary.Count;
88-
#else
88+
#else // NETSTANDARD2_0
89+
if ( SerializerOptions.CanEmit )
90+
{
91+
return this.GetCountCore( dictionary );
92+
}
93+
else
94+
{
95+
return this.GetCountCoreAotSafe( dictionary );
96+
}
97+
#endif // !NETSTANDARD2_0
98+
}
99+
100+
#if NETSTANDARD2_0
101+
102+
private int GetCountCore( TDictionary dictionary )
103+
{
104+
return dictionary.Count;
105+
}
106+
107+
private int GetCountCoreAotSafe( TDictionary dictionary )
108+
{
89109
// .constraind call for TDictionary.get_Count/TDictionary.GetEnumerator() causes AOT error.
90110
// So use cast and invoke as normal call (it might cause boxing, but most collection should be reference type).
91111
return ( dictionary as IDictionary<TKey, TValue> ).Count;
92-
#endif // ( !UNITY ) || AOT_CHECK
93112
}
94113

114+
#endif // NETSTANDARD2_0
115+
95116
/// <summary>
96117
/// Adds the deserialized item to the collection on <typeparamref name="TDictionary"/> specific manner
97118
/// to implement <see cref="DictionaryMessagePackSerializerBase{TDictionary,TKey,TValue}.UnpackToCore(MsgPack.Unpacker,TDictionary)"/>.
@@ -104,14 +125,36 @@ protected override int GetCount( TDictionary dictionary )
104125
/// </exception>
105126
protected override void AddItem( TDictionary dictionary, TKey key, TValue value )
106127
{
107-
#if ( !UNITY && !XAMARIN ) || AOT_CHECK
128+
#if !NETSTANDARD2_0
108129
dictionary.Add( key, value );
109-
#else
130+
#else // !NETSTANDARD2_0
131+
if ( SerializerOptions.CanEmit)
132+
{
133+
this.AddItemCore( dictionary, key, value );
134+
}
135+
else
136+
{
137+
this.AddItemCoreAotSafe( dictionary, key, value );
138+
}
139+
#endif // !NETSTANDARD2_0
140+
}
141+
142+
#if NETSTANDARD2_0
143+
144+
private void AddItemCore( TDictionary dictionary, TKey key, TValue value )
145+
{
146+
dictionary.Add( key, value );
147+
}
148+
149+
private void AddItemCoreAotSafe( TDictionary dictionary, TKey key, TValue value )
150+
{
110151
// .constraind call for TDictionary.Add causes AOT error.
111152
// So use cast and invoke as normal call (it might cause boxing, but most collection should be reference type).
112153
( dictionary as IDictionary<TKey, TValue> ).Add( key, value );
113-
#endif // ( !UNITY && !XAMARIN ) || AOT_CHECK
114154
}
155+
156+
#endif // NETSTANDARD2_0
157+
115158
}
116159

117160
#if UNITY

src/MsgPack/Serialization/ReflectionSerializers/ReflectionSerializerHelper.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,18 @@ public static Action<object, object> GetAddItem( Type targetType, CollectionTrai
213213

214214
// CreateDelegate causes AOT error.
215215
// So use reflection in AOT environment.
216-
#if !AOT || AOT_CHECK
217-
try
218-
{
219-
return collectionTraits.AddMethod.CreateDelegate( typeof( Action<TCollection, TItem> ) ) as Action<TCollection, TItem>;
220-
}
221-
catch ( ArgumentException )
216+
#if !UNITY
217+
if ( SerializerOptions.CanEmit )
222218
{
223-
#endif // !AOT || AOT_CHECK
224-
return ( collection, item ) => collectionTraits.AddMethod.InvokePreservingExceptionType( collection, item );
225-
#if !AOT || AOT_CHECK
219+
try
220+
{
221+
return collectionTraits.AddMethod.CreateDelegate( typeof( Action<TCollection, TItem> ) ) as Action<TCollection, TItem>;
222+
}
223+
catch ( ArgumentException ) { }
226224
}
227-
#endif // !AOT || AOT_CHECK
225+
#endif //! UNITY
226+
227+
return ( collection, item ) => collectionTraits.AddMethod.InvokePreservingExceptionType( collection, item );
228228
}
229229

230230
public static void GetMetadata(
@@ -632,4 +632,4 @@ public MessagePackSerializer Create( SerializationContext context, Type targetTy
632632
// ReSharper restore MemberHidesStaticFromOuterClass
633633
#endif // !UNITY
634634
}
635-
}
635+
}

src/MsgPack/Serialization/SerializerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public bool DisableRuntimeCodeGeneration
144144
}
145145
}
146146

147-
private static bool CanEmit = DetermineCanEmit();
147+
internal static readonly bool CanEmit = DetermineCanEmit();
148148

149149
[MethodImpl( MethodImplOptions.NoInlining )]
150150
private static bool DetermineCanEmit()

0 commit comments

Comments
 (0)