andyedinborough / aenetmail

C# POP/IMAP Mail Client
370 stars 153 forks source link

Downloading messages is very ineffecient #23

Closed rix0rrr closed 12 years ago

rix0rrr commented 12 years ago

The current code for downloading IMAP messages is very inefficient: it leads to a lot of string copying, which is hurting me for large messages (with attachments).

I've refactored the code to use the StringBuilder and it's quite a lot faster now for me:

New code:

    var body = new StringBuilder();
    while (bodyremaininglen > 0) {
      var line = GetResponse();

      if (bodyremaininglen < line.Length) {
        body.Append(line, 0, bodyremaininglen);
        bodyremaininglen = 0;
      } else {
        body.Append(line).Append(Environment.NewLine);
        bodyremaininglen -= line.Length + 2;  //extra 2 for CRLF
      }
    }

Original code:

    string body = String.Empty;
    while (bodyremaininglen > 0) {
      bodies += GetResponse();
      if (bodyremaininglen < bodies.Length) {
        body += bodies.Substring(0, bodyremaininglen);
        bodyremaininglen = 0;
        bodies = bodies.Remove(0);
      } else {
        body += bodies + Environment.NewLine;
        bodyremaininglen -= bodies.Length + 2;  //extra 2 for CRLF
        bodies = "";
      }
    }
andyedinborough commented 12 years ago

Looks good. Can you make this a pull-request? Perhaps add a unit test? :]