Saturday, July 21, 2012

SpaceChem optimization

SpaceChem is a programming video game disguised as a chemistry video game.  A static description cannot really do it justice, so here's a trailer:


Honestly, the trailer doesn't do it justice either, so maybe you should go grab the demo from Steam.  Anyway, SpaceChem has two types of puzzles: Research and Pipeline puzzles.  Research puzzles are fairly straightforward to work with because an input/output operation always takes exactly one cycle, whereas with a Pipeline puzzle, the tubes pipes can be clogged or empty, making your I/O operations block.  This means that performance problems with one reactor can affect others.  But optimization is not an all-or-nothing affair.  You need to know where to concentrate your work.  That's what this post is about.

Start at the end

You should start your analysis at the ends of the pipeline, that is, the reactors feeding into outputs.  If you never see waiting(α) or waiting(β) on those reactors, then they are the bottlenecks.  This is fairly straightforward: the speed of those reactors is your ultimate goal, and if nothing external is slowing them down, the only speed gains to be realized will be within those reactors.

On the other hand, if those reactors are waiting, look into what they're waiting on.  Keep track of which reactors you care about.  You should only care about reactors that directly or indirectly cause the end reactors to wait.

Trace in both directions

There are two types of waiting in SpaceChem: waiting on input and waiting on output.  End reactors generally don't wait on output, and beginning reactors don't wait on input.  If a reactor you're interested in is waiting on input, look at what's producing the input.  If it's waiting on output, look at what's consuming the output.  If both pipes are waiting on output, you probably shouldn't care about this reactor since it's producing faster than it's being consumed.

Identify bottlenecks

Follow the trail of waiting until you find a reactor that doesn't seem to wait very much, but often blocks other reactors.  This reactor is your bottleneck.  Until you improve it, none of your other optimizations will have much effect.

If you can't find a bottleneck easily, just focus on the end reactors until one emerges.