Skip to content

Commit

Permalink
micro optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Nov 22, 2023
1 parent 65ec713 commit 5fe15ed
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion MailKit/Net/Imap/ImapFolderFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public override Task<IList<int>> StoreAsync (IList<int> indexes, IStoreFlagsRequ
return StoreAsync (indexes, request, true, cancellationToken);
}

void AppendLabelList (StringBuilder command, ISet<string> labels, ICollection<object> args)
void AppendLabelList (StringBuilder command, ISet<string> labels, List<object> args)
{
command.Append ('(');

Expand Down
2 changes: 1 addition & 1 deletion MailKit/Net/Pop3/AsyncPop3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ static async Task ReadLangResponseAsync (Pop3Engine engine, Pop3Command pc, Canc
if (response == ".")
break;

var tokens = response.Split (new[] { ' ' }, 2);
var tokens = response.Split (Space, 2);
if (tokens.Length != 2)
continue;

Expand Down
14 changes: 8 additions & 6 deletions MailKit/Net/Pop3/Pop3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ enum ProbedCapabilities : byte {
UIDL = (1 << 1)
}

static readonly char[] Space = new char[] { ' ' };

readonly Pop3AuthenticationSecretDetector detector = new Pop3AuthenticationSecretDetector ();
readonly MimeParser parser = new MimeParser (Stream.Null);
readonly Pop3Engine engine;
Expand Down Expand Up @@ -594,7 +596,7 @@ Task ProcessStatResponse (Pop3Engine engine, Pop3Command pc, string text, bool d
return Task.CompletedTask;

// the response should be "<count> <total size>"
var tokens = text.Split (new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var tokens = text.Split (Space, StringSplitOptions.RemoveEmptyEntries);

if (tokens.Length < 2) {
pc.Exception = CreatePop3ParseException ("Pop3 server returned an incomplete response to the STAT command: {0}", text);
Expand Down Expand Up @@ -1594,7 +1596,7 @@ static void ReadLangResponse (Pop3Engine engine, Pop3Command pc, CancellationTok
if (response == ".")
break;

var tokens = response.Split (new[] { ' ' }, 2);
var tokens = response.Split (Space, 2);
if (tokens.Length != 2)
continue;

Expand Down Expand Up @@ -1796,7 +1798,7 @@ static Task ProcessUidlResponse (Pop3Engine engine, Pop3Command pc, string text,
if (pc.Status != Pop3CommandStatus.Ok)
return Task.CompletedTask;

var tokens = text.Split (new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var tokens = text.Split (Space, StringSplitOptions.RemoveEmptyEntries);
int seqid = GetExpectedSequenceId (pc);

if (tokens.Length < 2) {
Expand Down Expand Up @@ -1893,7 +1895,7 @@ public override string GetMessageUid (int index, CancellationToken cancellationT

static void ParseUidlAllResponse (Pop3Command pc, string response)
{
var tokens = response.Split (new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var tokens = response.Split (Space, StringSplitOptions.RemoveEmptyEntries);
var uids = (List<string>) pc.UserData;

if (tokens.Length < 2) {
Expand Down Expand Up @@ -2005,7 +2007,7 @@ Task ProcessListResponse (Pop3Engine pop3, Pop3Command pc, string text, bool doA
if (pc.Status != Pop3CommandStatus.Ok)
return Task.CompletedTask;

var tokens = text.Split (new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var tokens = text.Split (Space, StringSplitOptions.RemoveEmptyEntries);
int seqid = GetExpectedSequenceId (pc);

if (tokens.Length < 2) {
Expand Down Expand Up @@ -2084,7 +2086,7 @@ public override int GetMessageSize (int index, CancellationToken cancellationTok

static void ParseListAllResponse (Pop3Command pc, string response)
{
var tokens = response.Split (new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var tokens = response.Split (Space, StringSplitOptions.RemoveEmptyEntries);
var sizes = (List<int>) pc.UserData;

if (tokens.Length < 2) {
Expand Down
1 change: 0 additions & 1 deletion MailKit/Net/Proxy/HttpsProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class HttpsProxyClient : ProxyClient
#else
const SslProtocols DefaultSslProtocols = SslProtocols.Tls12 | (SslProtocols) 12288;
#endif
const int BufferSize = 4096;

/// <summary>
/// Initializes a new instance of the <see cref="T:MailKit.Net.Proxy.HttpsProxyClient"/> class.
Expand Down
7 changes: 4 additions & 3 deletions MailKit/Net/Smtp/SmtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace MailKit.Net.Smtp {
public partial class SmtpClient : MailTransport, ISmtpClient
{
static readonly byte[] EndData = Encoding.ASCII.GetBytes (".\r\n");
static readonly char[] NewLineCharacters = { '\r', '\n' };
internal static string DefaultLocalDomain;
const int MaxLineLength = 998;

Expand Down Expand Up @@ -1703,7 +1704,7 @@ static MailboxAddress GetMessageSender (MimeMessage message)
return message.From.Mailboxes.FirstOrDefault ();
}

static void AddUnique (IList<MailboxAddress> recipients, HashSet<string> unique, IEnumerable<MailboxAddress> mailboxes)
static void AddUnique (List<MailboxAddress> recipients, HashSet<string> unique, IEnumerable<MailboxAddress> mailboxes)
{
foreach (var mailbox in mailboxes) {
if (unique.Add (mailbox.Address))
Expand Down Expand Up @@ -2546,7 +2547,7 @@ string CreateExpandCommand (string alias)
if (alias.Length == 0)
throw new ArgumentException ("The alias cannot be empty.", nameof (alias));

if (alias.IndexOfAny (new [] { '\r', '\n' }) != -1)
if (alias.IndexOfAny (NewLineCharacters) != -1)
throw new ArgumentException ("The alias cannot contain newline characters.", nameof (alias));

CheckDisposed ();
Expand Down Expand Up @@ -2627,7 +2628,7 @@ string CreateVerifyCommand (string address)
if (address.Length == 0)
throw new ArgumentException ("The address cannot be empty.", nameof (address));

if (address.IndexOfAny (new [] { '\r', '\n' }) != -1)
if (address.IndexOfAny (NewLineCharacters) != -1)
throw new ArgumentException ("The address cannot contain newline characters.", nameof (address));

CheckDisposed ();
Expand Down
10 changes: 6 additions & 4 deletions MailKit/Security/SaslMechanismDigestMd5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ public static bool TryParseKeyValuePair (string text, out string key, out string
return true;
}

static readonly char[] Comma = new char[] { ',' };

public static DigestChallenge Parse (string token)
{
var challenge = new DigestChallenge ();
Expand All @@ -333,21 +335,21 @@ public static DigestChallenge Parse (string token)

switch (key.ToLowerInvariant ()) {
case "realm":
challenge.Realms = value.Split (new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
challenge.Realms = value.Split (Comma, StringSplitOptions.RemoveEmptyEntries);
break;
case "nonce":
if (challenge.Nonce != null)
throw new SaslException ("DIGEST-MD5", SaslErrorCode.InvalidChallenge, string.Format ("Invalid SASL challenge from the server: {0}", token));
challenge.Nonce = value;
break;
case "qop":
foreach (var qop in value.Split (new [] { ',' }, StringSplitOptions.RemoveEmptyEntries))
foreach (var qop in value.Split (Comma, StringSplitOptions.RemoveEmptyEntries))
challenge.Qop.Add (qop.Trim ());
break;
case "stale":
if (challenge.Stale.HasValue)
throw new SaslException ("DIGEST-MD5", SaslErrorCode.InvalidChallenge, string.Format ("Invalid SASL challenge from the server: {0}", token));
challenge.Stale = value.ToLowerInvariant () == "true";
challenge.Stale = value.Equals ("true", StringComparison.OrdinalIgnoreCase);
break;
case "maxbuf":
if (challenge.MaxBuf.HasValue || !int.TryParse (value, NumberStyles.None, CultureInfo.InvariantCulture, out maxbuf))
Expand All @@ -367,7 +369,7 @@ public static DigestChallenge Parse (string token)
case "cipher":
if (challenge.Ciphers.Count > 0)
throw new SaslException ("DIGEST-MD5", SaslErrorCode.InvalidChallenge, string.Format ("Invalid SASL challenge from the server: {0}", token));
foreach (var cipher in value.Split (new [] { ',' }, StringSplitOptions.RemoveEmptyEntries))
foreach (var cipher in value.Split (Comma, StringSplitOptions.RemoveEmptyEntries))
challenge.Ciphers.Add (cipher.Trim ());
break;
}
Expand Down
10 changes: 4 additions & 6 deletions UnitTests/BodyPartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static BodyPartBasic CreateBasic (string type, string subtype, string partSpecif
return new BodyPartBasic { ContentType = CreateContentType (type, subtype, partSpecifier) };
}

static BodyPartBasic CreateText (string type, string subtype, string partSpecifier)
static BodyPartText CreateText (string type, string subtype, string partSpecifier)
{
return new BodyPartText { ContentType = CreateContentType (type, subtype, partSpecifier) };
}
Expand All @@ -287,14 +287,12 @@ static void VerifyPartSpecifier (BodyPart part)

Assert.AreEqual (expected, part.PartSpecifier, "The part-specifier does not match for {0}", part.ContentType.MimeType);

var message = part as BodyPartMessage;
if (message != null) {
if (part is BodyPartMessage message) {
VerifyPartSpecifier (message.Body);
return;
}

var multipart = part as BodyPartMultipart;
if (multipart != null) {
if (part is BodyPartMultipart multipart) {
for (int i = 0; i < multipart.BodyParts.Count; i++)
VerifyPartSpecifier (multipart.BodyParts[i]);
return;
Expand All @@ -303,7 +301,7 @@ static void VerifyPartSpecifier (BodyPart part)

class TestVisitor : BodyPartVisitor
{
StringBuilder builder = new StringBuilder ();
readonly StringBuilder builder = new StringBuilder ();
int indent;

public override void Visit (BodyPart body)
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/MessageSortingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void TestArgumentExceptions ()
{
var messages = new List<MessageSummary> { new MessageSummary (0) };
var orderBy = new OrderBy[] { OrderBy.Subject };
var emptyOrderBy = new OrderBy[0];
var emptyOrderBy = Array.Empty<OrderBy> ();

Assert.Throws<ArgumentNullException> (() => MessageSorter.Sort ((List<MessageSummary>) null, orderBy));
Assert.Throws<ArgumentNullException> (() => MessageSorter.Sort (messages, null));
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/MessageSummaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static BodyPartBasic CreateBasic (string type, string subtype, string partSpecif
return basic;
}

static BodyPartBasic CreateText (string type, string subtype, string partSpecifier, bool attachment)
static BodyPartText CreateText (string type, string subtype, string partSpecifier, bool attachment)
{
var text = new BodyPartText { ContentType = CreateContentType (type, subtype, partSpecifier) };
if (attachment)
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/Net/Imap/ImapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static async Task GetStreamsAsyncCallback (ImapFolder folder, int index, UniqueI
{
using (var reader = new StreamReader (stream)) {
const string expected = "This is some dummy text just to make sure this is working correctly.";
var text = await reader.ReadToEndAsync ();
var text = await reader.ReadToEndAsync (cancellationToken);

Assert.AreEqual (expected, text);
}
Expand Down

0 comments on commit 5fe15ed

Please sign in to comment.