Waste not, want not

The map-converting code is ready and working. There’s not much that can be done with it just yet, since I don’t have any code for converting RPG Maker’s tilesets or actually displaying the map in the editor. That’s what I’m working on right now, and I’ll post an update once that’s working.
When I was testing the map-converting code, having the converter run through every map in a project, it worked but it was going slowly. So I hooked it up to a profiler, a debugging tool that watches your program and tells you how it’s spending most of its time, and found that it was spending almost half of its time creating new objects in memory. This doesn’t make much sense; creating an object is a very simple operation that takes very little time, even at the microseconds scale that computers operate at. A bit more checking and I found the reason: I was creating a whole lot of tiny little objects to decode BER compressed integers in RPG Maker’s savefiles to normal numbers in the computer’s memory.
BER compressed integers are a trick that can be used to store numbers to disc using a small amount of actual data, thus making your savefile smaller. RPG Maker uses them everywhere, and in the project I was using to test with, it was processing almost 800,000 of them in 440 maps, plus the LMT and LDB. When I originally wrote the code to read a map, several years ago when I knew a lot less about programming than I do now, I set it up to create a memory object to perform the decoding, and then throw it away once it was no longer needed. Problem is, every value gets decoded exactly once and is never needed again, so there’s no need to create a new decoder for each one.
I rewrote the code to make a single, reusable decoder object. Suddenly, instead of 800,000 BER compressed integer decoder objects, I was only creating one. Now the map converting phase speeds by, about 30% faster than it did before. The moral of the story? No need to throw something away when you could reuse it instead.

Comments are closed.