Skip to content

Commit ccc02ab

Browse files
committed
use formatter cache for TypelessObjectResolver
1 parent dddacef commit ccc02ab

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/TypelessObjectResolver.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,35 @@ private TypelessObjectResolver()
3535
/// <inheritdoc />
3636
public IMessagePackFormatter<T> GetFormatter<T>()
3737
{
38-
if (typeof(T).IsAbstract || typeof(T).IsInterface)
39-
{
40-
return new ForceTypelessFormatter<T>();
41-
}
38+
return Cache<T>.Formatter;
39+
}
4240

43-
if (typeof(T) == typeof(object))
44-
{
45-
return (IMessagePackFormatter<T>)TypelessFormatter.Instance;
46-
}
47-
else
41+
private static class Cache<T>
42+
{
43+
public static readonly IMessagePackFormatter<T> Formatter;
44+
45+
static Cache()
4846
{
49-
foreach (IFormatterResolver item in Resolvers)
47+
if (typeof(T).IsAbstract || typeof(T).IsInterface)
48+
{
49+
Formatter = new ForceTypelessFormatter<T>();
50+
}
51+
52+
if (typeof(T) == typeof(object))
5053
{
51-
IMessagePackFormatter<T> f = item.GetFormatter<T>();
52-
if (f != null)
54+
Formatter = (IMessagePackFormatter<T>)TypelessFormatter.Instance;
55+
}
56+
else
57+
{
58+
foreach (var item in Resolvers)
5359
{
54-
return f;
60+
var f = item.GetFormatter<T>();
61+
if (f != null)
62+
{
63+
Formatter = f;
64+
}
5565
}
5666
}
57-
58-
return null;
5967
}
6068
}
6169
}

src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/MessagePackSerializerTypelessTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ public void OmitAssemblyVersion()
7878
[Fact]
7979
public void SerializeInterface()
8080
{
81-
var foo = MessagePackSerializer.Typeless.DefaultOptions.Resolver.GetFormatter<TypelessAbstract>();
82-
8381
var v = new Holder() { T1 = new TypelessInterface { X = 999 }, T2 = new TypelessNonAbstract { X = 19, Y = 9999 } };
8482
var bin = MessagePackSerializer.Typeless.Serialize(v);
8583
var v2 = MessagePackSerializer.Typeless.Deserialize(bin).IsInstanceOf<Holder>();

0 commit comments

Comments
 (0)