There's nothing quite like some justified optimization to get the heart pumping. I've been working on a system which needs to produce multipart MIME messages very quickly. It's using MIME::Lite, which, it turns out, isn't actually all that lite. After solving the other bottlenecks in my code I was left with around 30% of my processing time locked-up in MIME::Lite's code.
My first stop was MIME::Fast. It certainly is fast, but unfortunately it also has some large memory leaks. Running at full speed it was losing around 5MB per second! I spent a few hours confirming that the leaks are somewhere in the huge XS codebase of MIME::Fast and not in the underlying gmime library. I gave up and filed a bug with the author.
That left me back with MIME::Lite, so I decided to see if I could speed it up. After a few hours of work I've come up with a patch which offers a 50% speedup for my use-case (creating two-part messages from parts in memory). For the curious, here's my work-log:
I sent the patch to the MIME::Lite maintainer, so hopefully this code will be available to all someday soon.
-sam
Re:I'd be interested in that patch
samtregar on 2005-12-05T19:21:34
Be my guest: http://sam.tregar.com/mime_liter.diff-sam
$sub_attrs->{'content-disposition'}{'filename'}
would ever be faster than $sub_attrs->{'content-disposition.filename'}
— but maybe there's more to it than I see at first look. I'd like to understand your rationale for that change.Re:Maintaining MIME::Lite
samtregar on 2005-12-07T07:41:51
1) I'll send you the benchmarks I sent to the current maintainer via. I used Benchmark.pm, of course.2) The code needs to be able to loop through all the sub-attrs for a given attr in fields(). That would be rather complicated with the scheme you suggest, although it's possible it would be faster (and there's only one way to find out!). However, this isn't a change I made: the old code looked up every single attribute like $attr->{$attr}{$sub} where $sub was '' for the "main" attribute. My change removed this for "main" attributes which are by far the most common case and left the sub-attribute storage scheme as-is, albeit moved to a separate structure.
-sam
Re:Maintaining MIME::Lite
bart on 2005-12-07T21:37:26
RE #2: You're right, judging by your explanation, the original looks very ugly indeed. I hadn't caught that in the casual reading of your patch.