From 5a58d2051f6cdb8be138fe554e7b80c25be8eca6 Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:47:44 +0100 Subject: [PATCH] use a pre-determined list capacity, if available --- M6502/M6502.Symbols/IdentifiableSection.cs | 5 +++-- M6502/M6502.Symbols/Parser.cs | 2 ++ M6502/M6502.Symbols/Section.cs | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/M6502/M6502.Symbols/IdentifiableSection.cs b/M6502/M6502.Symbols/IdentifiableSection.cs index e425016..d792ebe 100644 --- a/M6502/M6502.Symbols/IdentifiableSection.cs +++ b/M6502/M6502.Symbols/IdentifiableSection.cs @@ -38,16 +38,17 @@ protected List TakeReferences(string key, List? from) where T : IdentifiableSection { ArgumentNullException.ThrowIfNull(from); - List returned = []; var ids = this.MaybeTakeMultiple(key); if (ids != null) { + var returned = new List(ids.Count); foreach (var id in ids) { returned.Add(from[id]); } + return returned; } - return returned; + return []; } #endregion diff --git a/M6502/M6502.Symbols/Parser.cs b/M6502/M6502.Symbols/Parser.cs index 555ff5d..499ab7d 100644 --- a/M6502/M6502.Symbols/Parser.cs +++ b/M6502/M6502.Symbols/Parser.cs @@ -300,6 +300,8 @@ { throw new InvalidOperationException($"Debugging section: '{key}' is unavailable"); } + Debug.Assert(into.Count == 0); + into.Capacity = parsed.Count; foreach (var element in parsed) { var id = element.Key; diff --git a/M6502/M6502.Symbols/Section.cs b/M6502/M6502.Symbols/Section.cs index a64b4a3..325fc7b 100644 --- a/M6502/M6502.Symbols/Section.cs +++ b/M6502/M6502.Symbols/Section.cs @@ -89,8 +89,8 @@ protected static List ExtractCompoundString(string value) => new(value.Split('+')); protected static List ExtractCompoundInteger(string value) { - var returned = new List(); var elements = ExtractCompoundString(value); + var returned = new List(elements.Count); foreach (var element in elements) { returned.Add(ExtractInteger(element));