Lead Developer, Stardock Entertainment
Published on June 24, 2005 By CariElf In GalCiv Journals
Aside from working on fixing some of the more glaring bugs in beta 2, I've been working on optimizing the loading time for new games. According to our profiling code, it was taking 20-30 seconds to load a small galaxy. This, of course, was insane, and besides being annoying was nickeling and diming our development time. If it takes 30 seconds just to get into a game to test something, and you have to keep starting new games, that starts adding up.

The first thing that I fixed was a function that was meant to update data on screens that is recalculated every turn. It was always being called 2-3 times, redundantly, because the person who wrote it overrode a function from the base window class that was only meant to force a window to repaint, not actually update data. That helped a lot.

One section of code that was taking at least 13 seconds was all the calls to a function called OnStartNewGame. This function was going through and setting data in screens that only needs to be set at the beginning of a game. It was also initializing all of those screens because it needed them to be initialized to set the data. All of our screens are set up to initialize only when they are unhidden, so that we don't have as much startup time and if you never open a screen, it doesn't waste time initializing it, and having OnStartNewGame was pretty much eliminating that advantage. So I added a variable to the base screen class that indicates if the OnStartNewGame function has been called yet, and made it so that the function doesn't get called until the window is unhidden, then flags that it has done the OnStartNewGameCode. I left it initializing the planet window, the research window, and the economic window, because those screens will take a noticeable amount of time before opening the first time if they aren't pre-loaded. This eliminated about 12 seconds.

I tweaked a few other minor things, so the loading time is much better than it was, but it could still use some attention. One of the functions that takes the most time is the function that creates the actual graphics for the star map, all the stars and planets, etc. Partly just because there are a lot of them, and partly because it's a time intensive operation. We used to have it in a thread, but I noticed that it would often create weird graphical glitches when they were being created in the thread. I suspect that it was trying to create two graphic at once, and I can put in a critical section or something like that, but in order for the thread to be effective I would need to start doing the galaxy graphics as soon as the player has left the galaxy setup screen.

The problem with doing the galaxy graphics as soon as the player goes from the galaxy setup screen to the race setup screen is the civs might need to change a lower class planet to a higher class planet if it can't find a good one first. Also, the code to pick a starting planet takes 4 seconds on a small galaxy, and it's far worse on a gigantic galaxy. So I decided that I needed to kill two birds with one stone: I needed to know ahead of time which planets the civs were going to use, and make sure that they had enough good planets to choose one, and I needed to speed up the planet selection process.

I started working on this last night and I've got it about 90% done, and the difference is phenomenal. I went from taking 4 seconds to pick planets on a small galaxy to taking less than a millisecond. It's even more pronounced on a gigantic galaxy. The reason that I say it's only 90% done is because my current implementation is that on the larger galaxies where you want the races to be more spread out, it doesn't have any guarantee that the civs won't end up right next to each other. It works for any two of the planets next to each other, but if the first and third ones are right next to each other, it doesn't take that into account. It doesn't matter on the tiny and small galaxies because they're going to be within a sector of someone else no matter what. I'm probably going to have to end up writing my own algorithm instead of using the STL sort function, so it won't be as fast, but it should still be faster than the original code. The advantage of pre-picking all the starting planets at once is that I get to save data, so I'm not making redundant loops through all the available planets in the galaxy.

Anyway, it's Friday night so I'm going to wrap this up.

--Cari

Comments
on Jun 29, 2005
Go go Cari!
on Jun 29, 2005
Optimization?! HOLY CRAP!
on Jun 29, 2005
I wish I was half as productive!