Skip to content

Commit

Permalink
Avoid allocating empty List<string>s in DomainList.ctor()
Browse files Browse the repository at this point in the history
Routes are rarely used these days, so wait until the user starts adding
domains to the list before allocating a growable list.
  • Loading branch information
jstedfast committed Aug 27, 2023
1 parent 19b73ba commit df39eb2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions MimeKit/DomainList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace MimeKit {
public class DomainList : IList<string>
{
readonly static byte[] DomainSentinels = new [] { (byte) ',', (byte) ':' };
readonly List<string> domains;
IList<string> domains;

/// <summary>
/// Initialize a new instance of the <see cref="DomainList"/> class.
Expand Down Expand Up @@ -70,7 +70,13 @@ public DomainList (IEnumerable<string> domains)
/// </remarks>
public DomainList ()
{
domains = new List<string> ();
domains = Array.Empty<string> ();
}

void EnsureAllocated ()
{
if (domains == Array.Empty<string> ())
domains = new List<string> ();
}

#region IList implementation
Expand Down Expand Up @@ -113,6 +119,7 @@ public void Insert (int index, string domain)
if (domain is null)
throw new ArgumentNullException (nameof (domain));

EnsureAllocated ();
domains.Insert (index, domain);
OnChanged ();
}
Expand Down Expand Up @@ -180,6 +187,7 @@ public void Add (string domain)
if (domain is null)
throw new ArgumentNullException (nameof (domain));

EnsureAllocated ();
domains.Add (domain);
OnChanged ();
}
Expand Down

0 comments on commit df39eb2

Please sign in to comment.