use a pre-determined list capacity, if available

This commit is contained in:
Adrian Conlon
2024-06-10 20:47:44 +01:00
parent fb79d456b0
commit 5a58d2051f
3 changed files with 6 additions and 3 deletions

View File

@@ -38,16 +38,17 @@
protected List<T> TakeReferences<T>(string key, List<T>? from) where T : IdentifiableSection
{
ArgumentNullException.ThrowIfNull(from);
List<T> returned = [];
var ids = this.MaybeTakeMultiple(key);
if (ids != null)
{
var returned = new List<T>(ids.Count);
foreach (var id in ids)
{
returned.Add(from[id]);
}
return returned;
}
return returned;
return [];
}
#endregion

View File

@@ -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;

View File

@@ -89,8 +89,8 @@
protected static List<string> ExtractCompoundString(string value) => new(value.Split('+'));
protected static List<int> ExtractCompoundInteger(string value)
{
var returned = new List<int>();
var elements = ExtractCompoundString(value);
var returned = new List<int>(elements.Count);
foreach (var element in elements)
{
returned.Add(ExtractInteger(element));