From 9259ec55542b19a63e6ba888bdeb51e5bb29cf8e Mon Sep 17 00:00:00 2001 From: nmi Date: Mon, 22 Apr 2024 11:08:27 +0200 Subject: [PATCH 1/4] Verify empty dictionary is fine to deserialize --- .../Assets/Scripts/Tests/ShareTests/DictionaryTest.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs b/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs index cc4d3b078..9fc6fdc5c 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DictionaryTest.cs @@ -53,12 +53,14 @@ public void InterfaceDictionaryTest() public void ConcurrentDictionaryTest() { var cd = new ConcurrentDictionary(); + ConcurrentDictionary conv = this.Convert(cd); + conv.Count.Is(0); cd.TryAdd(1, 100); cd.TryAdd(2, 200); cd.TryAdd(3, 300); - ConcurrentDictionary conv = this.Convert(cd); + conv = this.Convert(cd); conv[1].Is(100); conv[2].Is(200); conv[3].Is(300); From 270b18e15d863c50204b8723be71e75d9d838e26 Mon Sep 17 00:00:00 2001 From: nmi Date: Mon, 22 Apr 2024 11:09:24 +0200 Subject: [PATCH 2/4] Do for loop only if any elements --- .../Scripts/MessagePack/Formatters/DictionaryFormatter.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs index 2b18677e1..fb6bb785b 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs @@ -88,6 +88,8 @@ public void Serialize(ref MessagePackWriter writer, TDictionary? value, MessageP options.Security.DepthStep(ref reader); try { + if (len > 0) + { for (int i = 0; i < len; i++) { reader.CancellationToken.ThrowIfCancellationRequested(); @@ -97,6 +99,7 @@ public void Serialize(ref MessagePackWriter writer, TDictionary? value, MessageP this.Add(dict, i, key, value, options); } + } } finally { From fb0bb13d8cd1dd6dbd229806736a25025474448e Mon Sep 17 00:00:00 2001 From: nmi Date: Mon, 22 Apr 2024 11:09:44 +0200 Subject: [PATCH 3/4] [Formatting] Respect to nesting --- .../MessagePack/Formatters/DictionaryFormatter.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs index fb6bb785b..fd753f5d4 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs @@ -90,15 +90,15 @@ public void Serialize(ref MessagePackWriter writer, TDictionary? value, MessageP { if (len > 0) { - for (int i = 0; i < len; i++) - { - reader.CancellationToken.ThrowIfCancellationRequested(); - TKey key = keyFormatter.Deserialize(ref reader, options); + for (int i = 0; i < len; i++) + { + reader.CancellationToken.ThrowIfCancellationRequested(); + TKey key = keyFormatter.Deserialize(ref reader, options); - TValue value = valueFormatter.Deserialize(ref reader, options); + TValue value = valueFormatter.Deserialize(ref reader, options); - this.Add(dict, i, key, value, options); - } + this.Add(dict, i, key, value, options); + } } } finally From 6b4beafff0ca35ae01df5b186c3aae074bd2bb65 Mon Sep 17 00:00:00 2001 From: nmi Date: Mon, 22 Apr 2024 11:10:23 +0200 Subject: [PATCH 4/4] Compute formatters only if elements are there --- .../Scripts/MessagePack/Formatters/DictionaryFormatter.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs index fd753f5d4..f1550a985 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs @@ -78,10 +78,6 @@ public void Serialize(ref MessagePackWriter writer, TDictionary? value, MessageP } else { - IFormatterResolver resolver = options.Resolver; - IMessagePackFormatter? keyFormatter = resolver.GetFormatterWithVerify(); - IMessagePackFormatter? valueFormatter = resolver.GetFormatterWithVerify(); - var len = reader.ReadMapHeader(); TIntermediate dict = this.Create(len, options); @@ -90,6 +86,9 @@ public void Serialize(ref MessagePackWriter writer, TDictionary? value, MessageP { if (len > 0) { + IFormatterResolver resolver = options.Resolver; + IMessagePackFormatter? keyFormatter = resolver.GetFormatterWithVerify(); + IMessagePackFormatter? valueFormatter = resolver.GetFormatterWithVerify(); for (int i = 0; i < len; i++) { reader.CancellationToken.ThrowIfCancellationRequested();