From f0b7882f6e7ad77e7565653ffde41c0bf94744fe Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Wed, 2 Nov 2022 09:07:22 +0100 Subject: [PATCH] ensure capacity of the arrays and dictionary in BeforeSerialize and AfterSerialize to reduce number of memory allocations --- Scripts/Node.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 704e99d..93396bf 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -396,6 +396,8 @@ namespace XNode { public void OnBeforeSerialize() { keys.Clear(); values.Clear(); + keys.Capacity = this.Count; + values.Capacity = this.Count; foreach (KeyValuePair pair in this) { keys.Add(pair.Key); values.Add(pair.Value); @@ -404,6 +406,7 @@ namespace XNode { public void OnAfterDeserialize() { this.Clear(); + this.EnsureCapacity(keys.Count); if (keys.Count != values.Count) throw new System.Exception("there are " + keys.Count + " keys and " + values.Count + " values after deserialization. Make sure that both key and value types are serializable.");