Skip to content

Commit

Permalink
Improved initial List<IMessageSummary> capacity estimation for Fetch …
Browse files Browse the repository at this point in the history
…(IList<UniqueId>, ...)
  • Loading branch information
jstedfast committed Aug 25, 2023
1 parent 7eaca19 commit 0a73f17
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions MailKit/Net/Imap/ImapFolderFetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,19 @@ string CreateFetchCommand (IList<UniqueId> uids, IFetchRequest request, out bool
return string.Format ("UID FETCH %s {0}{1}\r\n", query, changedSince);
}

static int EstimateInitialCapacity (IList<UniqueId> uids)
{
if (uids is UniqueIdRange || uids is UniqueIdSet) {
// UniqueIdRange is likely to refer to UIDs that have not yet been assigned or have been expunged,
// so cap our maximum initial capacity to 1024 (a reasonable limit?).
return Math.Min (uids.Count, 1024);
}

// If the user supplied an exact set of UIDs, then we'll assume they all exist
// and therefore we can use the capacity of `uids` as our initial capacity.
return uids.Count;
}

/// <summary>
/// Fetches the message summaries for the specified message UIDs.
/// </summary>
Expand Down Expand Up @@ -1154,7 +1167,7 @@ public override IList<IMessageSummary> Fetch (IList<UniqueId> uids, IFetchReques
return Array.Empty<IMessageSummary> ();

var command = CreateFetchCommand (uids, request, out bool previewText);
var ctx = new FetchSummaryContext (4); // FIXME: do a better guesstimate than '4'
var ctx = new FetchSummaryContext (EstimateInitialCapacity (uids));

MessageExpunged += ctx.OnMessageExpunged;

Expand Down Expand Up @@ -1242,7 +1255,7 @@ public override async Task<IList<IMessageSummary>> FetchAsync (IList<UniqueId> u
return Array.Empty<IMessageSummary> ();

var command = CreateFetchCommand (uids, request, out bool previewText);
var ctx = new FetchSummaryContext (4); // FIXME: do a better guesstimate than '4'
var ctx = new FetchSummaryContext (EstimateInitialCapacity (uids));

MessageExpunged += ctx.OnMessageExpunged;

Expand Down

0 comments on commit 0a73f17

Please sign in to comment.