Skip to content

Commit

Permalink
Improved performance of InternetAddressList.Parse()/TryParse()
Browse files Browse the repository at this point in the history
Avoid duplicating the parsed List<InternetAddress> returned by an
internal parsing method when packing it into an InternetAddressList.
  • Loading branch information
jstedfast committed Nov 28, 2024
1 parent 408264a commit 2ada64f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion MimeKit/InternetAddressList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace MimeKit {
[TypeConverter (typeof (InternetAddressListConverter))]
public class InternetAddressList : IList<InternetAddress>, IEquatable<InternetAddressList>, IComparable<InternetAddressList>
{
readonly List<InternetAddress> list = new List<InternetAddress> ();
readonly List<InternetAddress> list;

/// <summary>
/// Initialize a new instance of the <see cref="InternetAddressList"/> class.
Expand All @@ -71,6 +71,11 @@ public InternetAddressList (IEnumerable<InternetAddress> addresses)
if (addresses is null)
throw new ArgumentNullException (nameof (addresses));

if (addresses is IList<InternetAddress> lst)
list = new List<InternetAddress> (lst.Count);
else
list = new List<InternetAddress> ();

foreach (var address in addresses) {
address.Changed += AddressChanged;
list.Add (address);
Expand All @@ -85,6 +90,17 @@ public InternetAddressList (IEnumerable<InternetAddress> addresses)
/// </remarks>
public InternetAddressList ()
{
list = new List<InternetAddress> ();
}

// Note: This .ctor gets used by the Parse and TryParse methods and is meant to reduce duplicating an
// existing List<InternetAddress> for no reason.
InternetAddressList (List<InternetAddress> addresses)
{
list = addresses;

foreach (var address in addresses)
address.Changed += AddressChanged;
}

/// <summary>
Expand Down

0 comments on commit 2ada64f

Please sign in to comment.