mu.nu - "My Tamagotchi!" she said. "It has to poop."
April 05, 2004
Okay, folks, here's how it works.
MT is robust and has lots of good features, but it's as slow as a dead dog. For example, on an Athlon XP 2800+, with both the code and the database stored in a ram disk, it takes around 28 seconds of CPU time to add a three-word post to Ambient Irony. I haven't yet had time to profile MT to work out what the hell it's doing with all that time. Part of the problem is MT's insistence on static files and its brute-force approach: Whenever you update a post or add a comment, MT will rebuild not only the individual entry, but also the main index, the archive for whatever categories that post is in, and the archives for that day, week and month, and the archives for the previous day, week and month, and, if they exist, the archives for the following day, week and month. And MT is slow at just rebuilding a single entry, so when it has to run around rebuilding everything in sight, it's really slow. Without fixing the code, the only way I can see to cure this is to give it less work to do. One way to do this is to disable any archives you don't use. If you don't use categories, make sure that category archives are turned off. The next thing you can do is to make the templates simpler. And here's where my little trick comes in. If you have your blog set up like me, the appearance of the posts in the main index is exactly the same as in the monthly archives and the category archives. You could use a sub-template to keep the common data, but there's an even sneakier thing you can do:Step 1. Create a new template. Call it something like "Individual Include Template". Copy the section of your main template that deals with the individual entries into this new template. If you are using the default MT template, the result will be this:Save the new template.
<div class="blogbody">
<a name="<$MTEntryID pad="1"$>"></a>
<h3 class="title"><$MTEntryTitle$></h3>
<$MTEntryBody$>
<MTEntryIfExtended>
<span class="extended"><a href="<$MTEntryPermalink$>#more">Continue reading "<$MTEntryTitle$>"</a></span><br />
</MTEntryIfExtended>
<div class="posted">Posted by <$MTEntryAuthor$> at <a href="<$MTEntryPermalink$>"><$MTEntryDate format="%X"$></a>
<MTEntryIfAllowComments>
| <a href="<$MTCGIPath$><$MTCommentScript$>?entry_id=<$MTEntryID$>" onclick="OpenComments(this.href); return false">Comments (<$MTEntryCommentCount$>)</a>
</MTEntryIfAllowComments>
<MTEntryIfAllowPings>
| <a href="<$MTCGIPath$><$MTTrackbackScript$>?__mode=view&entry_id=<$MTEntryID$>" onclick="OpenTrackback(this.href); return false">TrackBack (<$MTEntryTrackbackCount$>)</a>
</MTEntryIfAllowPings>
</div>
</div>
Step 2. Now go into the Archiving page in Weblog Config. Click on the Add New button at the bottom, and create a new Individual Entry archive using the template you just created. You will need to give the files created by this template a new and unique name. This is what the Archive File Template field is for. You will need to use MT tags here to generate the name. A good, simple name might be<$MTEntryID>.iNow, save your archive settings and do a full rebuild of your blog to generate all those little .i files, because in the next step you're going to need them.
Step 3. Go into your Main Index template and (after making a backup copy), cut out all the code you put into your new "Individual Include Template" and replace it with this:
<?php include 'archives/<$MTEntryID>.i'; ?>(Assuming that your archive directory is called "archives", which it probably is.) Save your changes, but don't rebuild yet. Go into the Preferences page in Weblog Config, and scroll down to where it says File extension for archive files This is probably set to html. You will need to change it to php. And in the Templates page, select your Main Index template and change the output file to index.php. Save the changes, and now go and rebuild your main index. If you got it right, it will now rebuild without having to process the details of the individual entries. The entries will instead be read automatically from their individual .i files whenever someone visits your blog. Repeat step 3 for your daily, weekly, monthly and category archives, whichever ones you may be using, except that for these you will not need to specify the "archives" directory as it will look there automatically.
Step 4. (Optional) Somewhere near the top of your main index, add the following code:Having done all of that, is MT now zoomily fast? Alas, no. It will probably halve the time required to add a post or leave a comment, but it's still dog slow compared to what it should be capable of. ExpressionEngine is quite a bit faster at processing posts and comments - at least ten times faster - but it's between thirty and a hundred times slower at delivering the pages. That's the main reason I decided not to go down that route. This is basically how Minx works, except that the Minx template engine is about a hundred times faster than Movable Type's. It's just a pity that it's not finished yet.
And in a suitable place near the end, add this:<?php
$timeparts = explode(' ',microtime());
$starttime = $timeparts[1].substr($timeparts[0],1); ?>This will show you how long PHP takes to put all the individual entries together to build your page. A fairly typical result for Ambient Irony, which has 25 entries on the main page, is 0.005888 seconds. Which I must say is satisfyingly quick.<div class="sidetitle">
Render Time
</div>
<div class="side">
<?php
$timeparts = explode(' ',microtime());
$endtime = $timeparts[1].substr($timeparts[0],1);
echo bcsub($endtime,$starttime,6)?> seconds<br>
</div>

Update! I have now disabled the rebuilding of archive and index pages when someone leaves a comment on Ambient Irony. This has reduced the comment processing time down to around 4 seconds. Still slow, but much less slow.
Posted by: Pixy Misa at 11:58 AM | Comments (50) | Add Comment | Trackbacks (Suck)
Posted by: Kang A. Roo @ at April 05, 2004 03:12 PM
Posted by: Jennifer @ at April 05, 2004 04:38 PM
Posted by: Madfish Willie @ at April 05, 2004 04:56 PM
Posted by: Madfish Willie @ at April 05, 2004 04:59 PM
Either it will be doing something useful by then, or it won't be ready before Christmas, and I'll be looking at interim solutions again.
Posted by: Pixy Misa @ at April 05, 2004 05:18 PM
Posted by: Jennifer @ at April 05, 2004 05:20 PM

Posted by: Pixy Misa @ at April 05, 2004 05:22 PM
Oh, and Willie?? You wrote:
How will that affect links etc back to our moan page
Paging Dr. Freud!!
Heh.
Posted by: Emma @ at April 06, 2004 01:11 AM
I'm still trying to work out how to do that selectively.
Posted by: Pixy Misa @ at April 06, 2004 01:45 AM
Posted by: Jennifer @ at April 06, 2004 03:39 AM
Posted by: Pixy Misa @ at April 06, 2004 03:51 AM
Posted by: Alex @ at April 06, 2004 04:05 AM
If it helps, I'm in too, but I fear messing with my template much until I actually translate the HTML and CSS into english I really understand...
Posted by: Tim from Backstage @ at April 06, 2004 10:27 AM
And...I don't even HAVE a moan page. Unless you count it when what I post makes ya moan...in pain.
There should be a beverage alert on that comment...rotfl.
Posted by: Stevie @ at April 06, 2004 11:50 AM
Posted by: Pixy Misa @ at April 06, 2004 03:37 PM
Last time I looked the problem isn't directly with MT, it's with Perl. No support for multiple threads you see and no easy way to do Asyncronous processing... I was rather hoping they'd have ditched Perl in v 3 and started again with somehting a little more robust but never mind ... minx here we come.
Posted by: Rob @ at April 06, 2004 04:08 PM

The problem isn't Perl - Perl is darn fast at text processing and supports threads (as of version 5.005, but since revised and upgraded.) The problem is that MT is a pure-CGI program (it could launch background tasks to do some of the work, but doesn't), and that there is something seriously wrong with the MT template engine. Every other template engine in wide use is at least an order of magnitude faster than MT, whether it's written in Perl or PHP or Python or shell scripts.
Posted by: Pixy Misa @ at April 06, 2004 04:21 PM
Posted by: Jennifer @ at April 06, 2004 04:32 PM
Minx supports both SSI and PHP includes. Insofar as it supports anything...
Posted by: Pixy Misa @ at April 06, 2004 04:38 PM
Posted by: Rob @ at April 06, 2004 05:11 PM
Posted by: Pixy Misa @ at April 06, 2004 05:24 PM
i saw the include code in the source of my page.
do I have to turn something on for php to work?
Posted by: SpaceMonkey @ at April 06, 2004 08:36 PM
Posted by: Emma @ at April 06, 2004 10:17 PM
Posted by: Madfish Willie @ at April 07, 2004 12:40 AM
Posted by: Madfish Willie @ at April 07, 2004 12:44 AM
Posted by: Emma @ at April 07, 2004 02:13 AM
Posted by: Simon @ at April 07, 2004 03:36 AM
Posted by: John Lanius @ at April 07, 2004 04:05 AM
As far as the speed problem is concerned, I only got DSL a few months ago: I blogged for over three quarters of a year using *dialup.* I'm basically a quasi-Mennonite, here. I keep a nail file by my computer and practice my breathing while the machine does its thang.
Posted by: Little Miss Attila @ at April 07, 2004 06:14 AM
Posted by: Susie @ at April 07, 2004 06:27 AM
Unless you understand what all this is about, don't do it. You will probably end up breaking everything.
Posted by: Pixy Misa @ at April 07, 2004 06:38 AM
Posted by: Pixy Misa @ at April 07, 2004 06:40 AM
The more code you have in your blog templates, the more work MT has to do when you post / rebuild becasue it has to "read" (parse) all this code to find out where to put the bloggy bits. These bloggy bits are the sections of a page that change with every entry - title, category, entry, extended entry, etc. The rest of your page is always the same so there is no point in having MT read it.
What Pixy's technique does is reduce the amount of code that MT has to read by seperating the bloggy bits into a new file. That new file is then "included" (think of it as a automatic copy and paste) into the right place in your templates.
The upshot - less code means faster rebuilds.
The down side? None!
w00t!
Oh and Cnogi is the worst spelling of config I've ever managed - I AM THE TYPO KING!
Posted by: Rob @ at April 07, 2004 12:18 PM
robert@xset.co.uk
Posted by: Rob @ at April 07, 2004 12:26 PM
That settles it. I am definitely better off in the Don't Do It camp (but I've no objection to anyone else doing it on my site at their convenience.)
Posted by: Debbye @ at April 08, 2004 04:31 AM
Posted by: SpaceMonkey @ at April 08, 2004 05:30 AM
I've done this for you.
Posted by: Pixy Misa @ at April 08, 2004 11:03 AM
Posted by: SpaceMonkey @ at April 08, 2004 01:28 PM
I can't seem to get the XBitHack on to work as I can't CHMod HTML files to be executable so it looks like I'm stuck with the PHP include.
Pixy, any plans on that global apache handler to parse all HTML as PHP? Apparently it can cause quite a large increase in system resources
Posted by: Rob @ at April 08, 2004 03:13 PM
Processing 0.01, elapsed 0.008 seconds.
22 queries taking 0.0037 seconds, 62 records returned.
Page size 30 kb.
Powered by Minx 0.8 beta.