From 1958a1a28ee1ae3ff68475dc632ec23ccb55eceb Mon Sep 17 00:00:00 2001 From: bmesetovic Date: Thu, 20 Sep 2018 19:34:12 +0200 Subject: [PATCH] Reduce allocations on list deserialization (#310) * Do not capture context on async awaits * Do not capture context on async awaits (2) * Improve list allocation when deserializing lists from messages * Fixed List implementation with initial capacity * Fixed list implementation for NETMF --- netmf/NetMF/List.cs | 15 +++++++++++++++ src/Net/List.cs | 15 +++++++++++++++ src/Types/Encoder.cs | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/netmf/NetMF/List.cs b/netmf/NetMF/List.cs index f096c75f..7e24a978 100644 --- a/netmf/NetMF/List.cs +++ b/netmf/NetMF/List.cs @@ -24,5 +24,20 @@ namespace Amqp.Types /// public class List : ArrayList { + /// + /// Initializes a new instance of the class. + /// + public List() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The number of elements that the new list can initially store. + public List(int capacity) : base() + { + // NOTE: NETMF ArrayList does not have a constructor that takes an int parameter, so we cannot set initial capacity in this case + } } } diff --git a/src/Net/List.cs b/src/Net/List.cs index 8592fc71..29dd354a 100644 --- a/src/Net/List.cs +++ b/src/Net/List.cs @@ -24,5 +24,20 @@ namespace Amqp.Types /// public class List : List { + + /// + /// Initializes a new instance of the class. + /// + public List() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The number of elements that the new list can initially store. + public List(int capacity) : base(capacity) + { + } } } diff --git a/src/Types/Encoder.cs b/src/Types/Encoder.cs index 6e0c852f..8deb5e81 100644 --- a/src/Types/Encoder.cs +++ b/src/Types/Encoder.cs @@ -1346,7 +1346,7 @@ public static List ReadList(ByteBuffer buffer, byte formatCode) throw InvalidFormatCodeException(formatCode, buffer.Offset); } - List value = new List(); + List value = new List(count); for (int i = 0; i < count; ++i) { value.Add(ReadObject(buffer));