Optimizing my Haskell Raytracer
brett lajzer :: 02.25.2012+18:35

Optimizing my Haskell Raytracer::02.25.2012+18:35

Way back in 2010 I wrote a really crappy, proof of concept raytracer as a way of familiarizing myself more with Haskell. I picked it back up this morning because I wanted to see how much I could improve the performance of it by doing some pretty simple optimizations. I could probably improve performance further by doing actual structure changes to the program, but I’d rather fall back on algorithmic improvements before fighting with the code generator. At any rate, the final results are satisfying: a 70% decrease in running time overall.

Strictness Annotations

The first optimization I did was to put strictness annotations on the Doubles in the vector type. What started off as:

data Vec3f = Vec3f Double Double Double
  deriving(Eq,Show)
became:
data Vec3f = Vec3f !Double !Double !Double
  deriving(Eq,Show)
This resulted in a 19% decrease in overall run time. Nothing drastic, but still a very significant difference.

Unboxed Strict Fields

The next optimization was to add the compiler flag “-funbox-strict-fields”. What this does is tell GHC to automatically add the UNPACK pragma to strict fields in data constructors. The end result, is that the Vec3f constructor is no longer storing heap pointers to Doubles and instead just storing the Doubles themselves. Unboxing the strict fields brought the total run time decrease to 35%.

Float versus Double

As most people know, Doubles are not very fast on many systems. In the past, I had seen a speed increase by using Doubles instead of Floats in this raytracer. I believe that it had something to do with GHC not using SSE for Float and only for Double. Regardless, switching from Double to Float doubled the total savings. The result was a decrease of 70% run time versus the original.

Code / Full Disclosure

You can download the code here: raytracer.zip. When run it will generate a file called “output.ppm” containing the rendered image. It should look like this:
The above tests were done on my Acer Aspire One, which has an Intel Atom N270 1.6GHz HT and 1GB RAM. I’m not sure what the performance differences will be for a 64-bit machine with a better processor.

post a comment

Brett Plays Old Games: Deus Ex: Invisible War::08.07.2010+23:57

This is the first in a series of posts where I play and review old games that were possibly high-profile at the time they were released but have since faded. The majority of these games will be PC games because there are a lot of old PC games that most modern gamers haven’t played, and I happen to love PC games.

Often thought of as the ugly stepchild of the original rather than the direct successor, Deus Ex: Invisible War builds upon Deus Ex in some ways while stepping backwards in others. Released in 2003 and developed by Ion Storm, the game was visually appropriate for the time. Running in a highly-modified version of the Unreal Engine 2, it sports dynamic lighting, stencil shadows, normal mapping, and projected textures. It’s reasonably stable, crashing only twice in the three of four hours that I played it. This review only covers those hours, not the whole game, but first impressions are everything, right?

read more... post a comment

bayonetta::01.21.2010+22:17

Best game ever. Enough said

post a comment

waffle, etc…::06.23.2009+20:55

There’s a whole bunch of new pictures in the photos section, which I will probably end up posting about sometime in the future. Work is continuing on playing and beating awesome games, but it’s slow going right now.

Better than all of that, was that I finally got some time to sit down with my horrible, crappy, modular synth: waffle and start improving it. I think it’s a neat piece of software for exactly two reasons: 1) it proves that modular synthesizers (and audio software in general) aren’t that hard to write and 2) that jack has a really sweet API. Granted, it’s sometimes hard to get audio software right, but if you’re just looking for something good enough, you might be able to write it yourself. Hopefully, I will be able to keep up my motivation and get around to writing a gui (rather than an insane command-prompt-thingy). That’s it for now.

post a comment

life, again::05.27.2009+18:51

I haven’t updated this blog in forever, and I almost wonder why I even bother. At any rate, I graduated in December and got a job working for this pretty awesome little company in Troy, 1st Playable Productions. I was working there as an intern last summer and was pretty lucky to get hired back full-time, given the current economic situation. Last summer I worked a tiny bit on Club Penguin: Elite Penguin Force, a fair amount on Ener-G: Gym Rockets, and wrote a large portion of Imagine Cheerleader.

That’s pretty much it for now. I’m hoping to start updating this more often and maybe start posting pictures.

post a comment
older entries >
powered by uber