Speeding up loading times

August 09, 2014 | Filed under: programming

So I released a video yesterday of GSB2 running on multiple monitors, and in that video, a large space battle loads. It takes about 5 seconds. I find those 5 seconds agonizing. I recently spent half a day speeding up the games startup time because launching the game to test 100+ times a day was starting to buy me. Granted, 100×10 seconds is not very long, but I am incredibly impatient. I sometimes launch 2 apps on 2 different monitors and start typing on app 1 whilst windows finishes creating the window for app 2. I often encounter problems where clicking the google homepage and entering a search term means I miss the first few characters as the intel i7 catches up. I’m VERY impatient.

So how did I speed it up? and what tips are there for coding faster loads?

In this case, it was dumbness on my part. Some code that pre-loads ship designs was checking for whether or not lightmaps existed for each ship texture. Some have them, some don’t. It checked by creating the file and loading it in, failing if not found. HAHAHAHA. Very slow, and not needed. Those ships may well not be accessed this game…

So obviously it was tons quicker to not load the file in then, but just to check for the existence of the file on disk (or arguably to do this even later). Cue big speedup. Another speedup was possible because every instance of that item checked for the files existence, so I cached the result in the ship type and only checked for each file once. Much Much Faster.

That’s fine with a game that has few files on disk, but if you need to do this for 2,000 files, it can be real slow just to do those 2,000 file checks. Accessing actual hard drive data as opposed to RAM is painfully slow. With non SSD drives actual physical platters might need to spin. File access is the killer of load times. File access, and decompression.

Ideally you have data on disk that is pretty much in the format it will take in memory, then there is no decompression, just a straight ‘blap’ into RAM. There is a tradeoff here against debuggability, editability and modability. Some games have 2 file formats, a binary version, used when shipped, and a text version that overrides the binary and is only used during development. That can be kinda buggy, and prevents modders accessing the raw assets which might be a bad thing.

Another trick is to use ‘pak’ or resource files. In principle, and often in practice, these are just big zip files. The file access for the entire games resources becomes a single file read, and you effectively load the whole file-access-table for the game in on startup, vastly minimizing file-access slowdowns in cases where there are thousands of tiny files.

In general the golden rule of load times is to only do what you have to. If you can pre-process assets (into binary formats, for example) then do it. If you can defer some actions until that content is really needed, then do it. There is a natural tendency for programmers to have a ‘initapp()’ function and stick everything in there, but you really don’t need to. The player may well not launch the stats screen in this run-through, so don’t load any assets for it, or indeed, any data for it, and don’t waste CPU time initializing systems that might not even be used.

The best advice I could give anyone for speeding up load times would be to just get a profiler. Some are free, most IDEs come with a built-in one (they often suck), and LEARN how to use it. If you never put any time into fixing it, the chances are your load times are laughably inefficient. Most games are.

5 Responses to “Speeding up loading times”

  1. Bram says:

    A trick I use is to link 3d models and 2d images into the binary as straight blobs. This means everything is in mem at prog start, and there is zero loading.

    This was prompted by android porting where file data is hard to get to from native C, as it has to bridge from java.

    My trick only works for small data though. I have little data because my 3d models tend to be untextured. I only use textures for hud and menu.

  2. CdrJameson says:

    I’d say the best way to speed up loading times is to be impatient.

    My pet-hate are web apps – How to make your twenty-first century computer slower than your 1990s one! Even slower than disk access!

    If a delay is longer than about 100ms (ie perceptable) then my attention has gone.
    Do keep up, machine!

  3. Scott F. says:

    Jeff Atwood had a recent post that maps access times into more comprehensible units:

    http://blog.codinghorror.com/the-infinite-space-between-words/

    ‘If the CPU registers are how long it takes you to fetch data from your brain, then going to disk is the equivalent of fetching data from Pluto.’

  4. el presidente says:

    Interesting article, but could you please spend some time fixing Democracy 3 for iOS? The most recent patch is causing so many crashes, the game is 100% unplayable. From the looks of the complaints on the forums and in recent reviews on the appstore, this issue is pretty widespread.

    Thanks!! Love all your games otherwise!

  5. cliffski says:

    that crash is already fixed. We are all spinning our thumbs waiting for apple to ‘approve’ the fix :(