Performance Hacks
The following performance hacks are now implemented to improve the involution experience for the kids. Most of these were suggested to me by YSlow and Matt Mullenweg at Wordcamp.
- Installed wp-cache
- Enabled mod_deflate for gzip compressed html, javascript, and css
- Disabled ETags
- Added Expires headers
- Moved CSS to the top of the <head> container
- Removed the AJAX calendar wordpress plugin (this was hammering the database)
The front page now renders in about 1.5 seconds instead of the 4 seconds that it used to take. The next step is to remove all of the little mini icons in the sidebar and replace them with sprites which will reduce the number of HTTP requests from 21 down to 10. Running JSMin on my Javascript probably wouldn’t hurt either.
UPDATE: Apparently, you should only disable ETags if you’re running a server farm. Doing so for a single server installation probably does more harm than good.
The Answers
I just found the answers.
Next Prime in C
Occasionally, I like to dabble around with mathematical code just to keep myself honest. So, I wrote a little function a couple of months back to calculate the next prime given a number n (iff n < 232). I showed this to the illustrious Andy Grimm, and of course he suggested that it may be possible to get the upper bound of the maximum possible factor without actually performing an expensive square root in the is_prime function. I recall from reading an embedded systems book a couple of years ago that something like this should indeed be possible, but a solution isn’t really obvious to me right now. At any rate, here’s my implementation of next_prime(). If you know of a faster way to compute is_prime(), let me know.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
short isprime(unsigned long n) {
unsigned long increment=4, divisor=5;
unsigned long max_divisor = (unsigned long)sqrt(n);
if ((n==1)||((n&2ul)==1)||((n%3ul)==0)||(n%5ul==0)) return 0;
for(divisor=5; divisor<max_divisor; divisor+=increment) {
if (n%divisor==0) return 0;
increment = 6 - increment;
}
return 1;
}
unsigned long next_prime(unsigned long n) {
unsigned long increment, divisor=5;
if (n<7) {
if (n==1) return 2;
if (n==2) return 3;
if (n<5) return 5;
}
while(n%6!=1 && n%6!=5) n+=1;
increment = (n%6==1) ? 4 : 2;
while(!isprime(n)) {
n+=increment;
increment = 6 - increment;
}
return n;
}
int main(int argc, char **argv) {
printf("next prime is %d\n", next_prime(strtoul(argv[1], NULL, 10)));
}
CSS Templating with Sass
I don’t hear many people talking about Sass, but it is indeed fantastic. Basically, Sass is kind of like the Smarty of CSS Templating. Sass files compile to CSS after every change just like Smarty’s TPL files compile directly to PHP. Sass fits well into the Rails don’t-repeat-yourself methodology because you can define color and style constants within CSS rather than explicitly specifying them twenty times. The beauty of Sass is that you get a level of abstraction without any runtime overhead. As a result, Sass performs well and allows quick updates to color schemes and typography without resorting to perl/find or ninja text editor foo. So, why aren’t more people using it?
Ruby CAPTCHA / Ruby-GD StringFT Errors Revisited
Well, I finally solved the “undefined method `stringFT' for GD::Image:Class” error under Mac OS X. The rb-gd darwinport seems to have fallen off the face of the earth. Fortunately, I’ve learned that you can download the source from ruby-lang.org. So, I just rolled my own GD.bundle.
wget http://raa.ruby-lang.org/cache/ruby-gd/ruby-GD-0.7.4-1.tar.gz
cd ruby-GD-0.7.4
ruby extconf.rb --with-freetype --with-xpm --with-ttf
make
sudo make install
sudo mv /usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.8.2/GD.bundle /usr/local/lib/ruby/1.8/i686-darwin8.8.2/
This only works if you have an up-to-date ruby installed under /usr/local (via the Hivelogic instructions). If you’ve installed from ports or are using the older ruby shipped with Leopard, you’ll need to switch your prefix to /opt/local or /usr respectively.










