March 2, 2015

Space Lag

Premature optimization is bad! But space navigation had caught a serious case of lag. It was bad enough to annoy me, so I decided it was time to fix it.

Reading and writing files takes a bit of time and it was happening often. Move over one sector? Save and load the 25 surrounding sectors! Every time. Whenever this occurred, the animations would pause and there would be a delay to processing input.

The first step was to record on average how long it was taking to read and write everything. I started a new game and moved diagonally 50 screens. The result?

average: 0.919481 seconds

Almost an entire second!

The first optimization I made was to stop saving sectors that had no changes to them. Doing this was always in the plan but I hadn’t gotten around to adding it yet. This sped things up considerably.

average: 0.438379 seconds

That’s a lot better.

But I knew it could be faster. On average I was saving and loading the exact same sectors 60-80% of the time. With clever coding, it should only need to load new sectors in the direction you’re moving.

average: 0.303122 seconds

Even better!

This doesn’t even take into account the time it takes to generate new, unexplored sectors. Moving around already generated space is even faster.

Space exploration is now a much smoother and lag free experience.

(This improvement will be included in the next version of spacedust.)

tagged: status