Given the following code that reads email messages and parses them...
val inbox = store.getFolder("Inbox")
inbox.open(Folder.READ_WRITE)
val messages = inbox.getMessages()
for (message <- messages) {
val from = InternetAddress.toString(message.getFrom())
if (from.contains(sender)) {
message.getContent() match {
case content: String => {
parse(content) match {
case Some(reading: SiteReading) => {
readings += reading
Logger.info(s"Message added. Subject: ${message.getSubject()}");
}
case _ => Logger.warn(s"Unable to process message. Ignoring. $content");
}
message.setFlag(Flags.Flag.DELETED, true);
}
case _ => Logger.error(s"Unknown message type sent from $sender.");
}
}
}
The 7th line, case content: String =>
is really slow. I was thinking that my parse function was slow, but after profiling it at sub-millisecond it didn't really don on me that the pattern match could be the culprit. The pattern match is taking anywhere from 250-300ms.
Because javax.mail
returns an Object
from message.getContent()
, I have to test for the type.
I'm using Scala 2.10.2.