1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00

ensure capacity of the arrays and dictionary in BeforeSerialize and AfterSerialize to reduce number of memory allocations

This commit is contained in:
Simon Rodriguez 2022-11-02 09:07:22 +01:00
parent 7863679da6
commit f0b7882f6e

View File

@ -396,6 +396,8 @@ namespace XNode {
public void OnBeforeSerialize() {
keys.Clear();
values.Clear();
keys.Capacity = this.Count;
values.Capacity = this.Count;
foreach (KeyValuePair<string, NodePort> 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.");