Lazy Loading for PGN Database Views

Ok, so it’s not much, but I figured I could post a screenshot for a database program I’m working on:

One of the issues with developing for a mobile device is memory – it’s a precious resource, even if your device doesn’t allow for true multitasking.  Cocoa Touch (the iPhone framework) likes to use lazy loading of data to reduce memory usage.

“What is lazy loading?”, you might ask.  Lazy loading only loads data into memory just before it is needed, as opposed to having it stored in memory up front for quick access.  Think about a UITableView (you know, all the lists that show up on the iPhone, like in Contacts or a list of songs in iPod).  You can only have maybe 10 UITableViewCell (each individual item!) items displayed at any given time.  So for a user with 1000 songs in their library, it doesn’t matter what the other 990 are – the phone only has to load the data for the 10 cells it’s going to show.  Basically, this is a really cheap and effective way of giving the appearance of all the data being present up front, but it’s actually not loaded into memory until the UI needs to display it somehow on screen.

OK, so what does that have to do with PGN Databases?  Let’s say we want to load a database full of games from our favorite Sicilian variation – the Bg5 Najdorf Defense!  This used to be a popular line, and so there are a lot of games – the database I downloaded (from ChessMentor) has over 12,000 games.  I want to be able to scroll through my database and pick an arbitrary game, but my iPhone is going to kill my app if I try to make a UITableView with 12,000 items all loaded into memory ahead of time, so I need to be able to process the PGN file and know how many games there are and the header information so I can display something relevant about my game in a cell.

Yes, the cells are ugly right now – it’s just a prototype.  Once I get around to it I’ll post some different UITableViewCell styles and see if anybody has an opinion.  Right now I just wanted the information available for debugging.  Anyway, so I have to keep track of the location of each individual game in the PGN file so that my table can query for header information at the indices it’s displaying.  My current implementation loads the byte offset for each game into a vector when the database is opened.  Then it generates the header information on the fly when the table queries it for the information by seeking to the offset for the queried game and processing the first few bytes for header information, which is then returned and displayed in the table.

Unfortunately for large databases just initializing the offset vector can take ungodly amounts of time.  To load the SicilianNajdorf6Bg5 database on my iPhone 3GS (running 3.1.2), it took around 70 seconds.  One of the typical things to do for long table view lists is to only load 25 or so entries at a time, which I may try out eventually.

Hopefully this provides just a quick insight into what has to go on to actually develop a PGN reader (besides actually parsing the game data and displaying it!).  These are just sort of my musings and some of the thoughts that had to go in to writing an iPhone app.  I hope the screenshots create some interest in a database app!  I’m not sure if there’s a huge demand for it, but I think there definitely is potential if the app is good enough!

This entry was posted in iPhone. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s