andyedinborough/aenetmail — Issue #23: Downloading messages is very ineffecient
Author: rix0rrr
Created: 2012-01-22 18:09:49+00:00
State: open

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 = "";
}
}