<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Games from Within &#187; Game tech</title>
	<atom:link href="http://gamesfromwithin.com/category/game-tech/feed" rel="self" type="application/rss+xml" />
	<link>http://gamesfromwithin.com</link>
	<description>Indie iPhone game development</description>
	<lastBuildDate>Thu, 09 Sep 2010 19:59:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google App Engine As Back End For iPhone Apps</title>
		<link>http://gamesfromwithin.com/google-app-engine-as-back-end-for-iphone-apps</link>
		<comments>http://gamesfromwithin.com/google-app-engine-as-back-end-for-iphone-apps#comments</comments>
		<pubDate>Thu, 09 Sep 2010 19:59:04 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[Game tech]]></category>

		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1145</guid>
		<description><![CDATA[
			
				
			
		
As soon as a game involves servers, there&#8217;s no such a thing anymore as &#8220;ship and forget&#8221;. Flower Garden put this in evidence about a month ago when I started getting complaints from users that the Flower Shop kept going down. Sometimes they weren&#8217;t even getting the items they were purchasing! (fortunately they can always [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p>As soon as a game involves servers, there&#8217;s no such a thing anymore as &#8220;ship and forget&#8221;. Flower Garden put this in evidence about a month ago when I started getting complaints from users that the Flower Shop kept going down. Sometimes they weren&#8217;t even getting the items they were purchasing! (fortunately they can always do a free re-download, but they don&#8217;t always know that).</p>
<p>Flower Garden was using a shared Dreamhost server to upload bouquets, host in-game news, and, most importantly, to serve the Flower Shop (which involves hosting the static shop files, redeeming promo codes, recording transactions, verifying receipts with Apple&#8217;s server, and delivering purchased content). All the Flower Shop functionality was implement in PHP and using a mySQL database. </p>
<p>After some stressful sorting through the logs, it looked like the server was running out of memory and killing PHP processes. Dreamhost&#8217;s technical support didn&#8217;t help much and just claimed that I was simply something that happened and they would be happy to sell me a virtual private server. I was surprised to hear it was using up a lot of memory, and certainly nothing I could see with the tools available to me showed that. I certainly hadn&#8217;t done any changes in a while and traffic was constant. Doing some Googling at the time brought up lots of other angry Dreamhost users in a similar situation, so I suspected server configuration problems.</p>
<p>Whatever the case, I couldn&#8217;t let it be that way and I had to fix it somehow. I briefly considered a virtual private server, or even a dedicated host in some highly <a href="http://www.slicehost.com/">recommended</a> <a href="http://www.linode.com/">providers</a>. But in the end, the simplicity, scalability, and affordability of the Google App Engine won me over.</p>
<p>Now that Flower Garden has been using the Google App Engine for several weeks, I still think it&#8217;s fantastic. I wish someone had whacked me on the head when I started writing server code and forced me to use it. Hopefully this post will be more pleasant than a hit to the head and will still have a similar (good) effect.</p>
<h3>Google App Engine Overview</h3>
<p><img class="alignright size-full wp-image-673" src="http://gamesfromwithin.com/wp-content/uploads/2010/09/appengine_lowres.gif" alt="appengine_lowres.gif" border="0" width="142" height="109" />Even though I was pretty much completely new to the Google App Engine (I had looked into it briefly before the launch of Flower Garden but dismissed it because they couldn&#8217;t support the amount of email traffic I needed), it took me two days to port over everything. Actually flipping the switch from the old system to the new one took a while longer, but that required more courage than work. More on that later.</p>
<p>This is not a Google App Engine tutorial. Instead, it&#8217;s going to be an overview of what it can do and why you should consider it as a backend for your iPhone app instead of using a shared or even dedicated server.</p>
<p>The first shock (at least for me) when looking into the Google App Engine is that you can&#8217;t run PHP. You&#8217;re limited to Java or Python. It turns out Python is my go-to scripting language, so I was thrilled at the idea. PHP is fine, but Python is a whole class above.</p>
<p>Next, it took a bit of adjusting to the fact that the environment to run anything on the Google App Engine has to be carefully controlled. You need to create a configuration file indicating the entry points and their handlers. Fortunately, all of that is well covered in the <a href="http://code.google.com/appengine/docs/python/overview.html">extensive online documentation</a>. </p>
<p>This is what the Flower Garden config file looks like, indicating both static files and several execution entry points:</p>
<pre>
application: st-flowershop
version: 1
runtime: python
api_version: 1

handlers:
- url: /catalog
  static_dir: catalog

- url: /emailassets
  static_dir: emailassets

- url: /news
  static_dir: news

- url: /moregames
  static_dir: moregames

- url: /.*
  script: purchase.py
</pre>
<p>If you hit one of the URLs listed as static_dir, you get those files directly (like the <a href="http://st-flowershop.appspot.com/news/news.html">in-game news</a>). Anything else is handled by the Python script purchase.py. Google even provides the <a href="http://code.google.com/appengine/docs/python/tools/webapp/">webapp framework</a> that easily allows you to connect different parts of the script to handle different requests and easily get to the input parameters.</p>
<p>Once you&#8217;re past that and you have the requisite &#8220;Hello World&#8221; up and running, then it&#8217;s all fun and games.</p>
<p>One of the great features of the Google App Engine is that it comes with a fully-featured, local environment. This environment is installed automatically with the Google App Engine SDK, and there&#8217;s nothing to configure. It&#8217;s certainly nothing like setting up Apache + mySQL + PHP in your dev station! That way,  you can do all your work locally, and only update the servers when you&#8217;re confident everything is working. </p>
<p>Beyond this, the only other thing that is different from what you may be used to is the datastore. They provide a query language called <a href="http://code.google.com/appengine/docs/python/datastore/">GQL</a>, which is not exactly SQL, but it&#8217;s very similar. Close enough that I had no trouble porting over my very simple queries anyway.</p>
<p>Adding data to a data store couldn&#8217;t be simpler. For example, to add a promo code, I need the following class definition:</p>
<pre>
class PromoCode(db.Model):
	code = db.StringProperty()
	productid = db.StringProperty()
	amount = db.IntegerProperty(default=1)
	singleuse = db.BooleanProperty(default=True)
	enabled = db.BooleanProperty(default=True)
</pre>
<p>And then I can add it this way:</p>
<pre>
	promocode = PromoCode()
	# fill it up here
	promocode.put()
</pre>
<p>How do I check if a promocode is valid? I can use the SQL-like syntax, or something even simpler:</p>
<pre>
	q = PromoCode.all()
	q.filter("code =", code)
	return q.get()
</pre>
<p>Once you know that, you can go to town with your Python programs.</p>
<h3>Google App Engine Advantages</h3>
<p><b>Scalability</b><br />
This was the big reason that forced me to move away from Dreamhost. I have no way of testing how scalable it really is, but I&#8217;m going to take Google&#8217;s word for it. They know a tiny little bit about scalability. And besides, Flower Garden is nothing compared to other apps (it serves about 1.5 requests per second on average).</p>
<p>Since I don&#8217;t know how things are implemented under the hood in the Google App Engine, I don&#8217;t have a good feel for performance best practices. So I&#8217;m hoping that my very simple queries (add to the end of a table, or see if a record is present) are just fine. If not, I can do some tuning down the line (and I won&#8217;t have to wait for any approval to make the code go live!).</p>
<p><b>Local environment</b><br />
The local environment is simply great. It makes developing a pleasure. You can add any test data you want to the local datastore through a web interface without affecting the live server. Probably my favorite feature is the console: Since the server is running locally, you can print out any debug info in the server code and you can see it live as you perform some action. That saved me hours of debugging compared to doing it on a remote server with php!</p>
<p><b>Server synching</b><br />
In the past, I&#8217;ve synced my server scripts through ftp and I kept meaning to write an automated script to do that. In this case, Google provides a script (and a GUI tool) to do the actual synching with the servers, which is great. It even waits a few seconds until it can verify that the new version is live on the server.</p>
<p><b>Python</b><br />
I&#8217;m not a big Java fan (although I used it a bit way back in the day when it first came out), but I do love Python. For a program with some complexity like this, it&#8217;s definitely a much better choice than PHP.</p>
<p><b>Real-time stats</b><br />
The Google App Engine web console displays real-time stats of access to your site. You see it expressed in requests per second and you can visualize it in the past few hours, days, or even month. You also have access to all the logs, and you can filter by type of log message (debug, info, warning, error). </p>
<p><b>Price</b><br />
This is one category where the Google App Engine definitely shines. Every day you&#8217;re given a free quota of bandwidth, disk usage, and CPU usage. If you stay under that quota you don&#8217;t pay anything. If you go over, you pay per unit as you go (at very reasonable prices). You can even set a maximum cap on daily spending so you don&#8217;t encounter any nasty surprises at the end of the month.</p>
<p>So far, Flower Garden has been hovering right at the edge of the outgoing bandwidth free quota (the rest of the metrics it doesn&#8217;t even come close). So even if traffic were to double, expenses would be very reasonable.</p>
<h3>Development Tricks</h3>
<p>Along the way, I figured out a couple of interesting tricks that helped me during development.</p>
<p><b>Bypass the App store for testing</b><br />
For me, few things are more annoying than doing development in the actual device. Iteration is slow and stepping with the debugger is mostly impossible (seems to depend on the mood of the debugger that day). So I try to do the bulk of the development on the simulator and just test on the device when things are ready. </p>
<p>Unfortunately, the device can&#8217;t access the App Store, which makes testing all the server functionality dealing with the App Store painful. What I did was to add a define that, when present, the program bypasses the App Store but still functions as usual.</p>
<p><b>Local server</b><br />
During development, you want to be using your local server. In my case, I set it up so that Debug versions of the code access the local server, but Release and Distribution ones access the live one.</p>
<p>There is one problem: The Google App Engine server, by default, binds itself to localhost, not to the IP address of your network card. That meant I was able to hit it from the simulator just fine accessing http://localhost:8080, but not from the device. In order to access the development server from the device, I had to explicitly tell the server to bind itself to another address like this:</p>
<pre>dev_appserver.py --address 192.168.1.150 FlowerShop</pre>
<p>Now I can access the server at 192.168.1.150:8080 from both the simulator and any device.</p>
<p><b>Switching servers</b><br />
Porting the code took just a couple of days. Making the switch on the live server was a lot scarier though. I couldn&#8217;t wait for a new update to be released because that seems to take over a week these days, so I forwarded requests from the old server to the new one.</p>
<p>I did it one system every day, avoiding weekends which is when there&#8217;s the most traffic. News was easy, and so was the web view with more games. But I ran into a snag with promo codes and Flower Shop purchases. </p>
<p>Initially, I was just redirecting requests in .htaccess this way:</p>
<pre>
Redirect permanent /Shop/catalog http://st-flowershop.appspot.com/catalog
Redirect 301 /Shop/catalog http://st-flowershop.appspot.com/catalog
</pre>
<p>As I learned the hard way, POST variables aren&#8217;t preserved in a redirect of that kind (I guess to avoid state being changed in multiple servers). I tried a bunch of things, but in the end, I wrote a quick php script that gathered all POST variables and re-posted them to the new request. A bit ugly, but it did the trick:</p>
<pre>
&lt;?php
function PostRequest($url, $_data) {

    $data = array();
    while(list($n,$v) = each($_data)){
        $data[] = "$n=" . urlencode($v);
    }
    $data = implode('&#038;', $data);
    // format --> test1=a&#038;test2=b etc.

    $url = parse_url($url);
    if ($url['scheme'] != 'http') {
        die('Only HTTP request are supported !');
    }

    $host = $url['host'];
    $path = $url['path'];

    $fp = fsockopen($host, 80);

    fputs($fp, "POST $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
    fputs($fp, "Content-length: ". strlen($data) ."\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $data);

    $result = '';
    while(!feof($fp)) {
        $result .= fgets($fp, 128);
    }
    fclose($fp);

    $result = explode("\r\n\r\n", $result, 2);
    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';
    return $content;
}

print PostRequest("http://st-flowershop.appspot.com/purchase", $_POST);
?&gt;
</pre>
<h3>Conclusion</h3>
<p>I hope this post is enough to at least make you interested in checking out the Google App Engine and help you get over a couple of the initial hurdles. I&#8217;ll definitely be using it in any future projects.</p>
<p><br/>
<p><i>This post is part of <a href="http://idevblogaday.com/">iDevBlogADay</a>, a group of indie iPhone development blogs featuring two posts per day. You can keep up with iDevBlogADay through the <a href="http://idevblogaday.com/">web site</a>, <a href="http://feeds.feedburner.com/idevblogaday">RSS feed</a>, or <a href="http://twitter.com/#search?q=%23idevblogaday">Twitter</a>.</i></p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps&amp;title=Google%20App%20Engine%20As%20Back%20End%20For%20iPhone%20Apps&amp;bodytext=As%20soon%20as%20a%20game%20involves%20servers%2C%20there%27s%20no%20such%20a%20thing%20anymore%20as%20%22ship%20and%20forget%22.%20Flower%20Garden%20put%20this%20in%20evidence%20about%20a%20month%20ago%20when%20I%20started%20getting%20complaints%20from%20users%20that%20the%20Flower%20Shop%20kept%20going%20down.%20Sometimes%20they%20weren%27t%20e" title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps&amp;title=Google%20App%20Engine%20As%20Back%20End%20For%20iPhone%20Apps&amp;notes=As%20soon%20as%20a%20game%20involves%20servers%2C%20there%27s%20no%20such%20a%20thing%20anymore%20as%20%22ship%20and%20forget%22.%20Flower%20Garden%20put%20this%20in%20evidence%20about%20a%20month%20ago%20when%20I%20started%20getting%20complaints%20from%20users%20that%20the%20Flower%20Shop%20kept%20going%20down.%20Sometimes%20they%20weren%27t%20e" title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps&amp;t=Google%20App%20Engine%20As%20Back%20End%20For%20iPhone%20Apps" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps&amp;title=Google%20App%20Engine%20As%20Back%20End%20For%20iPhone%20Apps" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps&amp;title=Google%20App%20Engine%20As%20Back%20End%20For%20iPhone%20Apps" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Google%20App%20Engine%20As%20Back%20End%20For%20iPhone%20Apps&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fgoogle-app-engine-as-back-end-for-iphone-apps&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/google-app-engine-as-back-end-for-iphone-apps/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Remote Game Editing</title>
		<link>http://gamesfromwithin.com/remote-game-editing</link>
		<comments>http://gamesfromwithin.com/remote-game-editing#comments</comments>
		<pubDate>Thu, 22 Jul 2010 18:00:22 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[Game tech]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[flower garden]]></category>

		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1053</guid>
		<description><![CDATA[
			
				
			
		
I&#8217;ve long been a fan of minimal game runtimes. Anything that can be done offline or in a separate tool, should be out of the runtime. That leaves the game architecture and code very lean and simple.
One of the things you potentially give up by keeping the game runtime to a minimum is an editor [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;ve long been a fan of minimal game runtimes. Anything that can be done offline or in a separate tool, should be out of the runtime. That leaves the game architecture and code very lean and <a href="http://gamesfromwithin.com/simple-is-beautiful">simple</a>.</p>
<p>One of the things you potentially give up by keeping the game runtime to a minimum is an <a href="http://www.unrealtechnology.com/features.php?ref=editor">editor built in the game itself</a>. But that&#8217;s one of those things that sounds a lot better than it really is. From a technical point of view, having an editor in the game usually complicates the code a huge amount. All of a sudden you need to deal with objects being created and destroyed randomly (instead of through clearly defined events in the game), and you have to deal with all sorts of crazy inputs and configurations.</p>
<p>The worse part though, is having to implement some sort of GUI editing system in every platform. Creating the GUI to run on top of the game is not easy, requiring that you create custom GUI code or try to use some of the OpenGL/DirectX libraries available. And even then, a complex in-game GUI might not be a big deal on a PC, but wait and try to use that interface on a PS3 or iPhone. After all, making games is already complicated and time-consuming enough to waste more time reinventing the widget wheel.</p>
<p>Remote game editing manages to keep a minimal runtime and allow you to quickly create native GUIs that run on a PC. It&#8217;s the best of both worlds, and although it&#8217;s not quite a perfect solution, it&#8217;s the best approach I know.</p>
<h3>Debug Server</h3>
<p><a href="http://twitter.com/mysterycoconut">Miguel Ángel Friginal</a> <a href="http://mysterycoconut.com/blog/2010/07/tweak-away/">already covered the basics of the debug server</a>, so I&#8217;m not going to get in details there.</p>
<p>The idea is that you run a very simple socket server on the game, listening in a particular port. This server implements the basic <a href="http://www.faqs.org/rfcs/rfc854.html">telnet protocol</a>, which pretty much means that it&#8217;s a line-based, plain-text communication.</p>
<p>The main difference between my debug server and Miguel&#8217;s (other than mine is written in cross-platform C/C++ instead of ObjC), is that I&#8217;m not using Lua to execute commands. Using Lua for that purpose is a pretty great idea, but goes against the philosophy of keeping the runtime as lean and mean as possible.</p>
<p>Instead, I register variables with the server by hand. For each variable, I specify its memory address, it&#8217;s type, any restrictions (such as minimum and maximum values), and a &#8220;pretty name&#8221; to display in the client. Sounds like a lot of work, but it&#8217;s just one line with the help of a template:</p>
<pre>
registry.Add(Tweak(&#038;plantOptions.renderGloss, "render/gloss", "Render gloss"));
registry.Add(Tweak(&#038;BouquetParams::FovY, "bouquet/fov", "FOV", Pi/32, Pi/3))
</pre>
<p>And yes, if I were to implement this today, I would probably get rid of the templates and make it all explicit instead (ah, the follies of youth <img src='http://gamesfromwithin.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<pre>
TweakUtils::AddBool(registry, &#038;plantOptions.renderGloss, "render/gloss", "Render gloss");
TweakUtils::AddFloat(registry, &#038;BouquetParams::FovY, "bouquet/fov", "FOV", Pi/32, Pi/3);
</pre>
<p>The debug server itself responds to three simple commands:</p>
<ul>
<li><strong>list</strong>. Lists all the variables registered in the server.</li>
<li><strong>set varname value</strong>. Sets a value.</li>
<li><strong>print varname</strong>. Gets the value for that variable.</li>
</ul>
<p>For example, whenever the server receives a set command, it parses the value, verifies that it&#8217;s within the acceptable range, and applies it to the variable at the given memory location.</p>
<h3>Telnet Clients</h3>
<p><img class="alignright size-full wp-image-673" src="http://gamesfromwithin.com/wp-content/uploads/2010/07/telnet.png" alt="telnet.png" border="0" width="236" height="174" />Because we used the standard telnet protocol, we can start playing with it right away. Launch the game, telnet into the right port, and you can start typing away.</p>
<p>However, most telnet clients leave much to be desired for this. They all rely on the history and cursor manipulation being handled by the shell they assume you&#8217;re connected to. Here we aren&#8217;t connected to much of anything, but I&#8217;d like to be able to push up arrow and get my last command, and be able to move to the beginning of the line or the previous word like I would do in any text editor. The easiest solution I found for that was to use a telnet client prepared for that kind of thing: A MUD client! Just about any will do, but one that works well for me is <a href="http://www.riverdark.net/atlantis/">Atlantis</a>.</p>
<p>So far, we&#8217;ve implemented the equivalent of a FPS console, but working remotely. And because the code is fully portable, our game can be in just about any platform and we can always access it from our PC. Not just that, but we can even open multiple simultaneous connections to various development devices if you need to run them all at once.</p>
<h3>Custom Clients</h3>
<p>Game parameter tweaking is something that is OK through a text-based console, but really comes into its own when you add a GUI. That&#8217;s exactly what we did at <a href="http://gamesfromwithin.com/tag/power-of-two">Power of Two Games</a>. We created a generic GUI tool (based on WinForms since we were on Windows at the time), that would connect to the server, ask for a list of variables, and generate a GUI on the fly to represent those variables. Since we knew type and name of each variable, it was really easy to construct the GUI elements on the fly: A slider with a text field for floats and ints, a checkbox for bools, four text fields for vectors, and even a color picker for variables of the type color.</p>
<p>It worked beautifully, and adjusting different values by moving sliders around was fantastic. We quickly ran into two problems through.</p>
<p>The first one is that we added so many different tweaks to the game, that it quickly became unmanageable to find each one we wanted to tweak. So, in the spirit of keeping things as simple as possible (and pushing the complexity onto the client), we decided that the / symbol in a name would separate group name and variable name. That way we could group all related variables together and make everything usable again.</p>
<p>The second problem was realizing that some variables were changing on the runtime without us knowing it on the client. That created weird situations when moving sliders around. We decided that any time a registed variable changes on the server, it should notify any connected clients. That worked fine, but, as you can imagine, it became prohibitively expensive very quickly. To get around that, we added a fourth command: <strong>monitor varname</strong>. This way clients need to explicitly register themselves to receive notifications whenever a variable changes, and the GUI client only did it for the variables currently displayed on the screen.</p>
<p><center><img src="http://gamesfromwithin.com/wp-content/uploads/2010/07/tweaker.png" alt="tweaker.png" border="0" width="542" height="591" /></center></p>
<p>During this process, it was extremely useful to be able to display a log console to see what kind of traffic there was going back and forth. It helped me track down a few instances of bugs where changing a variable in the client would update it in the server, sending an update back to the client, which would send it again back to the server, getting stuck in an infinite loop.</p>
<p>You don&#8217;t need to stop at a totally generic tool like this either. You could create a more custom tool, like a level editor, that still communicates with the runtime through this channel.</p>
<h3>Flower Garden Example</h3>
<p>For Flower Garden, I knew I was going to need a lot of knobs to tweak all those plant DNA parameters, so I initially looked into more traditional GUI libraries that worked on OpenGL. The sad truth is that they all fell way short, even for development purposes. So I decided to grab what I had at hand: My trusty tweaking system from Power of Two Games.</p>
<p>I&#8217;m glad I did. It saved a lot of time and scaled pretty well to deal with the hundreds of parameters in an individual flower, as well as the miscellaneous tweaks for the game itself (rendering settings, infinite fertilizer, fast-forwarding time, etc).</p>
<p>Unfortunately, there was one very annoying thing: The tweaker GUI was written in .Net. Sure, it would take me a couple of days to re-write it in Cocoa (faster if I actually knew any Cocoa), but as an indie, I never feel I can take two days to do something tangential like that. So instead, I just launched it from VMWare Fusion running Windows XP and&#8230; it worked. Amazingly enough, I&#8217;m able to connect from the tweaker running in VMWare Fusion to the iPhone running in the simulator. Kind of mind boggling when you stop and think about it. It also connects directly to the iPhone hardware without a problem.</p>
<p>VMWare Fusion uses up a lot of memory, so I briefly looked into running the tweaker client in Mono. Unfortunately Mono for the Mac didn&#8217;t seem mature enough to handle it, and not only was the rendering of the GUI not refreshing correctly, but events were triggered in a slightly different order than in Windows, causing even more chaos with the variable updates.</p>
<p>Here&#8217;s a time-lapse video of the creation of a Flower Garden seed from the tweaker:</p>
<p><object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/j_j9nIQO_B8&amp;hl=en_US&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/j_j9nIQO_B8&amp;hl=en_US&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object></p>
<h3>Drawbacks</h3>
<p>As I mentioned earlier, I love this system and it&#8217;s better than anything else I&#8217;ve tried, but it&#8217;s not without its share of problems.</p>
<p>Tweaking data is great, but once you find that set of values that balances the level to perfection&#8230; then what? You write those numbers down and enter them in code or in the level data file? That gets old fast. Ideally you want a way to automatically write those values back. That&#8217;s easy if the tool itself is the editor, but if it&#8217;s just a generic tweaker, it&#8217;s a bit more difficult.</p>
<p>One thing that helped was adding a Save/Load feature to the tweaker GUI. It would simply write out a large text-based file with all the variables and their current values. Whenever you load one of those, it would attempt to apply those same values to the current registered variables. In the end, I ended up making the Flower Garden offline seed file format match with what the tweaker saved out, so that process went pretty smoothly.</p>
<p>Another problem is if you want lots of real-time (or close to real time) updates from the server. For example, you might want to monitor a bunch of data points and plot them on the client (fps, memory usage, number of collisions per frame, etc). Since those values change every frame, it can quickly overwhelm the simple text channel. For those cases, I created side binary socket channels that can simply send real-time data without any overhead.</p>
<p>Finally, the last drawback is that this tweaking system makes editing variables very easy, but calling functions is not quite as simple. For the most part, I&#8217;ve learned to live without function calls, but sometimes you really want to do it. You can extend the server to register function pointers and map those to buttons in the client GUI, but that will only work for functions without any parameters. What if you wanted to call any arbitrary function? At that point you might be better off integrating Lua in your server.</p>
<h3>Future Directions</h3>
<p>This is a topic I&#8217;ve been interested in for a long time, but the current implementation of the system I&#8217;m using was written 3-4 years ago. As you all know by now, <a href="http://gamesfromwithin.com/the-always-evolving-coding-style">my coding style and programming philosophy changes quite a bit over time</a>. If I were to implement a system like this today, I would do it quite differently.</p>
<p>For all I said about keeping the server lean and minimal, it could be even more minimal. Right now the server is receiving text commands, parsing them, validating them, and interpreting them. Instead, I would push all that work on the client, and all the server would receive would be a memory address, and some data to blast at that location. All of that information would be sent in binary (not text) format over a socket channel, so it would be much more efficient too. The only drawback is that we would lose the ability to connect with a simple telnet client, but it would probably be worth it in the long run.</p>
<p><br/></p>
<p><i>This post is part of <a href="http://idevblogaday.com/">iDevBlogADay</a>, a group of indie iPhone development blogs featuring two posts per day. You can keep up with iDevBlogADay through the <a href="http://idevblogaday.com/">web site</a>, <a href="http://feeds.feedburner.com/idevblogaday">RSS feed</a>, or <a href="http://twitter.com/#search?q=%23idevblogaday">Twitter</a>.</i></p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing&amp;title=Remote%20Game%20Editing&amp;bodytext=I%27ve%20long%20been%20a%20fan%20of%20minimal%20game%20runtimes.%20Anything%20that%20can%20be%20done%20offline%20or%20in%20a%20separate%20tool%2C%20should%20be%20out%20of%20the%20runtime.%20That%20leaves%20the%20game%20architecture%20and%20code%20very%20lean%20and%20simple.%0D%0A%0D%0AOne%20of%20the%20things%20you%20potentially%20give%20up%20by%20kee" title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing&amp;title=Remote%20Game%20Editing&amp;notes=I%27ve%20long%20been%20a%20fan%20of%20minimal%20game%20runtimes.%20Anything%20that%20can%20be%20done%20offline%20or%20in%20a%20separate%20tool%2C%20should%20be%20out%20of%20the%20runtime.%20That%20leaves%20the%20game%20architecture%20and%20code%20very%20lean%20and%20simple.%0D%0A%0D%0AOne%20of%20the%20things%20you%20potentially%20give%20up%20by%20kee" title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing&amp;t=Remote%20Game%20Editing" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing&amp;title=Remote%20Game%20Editing" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing&amp;title=Remote%20Game%20Editing" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Remote%20Game%20Editing&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fremote-game-editing&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/remote-game-editing/feed</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Managing Data Relationships</title>
		<link>http://gamesfromwithin.com/managing-data-relationships</link>
		<comments>http://gamesfromwithin.com/managing-data-relationships#comments</comments>
		<pubDate>Sun, 27 Jun 2010 21:54:49 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[Data-oriented design]]></category>
		<category><![CDATA[Game tech]]></category>
		<category><![CDATA[game developer magazine]]></category>
		<category><![CDATA[inner product]]></category>

		<guid isPermaLink="false">http://gamesfromwithin.com/?p=942</guid>
		<description><![CDATA[
			
				
			
		
I&#8217;ve been meaning to put up the rest of the Inner Product columns I wrote for Game Developer Magazine, but I wasn&#8217;t finding the time. With all the recent discussion on data-oriented design, I figured it was time to dust some of them off.
This was one of the first columns I wrote. At first glance [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p><em>I&#8217;ve been meaning to put up the rest of the <a href="http://gamesfromwithin.com/tag/inner-product">Inner Product columns</a> I wrote for <a href="http://gdmag.com">Game Developer Magazine</a>, but I wasn&#8217;t finding the time. With all the <a href="http://gamesfromwithin.com/the-always-evolving-coding-style">recent discussion on data-oriented design</a>, I figured it was time to dust some of them off.</p>
<p>This was one of the first columns I wrote. At first glance it might seem a completely introductory topic, not worth spending that much time on it. After all, all experience programmers know about pointers and indices, right? True, but I don&#8217;t think all programmers really take the time to think about the advantages and disadvantages of each approach, and how it affects architecture decisions, data organization, and memory traversal.</p>
<p>It should provide a good background for this coming Thursday&#8217;s <a href="http://twitter.com/#search?q=%23idevblogaday">#iDevBlogADay</a> post on how to deal with heterogeneous objects in a data-oriented way.</em></p>
<p><br/></p>
<p>From a 10,000-Foot view, all video games are just a sequence of bytes. Those bytes can be divided into code and data. Code is executed by the hardware and it performs operations on the data. This code is generated by the compiler and linker from the source code in our favorite computer language. Data is just about everything else. <a href="#1">[1]</a></p>
<p>As programmers, we’re obsessed with code: beautiful algorithms, clean logic, and efficient execution. We spend most of our time thinking about it and make most decisions based on a code-centric view of the game.</p>
<p>Modern hardware architectures have turned things around. A data-centric approach can make much better use of hardware resources, and can produce code that is much simpler to implement, easier to test, and easier to understand. In the next few months, we’ll be looking at different aspects of game data and how everything affects the game. This month we start by looking at how to manage data relationships.</p>
<h2>Data Relationships</h2>
<p>Data is everything that is not code: meshes and textures, animations and skeletons, game entities and pathfinding networks, sounds and text, cut scene descriptions and dialog trees. Our lives would be made simpler if data simply lived in memory, each bit totally isolated from the rest, but that’s not the case. In a game, just about all the data is intertwined in some way. A model refers to the meshes it contains, a character needs to know about its skeleton and its animations, and a special effect points to textures and sounds.</p>
<p>How are those relationships between different parts of data described? There are many approaches we can use, each with its own set of advantages and drawbacks. There isn’t a one-size-fits-all solution. What’s important is choosing the right tool for the job.</p>
<h2>Pointing The Way</h2>
<p>In C++, regular pointers (as opposed to “smart pointers” which we’ll discuss later on) are the easiest and most straightforward way to refer to other data. Following a pointer is a very fast operation, and pointers are strongly typed, so it’s always clear what type of data they’re pointing to.</p>
<p>However, they have their share of shortcomings. The biggest drawback is that a pointer is just the memory address where the data happens to be located. We often have no control over that location, so pointer values usually change from run to run. This means if we attempt to save a game checkpoint which contains a pointer to other parts of the data, the pointer value will be incorrect when we restore it.</p>
<p>Pointers represent a many-to-one relationship. You can only follow a pointer one way, and it is possible to have many pointers pointing to the same piece of data (for example, many models pointing to the same texture). All of this means that it is not easy to relocate a piece of data that is referred to by pointers. Unless we do some extra bookkeeping, we have no way of knowing what pointers are pointing to the data we want to relocate. And if we move or delete that data, all those pointers won’t just be invalid, they’ll be <i>dangling pointers</i>. They will point to a place in memory that contains something else, but the program will still think it has the original data in it, causing horrible bugs that are no fun to debug.</p>
<p>One last drawback of pointers is that even though they’re easy to use, somewhere, somehow, they need to be set. Because the actual memory location addresses change from run to run, they can’t be computed offline as part of the data build. So we need to have some extra step in the runtime to set the pointers after loading the data so the code can use them. This is usually done either by explicit creation and linking of objects at runtime, by using other methods of identifying data, such as resource UIDs created from hashes, or through pointer fixup tables converting data offsets into real memory addresses. All of it adds some work and complexity to using pointers.</p>
<p>Given those characteristics, pointers are a good fit to model relationships to data that is never deleted or relocated, from data that does not need to be serialized. For example, a character loaded from disk can safely contain pointers to its meshes, skeletons, and animations if we know we’re never going to be moving them around.</p>
<h2>Indexing</h2>
<p>One way to get around the limitation of not being able to save and restore pointer values is to use offsets into a block of data. The problem with plain offsets is that the memory location pointed to by the offset then needs to be cast to the correct data type, which is cumbersome and prone to error.</p>
<p>The more common approach is to use indices into an array of data. Indices, in addition to being safe to save and restore, have the same advantage as pointers in that they’re very fast, with no extra indirections or possible cache misses.</p>
<p>Unfortunately, they still suffer from the same problem as pointers of being strictly a many-to-one relationship and making it difficult to relocate or delete the data pointed to by the index. Additionally, arrays can only be used to store data of the same type (or different types but of the same size with some extra trickery on our part), which might be too restrictive for some uses.</p>
<p>A good use of indices into an array are particle system descriptions. The game can create instances of particle systems by referring to their description by index into that array. On the other hand, the particle system instances themselves would not be a good candidate to refer to with indices because their lifetimes vary considerably and they will be constantly created and destroyed.</p>
<p>It’s tempting to try and extend this approach to holding pointers in the array instead of the actual data values. That way, we would be able to deal with different types of data. Unfortunately, storing pointers means that we have to go through an extra indirection to reach our data, which incurs a small performance hit. Although this performance hit is something that we&#8217;re going to have to live with for any system that allows us to relocate data, the important thing is to keep the performance hit as small as possible.</p>
<p>An even bigger problem is that, if the data is truly heterogeneous, we still need to cast it to the correct type before we use it. Unless all data referred to by the pointers inherits from a common base class that we can use to query for its derived type, we have no easy way to find out what type the data really is.<br />
On the positive side, now that we’ve added an indirection (index to pointer, pointer to data), we could relocate the data, update the pointer in the array, and all the indices would still be valid. We could even delete the data and null the pointer out to indicate it is gone. Unfortunately, what we can’t do is reuse a slot in the array since we don’t know if there’s any data out there using that particular index still referring to the old data.</p>
<p>Because of these drawbacks, indices into an array of pointers is usually not an effective way to keep references to data. It’s usually better to stick with indices into an array of data, or extend the idea a bit further into a handle system, which is much safer and more versatile.</p>
<h2>Handle-Ing The Problem</h2>
<p>Handles are small units of data (32 bits typically) that uniquely identify some other part of data. Unlike pointers, however, handles can be safely serialized and remain valid after they’re restored. They also have the advantages of being updatable to refer to data that has been relocated or deleted, and can be implemented with minimal performance overhead.</p>
<p>The handle is used as a key into a handle manager, which associates handles with their data. The simplest possible implementation of a handle manager is a list of handle-pointer pairs and every lookup simply traverses the list looking for the handle. This would work but it’s clearly very inefficient. Even sorting the handles and doing a binary search is slow and we can do much better than that.</p>
<p><a href="http://gamesfromwithin.com/wp-content/uploads/2010/06/HandleManager.zip">Here&#8217;s an efficient implementation of a handle manager</a> (released under the usual <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>, so go to town with it). The handle manager is implemented as an array of pointers, and handles are indices into that array. However, to get around the drawbacks of plain indices, handles are enhanced in a couple of ways.</p>
<p>In order to make handles more useful than pointers, we’re going to use up different bits for different purposes. We have a full 32 bits to play with, so this is how we’re going to carve them out:</p>
<p><center><img src="http://gamesfromwithin.com/wp-content/uploads/2010/06/Handle.png" alt="Handle.png" border="0" width="492" height="104" /></center></p>
<ul>
<li><strong>The index field</strong>. These bits will make up the actual index into the handle manager, so going from a handle to the pointer is a very fast operation. We should make this field as large as we need to, depending on how many handles we plan on having active at once. 14 bits give us over 16,000 handles, which seems plenty for most applications. But if you really need more, you can always use up a couple more bits and get up to 65,000 handles.</li>
<li><strong>The counter field</strong>. This is the key to making this type of handle implementation work. We want to make sure we can delete handles and reuse their indices when we need to. But if some part of the game is holding on to a handle that gets deleted—and eventually that slot gets reused with a new handle—how can we detect that the old handle is invalid? The counter field is the answer. This field contains a number that goes up every time the index slot is reused. Whenever the handle manager tries to convert a handle into a pointer, it first checks that the counter field matches with the stored entry. Otherwise, it knows the handle is expired and returns null.
<li><strong>The type field</strong>. This field indicates what type of data the pointer is pointing to. There are usually not that many different data types in the same handle manager, so 6–8 bits are usually enough. If you’re storing homogeneous data, or all your data inherits from a common base class, then you might not need a type field at all.</li>
</ul>
<pre>
struct Handle
{
    Handle() : m_index(0), m_counter(0), m_type(0)
    {}

    Handle(uint32 index, uint32 counter, uint32 type)
        : m_index(index), m_counter(counter), m_type(type)
    {}

    inline operator uint32() const;

    uint32 m_index : 12;
    uint32 m_counter : 15;
    uint32 m_type : 5;
};

Handle::operator uint32() const
{
    return m_type << 27 | m_counter << 12 | m_index;
}
</pre>
<p>The workings of the handle manager itself are pretty simple. It contains an array of HandleEntry types, and  each HandleEntry has a pointer to the data and a few other bookkeeping fields: freelist indices for efficient addition to the array, the counter field corresponding to each entry, and some flags indicating whether an entry is in use or it’s the end of the freelist.</p>
<pre>
struct HandleEntry
{
	HandleEntry();
	explicit HandleEntry(uint32 nextFreeIndex);

	uint32 m_nextFreeIndex : 12;
	uint32 m_counter : 15;
	uint32 m_active : 1;
	uint32 m_endOfList : 1;
	void* m_entry;
};
</pre>
<p>Accessing data from a handle is just a matter of getting the index from the handle, verifying that the counters in the handle and the handle manager entry are the same, and accessing the pointer. Just one level of indirection and very fast performance.</p>
<p>We can also easily relocate or invalidate existing handles just by updating the entry in the handle manager to point to a new location or to flag it as removed.</p>
<p>Handles are the perfect reference to data that can change locations or even be removed, from data that needs to be serialized. Game entities are usually very dynamic, and are created and destroyed frequently (such as enemies spawning and being destroyed, or projectiles). So any references to game entities would be a good fit for handles, especially if this reference is held from another game entity and its state needs to be saved and restored. Examples of these types of relationships are the object a player is currently holding, or the target an enemy AI has locked onto.</p>
<h2>Getting Smarter</h2>
<p>The term smart pointers encompasses many different classes that give pointer-like syntax to reference data, but offer some extra features on top of “raw” pointers.</p>
<p>A common type of smart pointer deals with object lifetime. Smart pointers keep track of how many references there are to a particular piece of data, and free it when nobody is using it. For the runtime of games, I prefer to have very explicit object lifetime management, so I’m not a big fan of this kind of pointers. They can be of great help in development for tools written in C++ though.</p>
<p>Another kind of smart pointers insert an indirection between the data holding the pointer and the data being pointed. This allows data to be relocated, like we could do with handles. However, implementations of these pointers are often non- serializable, so they can be quite limiting.</p>
<p>If you consider using smart pointers from some of the popular libraries (STL, Boost) in your game, you should be very careful about the impact they can have on your build times. Including a single header file from one of those libraries will often pull in numerous other header files. Additionally, smart pointers are often templated, so the compiler will do some extra work generating code for each data type you instantiated templates on. All in all, templated smart pointers can have a significant impact in build times unless they are managed very carefully.</p>
<p>It’s possible to implement a smart pointer that wraps handles, provides a syntax like a regular pointer, and it still consists of a handle underneath, which can be serialized without any problem. But is the extra complexity of that layer worth the syntax benefits it provides? It will depend on your team and what you’re used to, but it’s always an option if the team is more comfortable dealing with pointers instead of handles.</p>
<h2>Conclusion</h2>
<p>There are many different approaches to expressing data relationships. It’s important to remember that different data types are better suited to some approaches than others. Pick the right method for your data and make sure it’s clear which one you’re using.</p>
<p>In the next few months, we’ll continue talking about data, and maybe even convince you that putting some love into your data can pay off big time with your code and the game as a whole.</p>
<p><br/></p>
<p><em>This article was originally printed in the September 2008 issue of <a href="http://gdmag.com">Game Developer</a>.<br />
</em></p>
<p><br/></p>
<p><a name="1"></a>[1] I'm not too happy about the strong distinction I was making between code and data. Really, data is any byte in memory, and that includes code. Most of the time programs are going to be managing references to non-code data, but sometimes to other code as well: function pointers, compiled shaders, compiled scripts, etc. So just ignore that distinction and think of data in a more generic way.</p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships&amp;title=Managing%20Data%20Relationships&amp;bodytext=I%27ve%20been%20meaning%20to%20put%20up%20the%20rest%20of%20the%20Inner%20Product%20columns%20I%20wrote%20for%20Game%20Developer%20Magazine%2C%20but%20I%20wasn%27t%20finding%20the%20time.%20With%20all%20the%20recent%20discussion%20on%20data-oriented%20design%2C%20I%20figured%20it%20was%20time%20to%20dust%20some%20of%20them%20off.%0A%0AThis%20was%20on" title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships&amp;title=Managing%20Data%20Relationships&amp;notes=I%27ve%20been%20meaning%20to%20put%20up%20the%20rest%20of%20the%20Inner%20Product%20columns%20I%20wrote%20for%20Game%20Developer%20Magazine%2C%20but%20I%20wasn%27t%20finding%20the%20time.%20With%20all%20the%20recent%20discussion%20on%20data-oriented%20design%2C%20I%20figured%20it%20was%20time%20to%20dust%20some%20of%20them%20off.%0A%0AThis%20was%20on" title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships&amp;t=Managing%20Data%20Relationships" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships&amp;title=Managing%20Data%20Relationships" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships&amp;title=Managing%20Data%20Relationships" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Managing%20Data%20Relationships&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fmanaging-data-relationships&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/managing-data-relationships/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Great Presentation on Data-Oriented Design</title>
		<link>http://gamesfromwithin.com/great-presentation-on-data-oriented-design</link>
		<comments>http://gamesfromwithin.com/great-presentation-on-data-oriented-design#comments</comments>
		<pubDate>Sun, 20 Dec 2009 22:07:56 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Data-oriented design]]></category>
		<category><![CDATA[Game tech]]></category>

		<guid isPermaLink="false">http://gamesfromwithin.com/?p=764</guid>
		<description><![CDATA[
			
				
			
		
A few days ago, Tony Albrecht posted the slides of his presentation titled &#8220;Pitfalls of Object-Oriented Design&#8221; [1]. Even though the title is really broad and could easily be misinterpreted, it&#8217;s not just a general bash on OOD. Instead, it&#8217;s very much focused on how object-oriented design is not a good match for high-performance apps [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="alignright size-full wp-image-773" title="Memory CPU gap" src="http://gamesfromwithin.com/wp-content/uploads/2009/12/mem_cpu_gap.jpg" alt="Memory CPU gap" width="300" height="164" />A few days ago, <a href="http://twitter.com/TonyAlbrecht">Tony Albrecht</a> posted the slides of his presentation titled <a href="http://research.scee.net/files/presentations/gcapaustralia09/Pitfalls_of_Object_Oriented_Programming_GCAP_09.pdf">&#8220;Pitfalls of Object-Oriented Design&#8221;</a> <a href="#1">[1]</a>. Even though the title is really broad and could easily be <a href="http://www.reddit.com/r/programming/comments/ag43j/pitfalls_of_object_oriented_programming_pdf/">misinterpreted</a>, it&#8217;s not just a general bash on OOD. Instead, it&#8217;s very much focused on how object-oriented design is not a good match for high-performance apps (games) on modern hardware architectures with slow memory access and deep memory hierarchies. His proposed solution: Data-oriented design. Spot on!</p>
<p>If you haven&#8217;t seen the presentation, go download it right now. It&#8217;s really well put together and he has some great detailed examples on how caches are affected with traditional object-oriented design vs. a more data-oriented approach.</p>
<p><br/></p>
<p><a name="1"></a>[1] Thanks to <a href="http://twitter.com/ChristerEricson/status/6783005918">Christer Ericson</a> for pointing that out.</p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design&amp;title=Great%20Presentation%20on%20Data-Oriented%20Design&amp;bodytext=A%20few%20days%20ago%2C%20Tony%20Albrecht%20posted%20the%20slides%20of%20his%20presentation%20titled%20%22Pitfalls%20of%20Object-Oriented%20Design%22%20%5B1%5D.%20Even%20though%20the%20title%20is%20really%20broad%20and%20could%20easily%20be%20misinterpreted%2C%20it%27s%20not%20just%20a%20general%20bash%20on%20OOD.%20Instead%2C%20it%27s%20very%20muc" title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design&amp;title=Great%20Presentation%20on%20Data-Oriented%20Design&amp;notes=A%20few%20days%20ago%2C%20Tony%20Albrecht%20posted%20the%20slides%20of%20his%20presentation%20titled%20%22Pitfalls%20of%20Object-Oriented%20Design%22%20%5B1%5D.%20Even%20though%20the%20title%20is%20really%20broad%20and%20could%20easily%20be%20misinterpreted%2C%20it%27s%20not%20just%20a%20general%20bash%20on%20OOD.%20Instead%2C%20it%27s%20very%20muc" title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design&amp;t=Great%20Presentation%20on%20Data-Oriented%20Design" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design&amp;title=Great%20Presentation%20on%20Data-Oriented%20Design" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design&amp;title=Great%20Presentation%20on%20Data-Oriented%20Design" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Great%20Presentation%20on%20Data-Oriented%20Design&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fgreat-presentation-on-data-oriented-design&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/great-presentation-on-data-oriented-design/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Data-Oriented Design (Or Why You Might Be Shooting Yourself in The Foot With OOP)</title>
		<link>http://gamesfromwithin.com/data-oriented-design</link>
		<comments>http://gamesfromwithin.com/data-oriented-design#comments</comments>
		<pubDate>Fri, 04 Dec 2009 19:40:20 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Data-oriented design]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Game tech]]></category>
		<category><![CDATA[game developer magazine]]></category>
		<category><![CDATA[inner product]]></category>

		<guid isPermaLink="false">http://gamesfromwithin.com/?p=723</guid>
		<description><![CDATA[
			
				
			
		
Picture this: Toward the end of the development cycle, your game crawls, but you don’t see any obvious hotspots in the profiler. The culprit? Random memory access patterns and constant cache misses. In an attempt to improve performance, you try to parallelize parts of the code, but it takes heroic efforts, and, in the end, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Picture this: Toward the end of the development cycle, your game crawls, but you don’t see any obvious hotspots in the profiler. The culprit? Random memory access patterns and constant cache misses. In an attempt to improve performance, you try to parallelize parts of the code, but it takes heroic efforts, and, in the end, you barely get much of a speed-up due to all the synchronization you had to add. To top it off, the code is so complex that fixing bugs creates more problems, and the thought of adding new features is discarded right away. Sound familiar?</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">That scenario pretty accurately describes almost every game I’ve been involved with for the last 10 years. The reasons aren’t the programming languages we’re using, nor the development tools, nor even a lack of discipline. In my experience, it’s object- oriented programming (OOP) and the culture that surrounds it that is in large part to blame for those problems. OOP could be hindering your project rather than helping it!</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">It&#8217;s All About Data</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">OOP is so ingrained in the current game development culture that it’s hard to think beyond objects when thinking about a game. After all, we’ve been creating classes representing vehicles, players, and state machines for many years. What are the alternatives? Procedural programming? Functional languages? Exotic programming languages?</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Data-oriented design is a different way to approach program design that addresses all these problems. Procedural programming focuses on procedure calls as its main element, and OOP deals primarily with objects. Notice that the main focus of both approaches is code: plain procedures (or functions) in one case, and grouped code associated with some internal state in the other. Data-oriented design shifts the perspective of programming from objects to the data itself: The type of the data, how it is laid out in memory, and how it will be read and processed in the game.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Programming, by definition, is about transforming data: It’s the act of creating a sequence of machine instructions describing how to process the input data and create some specific output data. A game is nothing more than a program that works at interactive rates, so wouldn’t it make sense for us to concentrate primarily on that data instead of on the code that manipulates it?</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">I’d like to clear up potential confusion and stress that data-oriented design does not imply that something is data- driven. A data-driven game is usually a game that exposes a large amount of functionality outside of code and lets the data determine the behavior of the game. That is an orthogonal concept to data-oriented design, and can be used with any type of programming approach.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Ideal Data</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">If we look at a program from the data point of view, what does the ideal data look like? It depends on the data and how it’s used. In general, the ideal data is in a format that we can use with the least amount of effort. In the best case, the format will be the same we expect as an output, so the processing is limited to just copying that data. Very often, our ideal data layout will be large blocks of contiguous, homogeneous data that we can process sequentially. In any case, the goal is to minimize the amount of transformations, and whenever possible, you should bake your data into this ideal format offline, during your asset-building process.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Because data-oriented design puts data first and foremost, we can architect our whole program around the ideal data format. We won’t always be able to make it exactly ideal (the same way that code is hardly ever by-the-book OOP), but it’s the primary goal to keep in mind. Once we achieve that, most of the problems I mentioned at the beginning of the column tend to melt away (more about that in the next section).</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">When we think about objects, we immediately think of trees— inheritance trees, containment trees, or message-passing trees, and our data is naturally arranged that way. As a result, when we perform an operation on an object, it will usually result in that object in turn accessing other objects further down in the tree. Iterating over a set of objects performing the same operation generates cascading, totally different operations at each object (see Figure 1a).</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">To achieve the best possible data layout, it’s helpful to break down each object into the different components, and group components of the same type together in memory, regardless of what object they came from. This organization results in large blocks of homogeneous data, which allow us to process the data sequentially (see Figure 1b). A key reason why data-oriented design is so powerful is because it works very well on large groups of objects. OOP, by definition, works on a single object. Step back for a minute and think of the last game you worked on: How many places in the code did you have only one of something? One enemy? One vehicle? One pathfinding node? One bullet? One particle? Never! Where there’s one, there are many. OOP ignores that and deals with each object in isolation. Instead, we can make things easy for us and for the hardware and organize our data to deal with the common case of having many items of the same type.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Does this sound like a strange approach? Guess what? You’re probably already doing this in some parts of your code: The particle system! Data-oriented design is turning our whole codebase into a gigantic particle system. Perhaps a name for this approach that would be more familiar to game programmers would have been particle-driven programming.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Advantages of Data-Oriented Design</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">hinking about data first and architecting the program based on that brings along lots of advantages.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Parallelization.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">These days, there’s no way around the fact that we need to deal with multiple cores. Anyone who has tried taking some OOP code and parallelizing it can attest how difficult, error prone, and possibly not very efficient that is. Often you end up adding lots of synchronization primitives to prevent concurrent access to data from multiple threads, and usually a lot of the threads end up idling for quite a while waiting for other threads to complete. As a result, the performance improvement can be quite underwhelming.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">When we apply data-oriented design, parallelization becomes a lot simpler: We have the input data, a small function to process it, and some output data. We can easily take something like that and split it among multiple threads with minimal synchronization between them. We can even take it further and run that code on processors with local memory (like the SPUs on the Cell processor) without having to do anything differently.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Cache utilization.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In addition to using multiple cores, one of the keys to achieving great performance in modern hardware, with its deep instruction pipelines and slow memory systems with multiple levels of caches, is having cache-friendly memory access. Data-oriented design results in very efficient use of the instruction cache because the same code is executed over and over. Also, if we lay out the data in large, contiguous blocks, we can process the data sequentially, getting nearly perfect data cache usage and great performance. Possible optimizations. When we think of objects or functions, we tend to get stuck optimizing at the function or even the algorithm level; Reordering some function calls, changing the sort method, or even re-writing some C code with assembly.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">That kind of optimization is certainly beneficial, but by thinking about the data first we can step further back and make larger, more important optimizations. Remember that all a game does is transform some data (assets, inputs, state) into some other data (graphics commands, new game states). By keeping in mind that flow of data, we can make higher-level, more intelligent decisions based on how the data is transformed, and how it is used. That kind of optimization can be extremely difficult and time- consuming to implement with more traditional OOP methods.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Modularity.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So far, all the advantages of data-oriented design have been based around performance: cache utilization, optimizations, and parallelization. There is no doubt that as game programmers, performance is an extremely important goal for us. There is often a conflict between techniques that improve performance and techniques that help readability and ease of development. For example, re-writing some code in assembly language can result in a performance boost, but usually makes the code harder to read and maintain.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Fortunately, data-oriented design is beneficial to both performance and ease of development. When you write code specifically to transform data, you end up with small functions, with very few dependencies on other parts of the code. The codebase ends up being very “flat,” with lots of leaf functions without many dependencies. This level of modularity and lack of dependences makes understanding, replacing, and updating the code much easier.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Testing.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">The last major advantage of data-oriented design is ease of testing. As we saw in the June and August Inner Product columns, writing unit tests to check object interactions is not trivial. You need to set up mocks and test things indirectly. Frankly, it’s a bit of a pain. On the other hand, when dealing directly with data, it couldn’t be easier to write unit tests: Create some input data, call the transform function, and check that the output data is what we expect. There’s nothing else to it. This is actually a huge advantage and makes code extremely easy to test, whether you’re doing test-driven development or just writing unit tests after the code.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Drawbacks of Data-Oriented Design</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Data-oriented design is not the silver bullet to all the problems in game development. It does help tremendously writing high-performance code and making programs more readable and easier to maintain, but it does come with a few drawbacks of its own.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">The main problem with data-oriented design is that it’s different from what most programmers are used to or learned in school. It requires turning our mental model of the program ninety degrees and changing how we think about it. It takes some practice before it becomes second-nature.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Also, because it’s a different approach, it can be challenging to interface with existing code, written in a more OOP or procedural way. It’s hard to write a single function in isolation, but as long as you can apply data-oriented design to a whole subsystem you should be able to reap a lot of the benefits.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Applying Data-Oriented Design</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Enough of the theory and overview. How do you actually get started with data-oriented design? To start with, just pick a specific area in your code: navigation, animations, collisions, or something else. Later on, when most of your game engine is centered around the data, you can worry about data flow all the way from the start of a frame until the end.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">The next step is to clearly identify the data inputs required by the system, and what kind of data it needs to generate. It’s OK to think about it in OOP terms for now, just to help us identify the data. For example, in an animation system, some of the input data is skeletons, base poses, animation data, and current state. The result is not “the code plays animations,” but the data generated by the animations that are currently playing. In this case, our outputs would be a new set of poses and an updated state.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">It’s important to take a step further and classify the input data based on how it is used. Is it read- only, read-write, or write-only? That classification will help guide design decisions about where to store it, and when to process it depending on dependencies with other parts of the program.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">At this point, stop thinking of the data required for a single operation, and think in terms of applying it to dozens or hundreds of entries. We no longer have one skeleton, one base pose, and a current state, and instead we have a block of each of those types with many instances in each of the blocks.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Think very carefully how the data is used during the transformation process from input to output. You might realize that you need to scan a particular field in a structure to perform a pass on the data, and then you need to use the results to do another pass. In that case, it might make more sense to split that initial field into a separate block of memory that can be processed independently, allowing for better cache utilization and potential parallelization. Or maybe you need to vectorize some part of the code, which requires fetching data from different locations to put it in the same vector register. In that case, that data can be stored contiguously so vector operations can be applied directly, without any extra transformations.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now you should have a very good understanding of your data. Writing the code to transform it is going to be much simpler. It’s like writing code by filling in the blanks. You’ll even be pleasantly surprised to realize that the code is much simpler and smaller than you thought in the first place, compared to what the equivalent OOP code would have been.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">If you think back about most of the topics we’ve covered in this column over the last year, you’ll see that they were all leading toward this type of design. Now it’s the time to be careful about how the data is aligned (Dec 2008 and Jan 2009), to bake data directly into an input format that you can use efficiently (Oct and Nov 2008), or to use non- pointer references between data blocks so they can be easily relocated (Sept 2009).</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Is Thre Room For OOP?</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Does this mean that OOP is useless and you should never apply it in your programs? I’m not quite ready to say that. Thinking in terms of objects is not detrimental when there is only one of each object (a graphics device, a log manager, etc) although in that case you might as well write it with simpler C-style functions and file-level static data. Even in that situation, it’s still important that those objects are designed around transforming data.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Another situation where I still find myself using OOP is GUI systems. Maybe it’s because you’re working with a system that is already designed in an object-oriented way, or maybe it’s because performance and complexity are not crucial factors with GUI code. In any case, I much prefer GUI APIs that are light on inheritance and use containment as much as possible (Cocoa and CocoaTouch are good examples of this). It’s very possible that a data-oriented GUI system could be written for games that would be a pleasure to work with, but I haven’t seen one yet.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Finally, there’s nothing stopping you from still having a mental picture of objects if that’s the way you like to think about the game. It’s just that the enemy entity won’t be all in the same physical location in memory. Instead, it will be split up into smaller subcomponents, each one forming part of a larger data table of similar components.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Data-oriented design is a bit of a departure from traditional programming approaches, but by always thinking about the data and how it needs to be transformed, you’ll be able to reap huge benefits both in terms of performance and ease of development.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Thanks to Mike Acton and Jim Tilander for challenging my ideas over the years and for their feedback on this article.</div>
<p>Picture this: Toward the end of the development cycle, your game crawls, but you don’t see any obvious hotspots in the profiler. The culprit? Random memory access patterns and constant cache misses. In an attempt to improve performance, you try to parallelize parts of the code, but it takes heroic efforts, and, in the end, you barely get much of a speed-up due to all the synchronization you had to add. To top it off, the code is so complex that fixing bugs creates more problems, and the thought of adding new features is discarded right away. Sound familiar?</p>
<p>That scenario pretty accurately describes almost every game I’ve been involved with for the last 10 years. The reasons aren’t the programming languages we’re using, nor the development tools, nor even a lack of discipline. In my experience, it’s object- oriented programming (OOP) and the culture that surrounds it that is in large part to blame for those problems. OOP could be hindering your project rather than helping it!</p>
<h2>It&#8217;s All About Data</h2>
<p>OOP is so ingrained in the current game development culture that it’s hard to think beyond objects when thinking about a game. After all, we’ve been creating classes representing vehicles, players, and state machines for many years. What are the alternatives? Procedural programming? Functional languages? Exotic programming languages?</p>
<p>Data-oriented design is a different way to approach program design that addresses all these problems. Procedural programming focuses on procedure calls as its main element, and OOP deals primarily with objects. Notice that the main focus of both approaches is code: plain procedures (or functions) in one case, and grouped code associated with some internal state in the other. Data-oriented design shifts the perspective of programming from objects to the data itself: The type of the data, how it is laid out in memory, and how it will be read and processed in the game.</p>
<p>Programming, by definition, is about transforming data: It’s the act of creating a sequence of machine instructions describing how to process the input data and create some specific output data. A game is nothing more than a program that works at interactive rates, so wouldn’t it make sense for us to concentrate primarily on that data instead of on the code that manipulates it?</p>
<p>I’d like to clear up potential confusion and stress that data-oriented design does not imply that something is data- driven. A data-driven game is usually a game that exposes a large amount of functionality outside of code and lets the data determine the behavior of the game. That is an orthogonal concept to data-oriented design, and can be used with any type of programming approach.</p>
<h2>Ideal Data</h2>
<div id="attachment_726" class="wp-caption alignright" style="width: 239px"><img class="size-full wp-image-726 " title="oo_design" src="http://gamesfromwithin.com/wp-content/uploads/2009/12/oo_design.png" alt="Call sequence with an object-oriented approach" width="229" height="247" /><p class="wp-caption-text">Figure 1a. Call sequence with an object-oriented approach</p></div>
<p>If we look at a program from the data point of view, what does the ideal data look like? It depends on the data and how it’s used. In general, the ideal data is in a format that we can use with the least amount of effort. In the best case, the format will be the same we expect as an output, so the processing is limited to just copying that data. Very often, our ideal data layout will be large blocks of contiguous, homogeneous data that we can process sequentially. In any case, the goal is to minimize the amount of transformations, and whenever possible, you should bake your data into this ideal format offline, during your asset-building process.</p>
<p>Because data-oriented design puts data first and foremost, we can architect our whole program around the ideal data format. We won’t always be able to make it exactly ideal (the same way that code is hardly ever by-the-book OOP), but it’s the primary goal to keep in mind. Once we achieve that, most of the problems I mentioned at the beginning of the column tend to melt away (more about that in the next section).</p>
<p>When we think about objects, we immediately think of trees— inheritance trees, containment trees, or message-passing trees, and our data is naturally arranged that way. As a result, when we perform an operation on an object, it will usually result in that object in turn accessing other objects further down in the tree. Iterating over a set of objects performing the same operation generates cascading, totally different operations at each object (see Figure 1a).</p>
<div id="attachment_728" class="wp-caption alignright" style="width: 243px"><img class="size-full wp-image-728 " title="do_design" src="http://gamesfromwithin.com/wp-content/uploads/2009/12/do_design1.png" alt="Call sequence with a data-oriented approach" width="233" height="297" /><p class="wp-caption-text">Figure 1b. Call sequence with a data-oriented approach</p></div>
<p>To achieve the best possible data layout, it’s helpful to break down each object into the different components, and group components of the same type together in memory, regardless of what object they came from. This organization results in large blocks of homogeneous data, which allow us to process the data sequentially (see Figure 1b). A key reason why data-oriented design is so powerful is because it works very well on large groups of objects. OOP, by definition, works on a single object. Step back for a minute and think of the last game you worked on: How many places in the code did you have only one of something? One enemy? One vehicle? One pathfinding node? One bullet? One particle? Never! Where there’s one, there are many. OOP ignores that and deals with each object in isolation. Instead, we can make things easy for us and for the hardware and organize our data to deal with the common case of having many items of the same type.</p>
<p>Does this sound like a strange approach? Guess what? You’re probably already doing this in some parts of your code: The particle system! Data-oriented design is turning our whole codebase into a gigantic particle system. Perhaps a name for this approach that would be more familiar to game programmers would have been particle-driven programming.</p>
<h2>Advantages of Data-Oriented Design</h2>
<p>Thinking about data first and architecting the program based on that brings along lots of advantages.</p>
<h3>Parallelization.</h3>
<p>These days, there’s no way around the fact that we need to deal with multiple cores. Anyone who has tried taking some OOP code and parallelizing it can attest how difficult, error prone, and possibly not very efficient that is. Often you end up adding lots of synchronization primitives to prevent concurrent access to data from multiple threads, and usually a lot of the threads end up idling for quite a while waiting for other threads to complete. As a result, the performance improvement can be quite underwhelming.</p>
<p>When we apply data-oriented design, parallelization becomes a lot simpler: We have the input data, a small function to process it, and some output data. We can easily take something like that and split it among multiple threads with minimal synchronization between them. We can even take it further and run that code on processors with local memory (like the SPUs on the Cell processor) without having to do anything differently.</p>
<h3>Cache utilization.</h3>
<p>In addition to using multiple cores, one of the keys to achieving great performance in modern hardware, with its deep instruction pipelines and slow memory systems with multiple levels of caches, is having cache-friendly memory access. Data-oriented design results in very efficient use of the instruction cache because the same code is executed over and over. Also, if we lay out the data in large, contiguous blocks, we can process the data sequentially, getting nearly perfect data cache usage and great performance. Possible optimizations. When we think of objects or functions, we tend to get stuck optimizing at the function or even the algorithm level; Reordering some function calls, changing the sort method, or even re-writing some C code with assembly.</p>
<p>That kind of optimization is certainly beneficial, but by thinking about the data first we can step further back and make larger, more important optimizations. Remember that all a game does is transform some data (assets, inputs, state) into some other data (graphics commands, new game states). By keeping in mind that flow of data, we can make higher-level, more intelligent decisions based on how the data is transformed, and how it is used. That kind of optimization can be extremely difficult and time- consuming to implement with more traditional OOP methods.</p>
<h3>Modularity.</h3>
<p>So far, all the advantages of data-oriented design have been based around performance: cache utilization, optimizations, and parallelization. There is no doubt that as game programmers, performance is an extremely important goal for us. There is often a conflict between techniques that improve performance and techniques that help readability and ease of development. For example, re-writing some code in assembly language can result in a performance boost, but usually makes the code harder to read and maintain.</p>
<p>Fortunately, data-oriented design is beneficial to both performance and ease of development. When you write code specifically to transform data, you end up with small functions, with very few dependencies on other parts of the code. The codebase ends up being very “flat,” with lots of leaf functions without many dependencies. This level of modularity and lack of dependences makes understanding, replacing, and updating the code much easier.</p>
<h3>Testing.</h3>
<p>The last major advantage of data-oriented design is ease of testing. As we saw in the June and August Inner Product columns, writing unit tests to check object interactions is not trivial. You need to set up mocks and test things indirectly. Frankly, it’s a bit of a pain. On the other hand, when dealing directly with data, it couldn’t be easier to write unit tests: Create some input data, call the transform function, and check that the output data is what we expect. There’s nothing else to it. This is actually a huge advantage and makes code extremely easy to test, whether you’re doing test-driven development or just writing unit tests after the code.</p>
<h2>Drawbacks of Data-Oriented Design</h2>
<p>Data-oriented design is not the silver bullet to all the problems in game development. It does help tremendously writing high-performance code and making programs more readable and easier to maintain, but it does come with a few drawbacks of its own.</p>
<p>The main problem with data-oriented design is that it’s different from what most programmers are used to or learned in school. It requires turning our mental model of the program ninety degrees and changing how we think about it. It takes some practice before it becomes second-nature.</p>
<p>Also, because it’s a different approach, it can be challenging to interface with existing code, written in a more OOP or procedural way. It’s hard to write a single function in isolation, but as long as you can apply data-oriented design to a whole subsystem you should be able to reap a lot of the benefits.</p>
<h2>Applying Data-Oriented Design</h2>
<p>Enough of the theory and overview. How do you actually get started with data-oriented design? To start with, just pick a specific area in your code: navigation, animations, collisions, or something else. Later on, when most of your game engine is centered around the data, you can worry about data flow all the way from the start of a frame until the end.</p>
<p>The next step is to clearly identify the data inputs required by the system, and what kind of data it needs to generate. It’s OK to think about it in OOP terms for now, just to help us identify the data. For example, in an animation system, some of the input data is skeletons, base poses, animation data, and current state. The result is not “the code plays animations,” but the data generated by the animations that are currently playing. In this case, our outputs would be a new set of poses and an updated state.</p>
<p>It’s important to take a step further and classify the input data based on how it is used. Is it read- only, read-write, or write-only? That classification will help guide design decisions about where to store it, and when to process it depending on dependencies with other parts of the program.</p>
<p>At this point, stop thinking of the data required for a single operation, and think in terms of applying it to dozens or hundreds of entries. We no longer have one skeleton, one base pose, and a current state, and instead we have a block of each of those types with many instances in each of the blocks.</p>
<p>Think very carefully how the data is used during the transformation process from input to output. You might realize that you need to scan a particular field in a structure to perform a pass on the data, and then you need to use the results to do another pass. In that case, it might make more sense to split that initial field into a separate block of memory that can be processed independently, allowing for better cache utilization and potential parallelization. Or maybe you need to vectorize some part of the code, which requires fetching data from different locations to put it in the same vector register. In that case, that data can be stored contiguously so vector operations can be applied directly, without any extra transformations.</p>
<p>Now you should have a very good understanding of your data. Writing the code to transform it is going to be much simpler. It’s like writing code by filling in the blanks. You’ll even be pleasantly surprised to realize that the code is much simpler and smaller than you thought in the first place, compared to what the equivalent OOP code would have been.</p>
<p>If you think back about most of the topics we’ve covered in this column over the last year, you’ll see that they were all leading toward this type of design. Now it’s the time to be careful about how the data is aligned (Dec 2008 and Jan 2009), to bake data directly into an input format that you can use efficiently (Oct and Nov 2008), or to use non- pointer references between data blocks so they can be easily relocated (Sept 2009).</p>
<h2>Is There Room For OOP?</h2>
<p>Does this mean that OOP is useless and you should never apply it in your programs? I’m not quite ready to say that. Thinking in terms of objects is not detrimental when there is only one of each object (a graphics device, a log manager, etc) although in that case you might as well write it with simpler C-style functions and file-level static data. Even in that situation, it’s still important that those objects are designed around transforming data.</p>
<p>Another situation where I still find myself using OOP is GUI systems. Maybe it’s because you’re working with a system that is already designed in an object-oriented way, or maybe it’s because performance and complexity are not crucial factors with GUI code. In any case, I much prefer GUI APIs that are light on inheritance and use containment as much as possible (Cocoa and CocoaTouch are good examples of this). It’s very possible that a data-oriented GUI system could be written for games that would be a pleasure to work with, but I haven’t seen one yet.</p>
<p>Finally, there’s nothing stopping you from still having a mental picture of objects if that’s the way you like to think about the game. It’s just that the enemy entity won’t be all in the same physical location in memory. Instead, it will be split up into smaller subcomponents, each one forming part of a larger data table of similar components.</p>
<p>Data-oriented design is a bit of a departure from traditional programming approaches, but by always thinking about the data and how it needs to be transformed, you’ll be able to reap huge benefits both in terms of performance and ease of development.</p>
<p><br/><br/></p>
<p>Thanks to <a href="http://www.cellperformance.com/mike_acton">Mike Acton</a> and <a href="http://www.tilander.org/aurora/">Jim Tilander</a> for challenging my ideas over the years and for their feedback on this article.</p>
<p>This article was originally printed in the September 2009 issue of <a href="http://gdmag.com">Game Developer</a>.</p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design&amp;title=Data-Oriented%20Design%20%28Or%20Why%20You%20Might%20Be%20Shooting%20Yourself%20in%20The%20Foot%20With%20OOP%29&amp;bodytext=Picture%20this%3A%20Toward%20the%20end%20of%20the%20development%20cycle%2C%20your%20game%20crawls%2C%20but%20you%20don%E2%80%99t%20see%20any%20obvious%20hotspots%20in%20the%20profiler.%20The%20culprit%3F%20Random%20memory%20access%20patterns%20and%20constant%20cache%20misses.%20In%20an%20attempt%20to%20improve%20performance%2C%20you%20try%20to%20" title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design&amp;title=Data-Oriented%20Design%20%28Or%20Why%20You%20Might%20Be%20Shooting%20Yourself%20in%20The%20Foot%20With%20OOP%29&amp;notes=Picture%20this%3A%20Toward%20the%20end%20of%20the%20development%20cycle%2C%20your%20game%20crawls%2C%20but%20you%20don%E2%80%99t%20see%20any%20obvious%20hotspots%20in%20the%20profiler.%20The%20culprit%3F%20Random%20memory%20access%20patterns%20and%20constant%20cache%20misses.%20In%20an%20attempt%20to%20improve%20performance%2C%20you%20try%20to%20" title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design&amp;t=Data-Oriented%20Design%20%28Or%20Why%20You%20Might%20Be%20Shooting%20Yourself%20in%20The%20Foot%20With%20OOP%29" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design&amp;title=Data-Oriented%20Design%20%28Or%20Why%20You%20Might%20Be%20Shooting%20Yourself%20in%20The%20Foot%20With%20OOP%29" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design&amp;title=Data-Oriented%20Design%20%28Or%20Why%20You%20Might%20Be%20Shooting%20Yourself%20in%20The%20Foot%20With%20OOP%29" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Data-Oriented%20Design%20%28Or%20Why%20You%20Might%20Be%20Shooting%20Yourself%20in%20The%20Foot%20With%20OOP%29&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fdata-oriented-design&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/data-oriented-design/feed</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Dirty Coding Tricks</title>
		<link>http://gamesfromwithin.com/dirty-coding-tricks</link>
		<comments>http://gamesfromwithin.com/dirty-coding-tricks#comments</comments>
		<pubDate>Fri, 21 Aug 2009 02:11:45 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[Game tech]]></category>
		<category><![CDATA[game developer magazine]]></category>

		<guid isPermaLink="false">http://gamesfromwithin.com/?p=604</guid>
		<description><![CDATA[
			
				
			
		
If you haven&#8217;t read it already on Game Developer Magazine, don&#8217;t miss the Dirty Coding Tricks feature article on Gamasutra. I contributed two &#8220;dirty tricks&#8221; used in some of my past projects.
Identity Crisis deals with the horrors of a CRC clash on a resource right before submitting the gold master. And The Programming Antihero shows [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="alignright" src="http://www.gamasutra.com/db_area/images/feature/4111/2.jpg" alt="" width="175" height="175" />If you haven&#8217;t read it already on Game Developer Magazine, don&#8217;t miss the <a href="http://www.gamasutra.com/view/feature/4111/dirty_coding_tricks.php">Dirty Coding Tricks feature article</a> on Gamasutra. I contributed two &#8220;dirty tricks&#8221; used in some of my past projects.</p>
<p><em>Identity Crisis </em>deals with the horrors of a CRC clash on a resource right before submitting the gold master. And <em>The Programming Antihero</em> shows a fairly common trick in the games industry to get that extra memory after the artists and designers swear up and down that they can&#8217;t cut any more content.</p>
<p>Nothing like a good dose of reality to make everybody feel better about their own code <img src='http://gamesfromwithin.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks&amp;title=Dirty%20Coding%20Tricks&amp;bodytext=If%20you%20haven%27t%20read%20it%20already%20on%20Game%20Developer%20Magazine%2C%20don%27t%20miss%20the%20Dirty%20Coding%20Tricks%20feature%20article%20on%20Gamasutra.%20I%20contributed%20two%20%22dirty%20tricks%22%20used%20in%20some%20of%20my%20past%20projects.%0D%0A%0D%0AIdentity%20Crisis%20deals%20with%20the%20horrors%20of%20a%20CRC%20clash%20on" title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks&amp;title=Dirty%20Coding%20Tricks&amp;notes=If%20you%20haven%27t%20read%20it%20already%20on%20Game%20Developer%20Magazine%2C%20don%27t%20miss%20the%20Dirty%20Coding%20Tricks%20feature%20article%20on%20Gamasutra.%20I%20contributed%20two%20%22dirty%20tricks%22%20used%20in%20some%20of%20my%20past%20projects.%0D%0A%0D%0AIdentity%20Crisis%20deals%20with%20the%20horrors%20of%20a%20CRC%20clash%20on" title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks&amp;t=Dirty%20Coding%20Tricks" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks&amp;title=Dirty%20Coding%20Tricks" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks&amp;title=Dirty%20Coding%20Tricks" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Dirty%20Coding%20Tricks&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fdirty-coding-tricks&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/dirty-coding-tricks/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Back to The Future (Part 2)</title>
		<link>http://gamesfromwithin.com/back-to-the-future-part-2</link>
		<comments>http://gamesfromwithin.com/back-to-the-future-part-2#comments</comments>
		<pubDate>Mon, 15 Sep 2008 00:54:34 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[Game tech]]></category>
		<category><![CDATA[game developer magazine]]></category>
		<category><![CDATA[inner product]]></category>

		<guid isPermaLink="false">http://www.gamesfromwithin.dreamhosters.com/?p=450</guid>
		<description><![CDATA[I really enjoy a good cup of tea. On the surface, making tea is really easy: take some tea leaves, pour some hot water over them, and wait a few minutes. In practice, the difference between a bitter, undrinkable brew, and a perfect cup of tea is all in the details; the type and amount of tea, the temperature of the water, and the steeping, time all make a huge difference. A playback system for a game is very much the same. As we saw last month, the basic idea is really simple: Record all game inputs, make the game deterministic, and you get the same playback every time. Unfortunately things aren't quite that simple in real life. Just as with tea making, the secret to a perfect playback system is all in the details.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p>I really enjoy a good cup of tea. On the surface, making tea is really easy: take some tea leaves, pour some hot water over them, and wait a few minutes. In practice, the difference between a bitter, undrinkable brew, and a perfect cup of tea is all in the details; the type and amount of tea, the temperature of the water, and the steeping, time all make a huge difference. A playback system for a game is very much the same. As we saw last month, the basic idea is really simple: Record all game inputs, make the game deterministic, and you get the same playback every time. Unfortunately things aren&#8217;t quite that simple in real life. Just as with tea making, the secret to a perfect playback system is all in the details.<span id="more-103"></span></p>
<p>Let&#8217;s look at some of the common problems that can trip up the playback system and what we can do about them. We&#8217;ll warm up by starting with the easiest ones and work our way to the hardest ones.</p>
<h3>Game Versions</h3>
<p>A playback of a game recorded with a different version is almost guaranteed to end up with different results than the original version. Any minor tweaks to AI parameters, pathfinding algorithms, or just about anything is most likely going to generate a different game state. It doesn&#8217;t even have to be a change in code either: adding a new character or moving a trigger volume will also cause different playbacks.</p>
<p>In general, you&#8217;re best off avoiding using recorded inputs from previous versions of the game. Inserting the game build version at the beginning of the recorded game state file allows you to detect version mismatches during playback. If a mismatch is detected, you can either print a warning or give up on the state verification altogether. This check will be particularly useful if you&#8217;re sharing recorded sessions across the office (perhaps through a bug database) and people are on slightly different versions.</p>
<p>The idea of recording a session in release mode (all optimizations enabled, few debugging checks in place), and playing it back in debug mode is very tempting. Unfortunately, mixing and matching configurations is almost guaranteed not to work. Middleware libraries will often have slightly different behaviors in debug and release, which will throw off playback right away. But most likely so does your own code, and often in ways you might not expect. For example, you might have different floating point rounding modes in debug and release. Or even a more subtle difference: depending on the optimizations you have turned on, in debug mode a float variable might be copied back to memory after each operation (which rounds it back to a float), whereas in release the program might execute several operations in a row on that value before writing it back to main memory and causing the rounding only once. The results are going to be very close, but they&#8217;ll often be cumulative and the simulation will quickly diverge.</p>
<p>As a general rule, treat debug and release builds as if they were different versions. Make sure to include which configuration it is along with the build number in the game state file so you can check for that as well.</p>
<h3>Memory State</h3>
<p>Reading data from uninitialized memory is probably the number one cause of non-determinism in games. Uninitialized memory will contain whatever data was stored there before. That can mean anything from sane-looking values to total garbage. What&#8217;s worse, they&#8217;re going to change from run to run, so if your game ever uses the values contained in uninitialized memory, playback is not going to be deterministic.</p>
<p>The most common case of using uninitialized memory happens by reading a variable that was declared and not initialized. This mistake is easy to see when we&#8217;re dealing with a standalone variable, but when it becomes a member variable hidden inside some class, it becomes a lot less obvious. It&#8217;s also important to notice that this will only happen when we declare variables on the stack or we create them dynamically on the heap. For global and static variables, the C runtime takes care of initializing them all to zero for us.</p>
<p>Cranking up the compiler warning level to the maximum will allow it to catch some of the most obvious cases of reading uninitialized variables. For extra checks, I recommend a static code analysis program such as <a href="http://www.gimpel.com/html/lintinfo.htm">PC Lint.</a> That&#8217;s almost guaranteed to catch every use of uninitialized variables (along with ten thousand other things, some of them extremely useful, though most of them you probably won&#8217;t care about at all).</p>
<p>Another common mistake is to read past the end of a valid memory area, such as an array. The values read from memory in that case are going to depend on where that array is, and what happened before. In any case, it&#8217;s going to become a source of non-determinsim, and it&#8217;s a clear bug that should be fixed right away.</p>
<p>If you ever find that your game is deterministic in debug mode but not in release mode, start by suspecting access to uninitialized memory. In debug mode, a lot of platforms will fill the memory heap with specific bit patterns describing the memory status: allocated, freed, etc. Those patterns can be really useful to track down problems with memory allocations, but they also set memory values to a consistent state, which will make debug builds seem deterministic when they really aren&#8217;t.</p>
<h3>Pointer Values</h3>
<p>Unless you&#8217;re working on a platform over which you have total control, and you have a very strict memory allocation scheme, you should probably never rely on the actual numerical value of your pointer variables. The pointer values will change from run to run, and will depend on what happened before, including what other programs were running at the time.</p>
<p>If you&#8217;re puzzled at the idea of using the pointer values as part of the logic in your program (and you should be), you still need to watch out for this possibility. Some well-known, open source compression libraries rely on this technique by hashing pointer values. This means that two consecutive runs of the same program might end up with two slightly different results.</p>
<p>In general, it&#8217;s considered a good practice to avoid using pointer values for anything other than dereferencing them and accessing the data they&#8217;re pointing to. Making decisions based on their actual numerical values is asking for trouble, and you can almost always achieve the same result by using offsets between pointer values (as long as you know they&#8217;re coming from the same memory block), which has none of those problems.</p>
<h3>Asynchronous File I/O</h3>
<p>This is where things start to get fun. A lot of games perform background loads while the game is running. Whenever the game detects it will need an asset, it initiates an asynchronous load. When the load is complete, the game is notified and, optionally, does some processing on that asset. There is no guarantee about when exactly the load will complete, which means that each playback has the potential to be slightly different.</p>
<p>If the background load just brings in more mipmaps for a texture, it&#8217;s not going to affect the simulation whether it happened a frame earlier or a frame later. On the other hand, if it&#8217;s loading a more detailed AI navigation path, it can definitely affect the position and state of AI units. Even if you&#8217;re not loading data that affects the simulation, it&#8217;s very useful to have asynchronous file IO work deterministically to catch bugs more reliably. For example, the game might crash if a background texture load completes the same frame as a line of dialog sound is requested. As any programmer who&#8217;s ever had to debug a problem like this will tell you, being able to reliably reproduce that crash is worth a small fortune.</p>
<p>We can turn asynchronous file loading into a deterministic operation by considering the read completions as inputs to the game. Whenever an asynchronous file load completes, we record it in the game input file at the corresponding frame. During playback, the game will request background loads as usual, but completion events are made available only when they&#8217;re read from the input stream.</p>
<p>This can be easily implemented at the game level by buffering all background read completions. During recording and normal game operation, background read completion events are made available as soon as they happen. During playback, however, background read completion events need to be available the exact same frame they happened in the original session. If the read finishes earlier, it is simply buffered for a few frames until the correct time. If the read hasn&#8217;t completed by the time it finished in the original session, the game blocks until it&#8217;s done and it&#8217;s made available right away. That means that playback might be a bit choppy at times, while the game blocks for data to be read, but it will ensure that playback is fully deterministic. Notice that blocking the game while waiting for a read to complete is not going to affect subsequent frames with a larger delta time because we&#8217;re also reading the system clock from the input<br />
stream.</p>
<h3>Network Data</h3>
<p>What about online games? Our playback method has been concerned exclusively with local players. Playtesting and Q/A on online games is much more expensive and time consuming than single-player games because of the manpower required and the coordination necessary to set the games up. Wouldn&#8217;t it be great to be able to replay a 30-person game that resulted in a crash without having to get 30 players again?</p>
<p>Data received from the network most definitely influences the game itself, so it&#8217;s another input to the game. We could treat all network traffic like any of the other inputs. Record it along with the other game input and play it back by inserting the network packets as if they had come from the network card. That would work, but it&#8217;s a bit more complicated than it has to be. Fully emulating the network traffic, connection status, and everything else can be a bit tricky to get just right.</p>
<p>A simpler approach consists of translating the data received through the network into higher-level game actions. For example, whenever a player fires a weapon, it results in a network packet, which is translated into a game-level action whenever it&#8217;s received. Then, the local simulation applies the action that causes that player to fire locally. You probably already have a system like that in place in your multiplayer game anyway. Recording those actions is a much simpler task than the raw network traffic, and playing back a recorded multiplayer game is just a matter of inserting the game-level actions at the same time they occurred, just like any<br />
other input.</p>
<p>The game input file is going to grow significantly as soon as you start recording all the actions created by the players through the network. Fortunately, most games are extremely careful to minimize network bandwidth, so the input file will remain at a reasonable size even recording all that data.</p>
<h3>Threads And Multiprocessors</h3>
<p>In today&#8217;s games, multithreading is an inevitable reality. Even if your code isn&#8217;t explicitly multithreaded, parts of your engine and middleware are probably using multiple threads. The problem with threads is that you never know exactly when one thread stops working and another one resumes. So it is possible that two events in two different threads will happen in different order in two runs of the game. To make things even more fun, most of today&#8217;s platforms have multiple cores, making determinism even more complicated. Do we need to give up determinism in a thread-dominated world?</p>
<p>The totally honest answer is that we&#8217;ve already picked all the low-hanging fruit. If you really want your game to be 100 percent deterministic while running in multiple threads over multiple cores, get ready to roll up your sleeves and do some serious work. For the rest of us, we can still get most of the benefits of a playback system without total determinism. That means that there is potential for bugs caused by thread interactions that might not be repeatable from run to run. But at least the game simulation will be the same for every playback, so the recording technique should still be very useful.</p>
<p>In the easiest case, the simulation runs on a single thread, with the rest of the threads dedicated to graphics, sound, and other systems. If we apply all the techniques we&#8217;ve covered so far, the simulation itself should be deterministic and we shouldn&#8217;t have to do anything different than we did in the singlethreaded case.</p>
<p>However, as soon as the simulation is spread across multiple threads, we need to be a lot more careful. Even if we have no control over the thread context switches, we can at least ensure that major events happen in the same order no matter how they were executed. For example, if a worker thread creates a set of actions to be executed, we can sort those actions before processing them. If that&#8217;s not an option (because those actions are processed as soon as they are created, for example), we could record the creation order as part of the input recording, although enforcing that order during playback might have far reaching consequences for many systems deep inside the game.</p>
<p>If achieving 100 percent determinism in a threaded environment is important to your project, have a look at <a href="http://www.replaysolutions.com/technology">Replay Director</a>. It&#8217;s a commercial tool and set of libraries that gives you very accurate recordings and playbacks of your game, including thread context switches, with little extra programming on your part.</p>
<h3>More Than Just For Debugging</h3>
<p>At this point, if you&#8217;ve applied all the techniques we&#8217;ve discussed so far, you should have a pretty bullet-proof recording and playback system: lightweight, reliable, and accurate. It will be a great help as a debugging tool, but there&#8217;s no reason to stop there. You could use the same system to record demo sequences to show off in presentations without the need to make a movie capture and degrade the image quality. You can also truthfully claim that it&#8217;s a live demo, running on the game itself, not a canned movie, which always carries extra weight with most audiences.</p>
<p>You can even integrate the playback system as a key feature in your game. Halo 3 already does this by allowing you to replay any play session, examine what happen exactly, and let you take screenshots of the juiciest moments. It can be a great feature for a lot of games for almost no extra effort over what you&#8217;ve already implemented. The main problem will be to make sure future updates to the game don&#8217;t affect the playback of older gameplay sessions. This is really hard to achieve, since the most insignificant change can end up causing the simulation to diverge in unexpected ways. So if you&#8217;re going to rely on it as a game feature, you need a suite of comprehensive tests verifying that nothing changes whenever the game is updated. Last month we saw how to verify that a playback results in the same game state as the original session, so again, there&#8217;s very little extra work there.</p>
<p>With all these techniques in your toolbox, you should be able to make your game pretty close to 100 percent deterministic—enough to have a solid playback system and use it to its fullest during production, and maybe even as a game feature.</p>
<p>This article was originally printed in the June 2008 issue of <a href="http://www.gdmag.com/homepage.htm">Game Developer</a>.</p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2&amp;title=Back%20to%20The%20Future%20%28Part%202%29&amp;bodytext=I%20really%20enjoy%20a%20good%20cup%20of%20tea.%20On%20the%20surface%2C%20making%20tea%20is%20really%20easy%3A%20take%20some%20tea%20leaves%2C%20pour%20some%20hot%20water%20over%20them%2C%20and%20wait%20a%20few%20minutes.%20In%20practice%2C%20the%20difference%20between%20a%20bitter%2C%20undrinkable%20brew%2C%20and%20a%20perfect%20cup%20of%20tea%20is%20all%20in%20the%20details%3B%20the%20type%20and%20amount%20of%20tea%2C%20the%20temperature%20of%20the%20water%2C%20and%20the%20steeping%2C%20time%20all%20make%20a%20huge%20difference.%20A%20playback%20system%20for%20a%20game%20is%20very%20much%20the%20same.%20As%20we%20saw%20last%20month%2C%20the%20basic%20idea%20is%20really%20simple%3A%20Record%20all%20game%20inputs%2C%20make%20the%20game%20deterministic%2C%20and%20you%20get%20the%20same%20playback%20every%20time.%20Unfortunately%20things%20aren%27t%20quite%20that%20simple%20in%20real%20life.%20Just%20as%20with%20tea%20making%2C%20the%20secret%20to%20a%20perfect%20playback%20system%20is%20all%20in%20the%20details." title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2&amp;title=Back%20to%20The%20Future%20%28Part%202%29&amp;notes=I%20really%20enjoy%20a%20good%20cup%20of%20tea.%20On%20the%20surface%2C%20making%20tea%20is%20really%20easy%3A%20take%20some%20tea%20leaves%2C%20pour%20some%20hot%20water%20over%20them%2C%20and%20wait%20a%20few%20minutes.%20In%20practice%2C%20the%20difference%20between%20a%20bitter%2C%20undrinkable%20brew%2C%20and%20a%20perfect%20cup%20of%20tea%20is%20all%20in%20the%20details%3B%20the%20type%20and%20amount%20of%20tea%2C%20the%20temperature%20of%20the%20water%2C%20and%20the%20steeping%2C%20time%20all%20make%20a%20huge%20difference.%20A%20playback%20system%20for%20a%20game%20is%20very%20much%20the%20same.%20As%20we%20saw%20last%20month%2C%20the%20basic%20idea%20is%20really%20simple%3A%20Record%20all%20game%20inputs%2C%20make%20the%20game%20deterministic%2C%20and%20you%20get%20the%20same%20playback%20every%20time.%20Unfortunately%20things%20aren%27t%20quite%20that%20simple%20in%20real%20life.%20Just%20as%20with%20tea%20making%2C%20the%20secret%20to%20a%20perfect%20playback%20system%20is%20all%20in%20the%20details." title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2&amp;t=Back%20to%20The%20Future%20%28Part%202%29" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2&amp;title=Back%20to%20The%20Future%20%28Part%202%29" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2&amp;title=Back%20to%20The%20Future%20%28Part%202%29" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Back%20to%20The%20Future%20%28Part%202%29&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-2&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/back-to-the-future-part-2/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Back to The Future (Part 1)</title>
		<link>http://gamesfromwithin.com/back-to-the-future-part-1</link>
		<comments>http://gamesfromwithin.com/back-to-the-future-part-1#comments</comments>
		<pubDate>Fri, 22 Aug 2008 03:27:47 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[Game tech]]></category>
		<category><![CDATA[game developer magazine]]></category>
		<category><![CDATA[inner product]]></category>

		<guid isPermaLink="false">http://www.gamesfromwithin.dreamhosters.com/?p=449</guid>
		<description><![CDATA[How would you like to be able to reproduce every crash report that QA adds to the bug database quickly and reliably? How useful would it be to be able to put a breakpoint the frame before a crash bug happens? You can do all that and more if your game is deterministic and you feed it the same inputs as an earlier run. Sounds easy? It is, if you implement it early on and you keep it that way during development.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p><em> Insanity: doing the same thing over and over again and expecting different results. </em>– (attributed) Albert Einstein</p>
<p>How would you like to be able to reproduce every crash report that QA adds to the bug database quickly and reliably? How useful would it be to be able to put a breakpoint the frame before a crash bug happens?</p>
<p>You can do all that and more if your game is deterministic and you feed it the same inputs as an earlier run. Sounds easy? It is, if you implement it early on and you keep it that way during development. If you choose not to make your game deterministic, your team will go insane by Einstein’s definition, and maybe by a few other definitions as well by the time the project ends.<span id="more-102"></span></p>
<h3>Determinism</h3>
<p>A system is said to be deterministic if, given the same set of inputs, it produces the same set of outputs. Think of your game as a big, black box, with some inputs and outputs.</p>
<p>The two main input types for most games are:</p>
<ul>
<li><em>Player input devices.</em> The state of the gamepad, keyboard, mouse, or any other input device is used to update the simulation.</li>
<li><em>System clock.</em> Most games query the system clock once per frame to retrieve a current time and delta time. The game then advances the simulation forward by that amount of time. Even console games that expect to run at a rocksolid 60 Hz are often implemented this way to be able to deal with different TV refresh rates or with slower builds during development.</li>
</ul>
<p>The main outputs of a game system are the pixels on the screen and the generated sounds. Optionally there are other outputs such as network packets or force feedback.</p>
<p>To make the game deterministic we need to make sure that, for a given set of inputs, it always produces the same outputs. In other words, playing the game twice, entering the same button presses at the exact times, the game will end up in the same state both times (same location, same health, number of lives, NPC positions, etc).</p>
<p>What about random numbers, which we rely on so much in games? Aren’t random numbers inputs as well? Yes and no. In games we use pseudo random number generators. That means that the sequence of numbers from a particular seed is always the same. For a given seed, all runs of the game are fully deterministic, so the random numbers are not really an input into the system. The seed to the random number generator is an input, since it will greatly affect the state of the simulation.</p>
<h3>Record and Playback</h3>
<p>Recording all player inputs and the system clock is as straightforward as it sounds: At the beginning of the simulation loop, sample the clock and the player input devices. Then save those values to a file and continue the game simulation as usual. You want to make sure the file is flushed after writing the values for each frame. That way, if the game crashes, you’ll have all the input leading up to that frame and you’ll be able to play it back right up to the point where it crashed.</p>
<p>Recording the input has a negligible performance impact and the amount of data saved is very small. As an example, in our current game, every frame we save the frame number, the clock time, frame delta time, and a 20-byte structure for each gamepad (see Listing 1). Without any compression, that’s 52 bytes per frame for a two-player game. At 60 Hz, a 20-minute game would be a tiny 3.6 MB, which is small enough to attach to a bug tracking system, email to the team, or archive with the build itself.</p>
<p><strong>Listing 1. Game input structures.</strong></p>
<pre>struct FameInput{    uint32 frameNumber;    float time;    float dt;};

struct RawControllerState{    uint32 buttons;    float leftStickX;    float leftStickY;    float rightStickX;    float rightStickY;};</pre>
<p>Using the recorded input for playback is almost as easy. Every frame, before the simulation starts, we read the input data from the file and feed it to the game as if it came from the system clock and the input devices. A clean way to do that is to separate the reading of the data from where it comes from. In C++ we can use abstract base classes that define an interface describing how to retrieve the data. For example, a class IGameInput can define an interface, and the class GamepadGameInput reads the data from the hardware, while the class FileGameInput reads it from the recorded file (see Listing 2).</p>
<p><strong>Listing 2. IGameInput, GamepadGameInput and FileGameInput</strong></p>
<pre>class IGameInput{public:    virtual void GetInputData(RawControllerState&amp; state) = 0;};

class GamepadGameInput: public IGameInput{public:    virtual void GetInputData(RawControllerState&amp; state);};

class FileGameInput: public IGameInput{public:    virtual void GetInputData(RawControllerState&amp; state);};</pre>
<p>In addition to recording and playing back those inputs every frame, we also need to record the seed to the random number generator before the game starts, and read it and apply it during playback. That will ensure that all the random numbers are the same in both runs of the game.</p>
<h3>Verification</h3>
<p>If you’re going to rely on this recording system, you need to make sure the playback produces the exact same results as the original session. If they aren’t the same, the whole system is worthless. Also, because we’re just recording input, if the state of the game during playback ever starts to diverge, it will continue getting more and more out of sync from there. We need a way to make sure the playback produces the same state as the initial recording.</p>
<p>Earlier we identified some of the outputs of the game as the pixels on the screen or the sound produced by the speakers. We could compare pixels with a previous run of the game, but the storage requirements would be enormous, and the performance less than ideal. Besides, subtle changes in shaders, lighting, or a simple texture change would throw the comparison off. An easier solution is to check the game state itself. If an enemy is in a different location in two runs of the game, of course it’s going to render to a different set of pixels. It will be a lot faster and easier to compare game states than raw output.</p>
<p>During recording, in addition to the game input, we can save some of the state of the game. There’s no need to record it all, just some of the most important and representative state. Good candidates include player and enemy positions, prop transforms, score, etc. Unless it’s crucial to your game, there’s no need to record things like player animations or enemy AI state. Chances are that if any of those diverge, the positions for those entities will also diverge right away.</p>
<p>Once we have all of this state recorded, we can verify the state does not change during playback. Every frame, we compare the current game state with the recorded game state. If they’re different, even by the smallest amount, we know something has gone wrong and we flag it right away. The game isn’t quite deterministic and something needs to be fixed. We can choose to record and verify the game state anywhere in the simulation loop (as long as they’re both done in the same spot), but if we do it after the simulation step instead of before, we’ll be able to see what the inputs were that caused the divergence this frame, which will help when debugging. The main loop is shown in Listing 3.</p>
<p><strong>Listing 3. Main loop structure</strong></p>
<pre>while (!done){    SampleClockAndInputs();   // from different sources through the same interface    RecordInput();    UpdateSimulation();    if (recordGameState) RecordGameState();    if (verifyGameState) VerifyGameState();}</pre>
<p>Unlike recording game input, recording the game state can be a much more expensive operation. Traversing the game structures can be a significant performance hit due to cache misses, and the data saved to disk often results in large files. Because of this, in our current game player input is recorded all the time, but recording game state is optional, and we can be controlled through a command-line parameter. Game state verification is also optional, since sometimes we want to play back a recorded set of inputs even knowing that the state of the game is going to diverge due to changes we’ve made.</p>
<p>For a few of you, checking the game state won’t be enough. If you’re writing a middleware graphics layer, you probably want to verify that the values you’re generating in the back buffer are the same ones from a previous pass. Or maybe that improving the performance of a rendering algorithms still generates the same image. In that case, you might want to consider something like <a href="http://pdiff.sourceforge.net/">Perceptual Image Difference</a>, which will be a much less error-prone than comparing exact values for pixels.</p>
<h3>Monkeying Around</h3>
<p>Once the game is fully deterministic and the playbacks are rock solid, you want to make sure it stays that way. It’s all too easy to introduce bugs that will cause playbacks to diverge. For example, a single, innocent-looking, uninitialized variable can change the simulation depending on the value it happens to have for this run.</p>
<p>The best method I’ve found to stress test the playback system is to use a recorded session of monkey input. The monkey input method consists of feeding the game pseudo-random inputs (as if a monkey were playing the game). Recording both the input and game state of a monkey input play session, and then playing it back verifying the game state kills two birds with one stone: You get some nice automated testing of your game, and you verify that the game is fully deterministic. It sounds too simple to be useful, but you’ll be amazed at how many bugs your first session of monkey input will uncover.</p>
<p>A clean way to implement the monkey input, is writing a new class that implements the IGameInput interface. This new class will generate game input for all the buttons and axes of a game controller, but it won’t come from the hardware game controller or from a recorded session, but from randomly generated values. Apart from being a very clean way to insert input into the game, this approach has the advantage that the monkey input can be recorded just as if it were regular input coming from the controller. That means we can later replay a session and verify its game state, which makes for a very useful functional test.</p>
<p>It turns out that totally random input values are not ideal, as it becomes apparent as soon as you implement the naive random monkey input class. If the state of the jump button is truly random, it will be pressed and released almost every frame, which means the player will hardly ever jump high enough off the floor. A better implementation will use a range with a minimum and maximum press time durations, which will give much better results. You might also want to avoid pressing specific buttons (like the pause button, or the restart button combination). Resist the temptation to make the monkey input too smart and make it behave more like a real player, for example, by limiting the number of buttons it can have pressed simultaneously. Part of the benefit of the monkey input is that it will do very unexpected things, that no sane player will ever do on purpose, and by doing that, it will unearth many more problems and issues with the game.</p>
<p>One day, shortly after I implemented the monkey input system, the playback verification started failing. Some game entities were ending up in a different state than expected. After doing some digging, I realized that I was using the same random number generator for the game simulation and for the monkey input. Since the playback was just reading input values from the file and not generating them on the fly, the random number sequence was getting off sync right away, causing entities with some randomness in them to behave differently. Lesson learned: Make sure that nothing in the monkey input affects the game systems. In this case, I solved it by using two instances of the random number generator, one for the monkey and one for the game.</p>
<h3>Playback in The Real World</h3>
<p>At this point we can easily reproduce bugs. We can re-run the game and put a breakpoint right before the game crashes—except that the crash happens 20 minutes into the playback and nobody wants to wait that long.</p>
<p>Fortunately, we can make time go by faster. During playback we don&#8217;t care about tearing artifacts, so we can turn off the vertical sync, which will speed things up a bit. Most importantly, we often don’t even care whether we render anything or not, so if we turn off rendering completely, we can make the playback run significantly faster, particularly if you were graphics bound. One word of caution: If the rendering part of the game does significant work, and especially if you suspect it might be interacting with the rest of the game to cause a bug or some other source of non-determinism, you might want to do the same work in the rendering system, constructing the push buffer the way you would normally do, but never send it off to the graphics hardware.</p>
<p>Turning off the vertical sync and disabling the graphics rendering, is just reducing the amount of time we spend in each frame. But it still takes 10 minutes to get 10 minutes into the game, we just go through many more frames to get there. The last piece of the puzzle to make time go faster is to be able to set a fixed timestep. Now we can go through the simulation at a rate much faster than real time (and the beefier you computer, the faster it will go). In my current game, we can go through 10 minutes of game time in about 1 minute of real time.</p>
<p>One use for recording and playback that we haven’t mentioned is performance comparisons. When you’re optimizing the game, you can record a gameplay sequence and gather some performance statistics: average fps, longest frames, etc. Then you can apply your optimizations, playback the same input sequence, and compare the performance statistics to see how effective the optimizations really were. Just make sure you use real clock time and not the recorded clock time, otherwise you won’t see any differences, even with all the hard effort you put into the optimizations.</p>
<p>There are a few details we glossed over that might prevent some games from begin fully deterministic: network traffic, asynchronous file I/O, and threading issues. We’ll cover those in detail next month.</p>
<p>You’ll soon find that input recording and playback becomes an essential tool for in development process and you’ll wonder how you ever lived without it. Spend a few hours implementing it early in the development cycle and reap the benefits many times over during production. You’ll be the hero of the day when that dreaded crash bug from QA arrives minutes before a deadline.</p>
<p>Thanks to <a href="http://www.tilander.org/aurora/">Jim Tilander</a> for being a great idea bouncing-board and proofreading the article.</p>
<p>This article was originally printed in the May 2008 issue of <a href="http://www.gdmag.com/homepage.htm">Game Developer</a>.</p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1&amp;title=Back%20to%20The%20Future%20%28Part%201%29&amp;bodytext=How%20would%20you%20like%20to%20be%20able%20to%20reproduce%20every%20crash%20report%20that%20QA%20adds%20to%20the%20bug%20database%20quickly%20and%20reliably%3F%20How%20useful%20would%20it%20be%20to%20be%20able%20to%20put%20a%20breakpoint%20the%20frame%20before%20a%20crash%20bug%20happens%3F%20You%20can%20do%20all%20that%20and%20more%20if%20your%20game%20is%20deterministic%20and%20you%20feed%20it%20the%20same%20inputs%20as%20an%20earlier%20run.%20Sounds%20easy%3F%20It%20is%2C%20if%20you%20implement%20it%20early%20on%20and%20you%20keep%20it%20that%20way%20during%20development." title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1&amp;title=Back%20to%20The%20Future%20%28Part%201%29&amp;notes=How%20would%20you%20like%20to%20be%20able%20to%20reproduce%20every%20crash%20report%20that%20QA%20adds%20to%20the%20bug%20database%20quickly%20and%20reliably%3F%20How%20useful%20would%20it%20be%20to%20be%20able%20to%20put%20a%20breakpoint%20the%20frame%20before%20a%20crash%20bug%20happens%3F%20You%20can%20do%20all%20that%20and%20more%20if%20your%20game%20is%20deterministic%20and%20you%20feed%20it%20the%20same%20inputs%20as%20an%20earlier%20run.%20Sounds%20easy%3F%20It%20is%2C%20if%20you%20implement%20it%20early%20on%20and%20you%20keep%20it%20that%20way%20during%20development." title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1&amp;t=Back%20to%20The%20Future%20%28Part%201%29" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1&amp;title=Back%20to%20The%20Future%20%28Part%201%29" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1&amp;title=Back%20to%20The%20Future%20%28Part%201%29" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Back%20to%20The%20Future%20%28Part%201%29&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fback-to-the-future-part-1&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/back-to-the-future-part-1/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The Measure Of Code</title>
		<link>http://gamesfromwithin.com/the-measure-of-code</link>
		<comments>http://gamesfromwithin.com/the-measure-of-code#comments</comments>
		<pubDate>Fri, 13 Jun 2008 07:44:32 +0000</pubDate>
		<dc:creator>Noel</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Game tech]]></category>
		<category><![CDATA[power of two]]></category>

		<guid isPermaLink="false">http://www.gamesfromwithin.dreamhosters.com/?p=447</guid>
		<description><![CDATA[<img hspace="10" width="200" height="129" align="right" src="/userfiles/image/2008_06/tapemeasure_s.jpg" alt="tape measure" />I've gotten a lot of questions about how big our codebase is, how fast does it build, how many tests we have... Fear not, Gentle Reader, all your pressing questions will be answered here.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code&amp;source=snappytouch&amp;style=normal&amp;service=bit.ly&amp;service_api=R_185f898b3dec44c3a4de33e8fccadcf7" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;ve gotten a lot of questions about how big our codebase is, how fast does it build, how many tests we have&#8230; Fear not, Gentle Reader, all your burning questions will be answered here.<span id="more-101"></span></p>
<h2>Size</h2>
<p>Charles and I were priding ourselves in keeping things small and minimal. But truth be told, it&#8217;s not like we were keeping track of how many lines of code we had written. Were things as small as we hoped they were?</p>
<p>The most convenient way of counting lines of code that I know is <a href="http://cloc.sourceforge.net/">CLOC</a>. It&#8217;s an extremely easy to use open source program which counts the lines of code in a codebase, gives very detailed information, strips out whitespace, breaks things down by language, and does just about everything you&#8217;d want from a program like that.</p>
<p>Running it on the latest version of our code (not including any 3rd party libraries) produces this:</p>
<pre>    1621 text files.    1579 unique files.    3721 files ignored.
-------------------------------------------------------------------------------
Language                    files          blank        comment          code
-------------------------------------------------------------------------------
C++                            485          13577            303          46181
C#                             324           4935            712          22966
C/C++ Header                   407           4153             95          11975
MSBuild scripts                 18              0            126          1490
-------------------------------------------------------------------------------
SUM:                          1234          22665           1236          82612</pre>
<p>Almost 60K lines of C++ code seemed very high. At first I thought it was because CLOC was counting files twice: once in their regular location and once in the .svn directory, but apparently it&#8217;s already removing all duplicates, so that wasn&#8217;t it.</p>
<p>Almost more scary than the amount of C++ code (which is all our runtime and some of our tools) is the amount of C# code. For a language that claims to be of significantly higher level than C++, that&#8217;s quite a mouthful of code!</p>
<p>Another surprising count in there is the number of lines with comments. Since we make heavy use of <a href="http://www.gamesfromwithin.com/articles/cat_testdriven_development.html">TDD</a>, I really didn&#8217;t expect more than a couple dozen lines of code in the whole codebase. Still, I&#8217;m kind of proud that we have less than one line of code per file on average <img src='http://gamesfromwithin.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here&#8217;s a more detailed breakdown, with the line count just for our runtime (engine and game):</p>
<pre>1089 text files.    1053 unique files.    2338 files ignored.
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C++                            441          11997            245          40943
C/C++ Header                   385           3964             90          11405
-------------------------------------------------------------------------------
SUM:                           826          15961            335          52348</pre>
<p>and for our tools:</p>
<pre>532 text files.     531 unique files.    1383 files ignored.
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C#                             324           4935            712          22966
C++                             44           1580             58           5238
MSBuild scripts                 18              0            126           1490
C/C++ Header                    23            199              5            591
-------------------------------------------------------------------------------
SUM:                           409           6714            901          30285</pre>
<h2>Tests</h2>
<p>Then I realized that a good chunk of those were tests. So excluding all directories matching *Tests* gets the following result:</p>
<pre>1206 text files.    1187 unique files.    4199 files ignored.
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C++                            283           6464            150          22464
C#                             213           2636            534          12402
C/C++ Header                   380           3824             94          10782
MSBuild scripts                 12              0             84            978
-------------------------------------------------------------------------------
SUM:                           888          12924            862          46626</pre>
<p>A bit more than half the C++ code consisted of tests. That&#8217;s pretty consistent with my experience with TDD. C# seems to follow a similar percentage as well.</p>
<p>As for the exact number of tests, running a grep for TEST shows all the C++ tests:</p>
<pre>C:\pow2&gt;grep -r TEST SweetPea Engine Tools | grep -v svn | wc   2163    3620  221953</pre>
<p>And doing the same thing with [Test] brings up all C# tests:</p>
<pre>C:\pow2&gt;grep -r \[Test\] SweetPea Engine Tools | grep -v svn | wc   735    1470  52717</pre>
<p>That means that our average C++ test is about 11.5 lines long, and C# tests 14.4. Frankly, that sounds rather high. We make heavy use of fixtures whenever possible and each test usually only checks for a single condition (even if it involves a couple check statements). I suppose that number is higher than expected because it probably includes all the lines from #include statements and all the fixtures as part of the average.</p>
<table border="1" cellspacing="1" cellpadding="1" width="100%">
<tbody>
<tr>
<td><strong>Language</strong></td>
<td><strong>Lines</strong></td>
<td><strong>Non test lines</strong></td>
<td><strong>Test lines</strong></td>
<td><strong>% of non test code</strong></td>
<td><strong>Number of tests</strong></td>
<td><strong>Lines per test</strong></td>
</tr>
<tr>
<td>C++</td>
<td>58156</td>
<td>33246</td>
<td>24910</td>
<td>57% *</td>
<td>2163</td>
<td>11.5</td>
</tr>
<tr>
<td>C#</td>
<td>22966</td>
<td>12402</td>
<td>10564</td>
<td>54%</td>
<td>735</td>
<td>14.4</td>
</tr>
</tbody>
</table>
<p>* If we only count cpp files, that goes down to 49%</p>
<p>I was curious about that last part of checking a single thing per test, so I ran a grep for the number of CHECK statements in our code:</p>
<pre>C:\pow2&gt;grep -r CHECK SweetPea Engine Tools | grep -v svn | wc   3886   15079  399598</pre>
<h2><img src="/userfiles/image/2008_06/tapemeasure.jpg" alt="" hspace="10" vspace="10" width="400" height="258" align="right" /></h2>
<p>That&#8217;s 1.8 CHECK statements per TEST, which is about right. Even though we&#8217;re checking for a single condition, we&#8217;ll often check a couple things about it (i.e. the camera stopped and it reached its final destination).</p>
<h2>Build Times</h2>
<p>So, given that amount of code, how long does it take to build it? Clearly it depends on your hardware. Since we&#8217;re not exactly rolling in money, we don&#8217;t have particularly powerful machines. Here at home, I&#8217;m using a modest Core 2 Duo E4300 (overclocked to 2.6 GHz) with fast memory and a relatively fast SATA hard drive, so that&#8217;s what I used for all my timings.</p>
<p>A full build of our game, plus all the libraries, all the tests, and running all the tests takes exactly 1 minute and 10 seconds. That&#8217;s pretty good for two reasons:</p>
<ul>
<li>When we work with the game we don&#8217;t build and run the unit tests for the engine. We have a separate solution for that. A full build of just the engine, the game, and the game unit tests only takes 43 seconds.</li>
<li>The game itself is a fairly large project and devenv doesn&#8217;t know how to paralellize that build, so it&#8217;s only using half the available CPU power for about half the build time.</li>
</ul>
<p>An incremental build after changing a single cpp file takes <a href="http://powerof2games.com/node/32">slightly over a second</a> (including half a second of unit test execution).</p>
<p>As you can imagine, working with that codebase is a dream come true. Snappy, responsive. Nothing is hard enough that can&#8217;t be changed.</p>
<p>Unfortunately that&#8217;s where the fairy tale ends. The tools are another story altogether. Our C# tools, with all their unit tests, build in a mere 18 seconds, and the C++ tools in 1 minute and 10 seconds. That&#8217;s not too bad, except that it&#8217;s a surprisingly large amount of time for the C++ tools since there aren&#8217;t that many of them.</p>
<p>Here&#8217;s the kicker, doing another build without changing a single thing take 38 seconds. Whoa! We&#8217;re doing some C++/CLI trickery and apparently dependency checking is totally broken in VS2005 (either that, or we just don&#8217;t know how to set it up right).</p>
<h2>Keeping things fast</h2>
<p>What&#8217;s the secret of a lighting-fast build? Clearly, keeping the code size down is crucial. If your codebase is 2 million lines of code, builds are going to be painful no matter what. But they can be a little less painful with some gentle care.</p>
<p>One of the main build-time killers that we&#8217;re avoiding is the use of STL or <a href="http://www.boost.org/">Boost</a>. Those libraries pull in everything and the kitchen sink, and their heavy use of templates make build and link time go through the roof. No thanks.</p>
<p>Our template use is pretty minimal. We have a couple containers (which I love and I&#8217;ll write about it one of these days) and that&#8217;s about it.</p>
<p>We&#8217;re pretty anal when it comes to keeping <a href="http://www.gamesfromwithin.com/?p=8 ">physical dependencies</a> to a minimum. We forward declare aggressively, and we only include the headers that are necessary for each cpp file (<a href="http://www.gimpel.com/">PC Lint</a> is &#8220;kind&#8221; of enough to remind us every time we have unnecessary #includes). We&#8217;re not using external include guards or #pragma once.</p>
<p>Precompiled headers are either not used, or kept to a minimum. I think the only project that uses them is the game and only for Havok headers. We don&#8217;t even have windows.h in a precompiled header (which would be a really bad idea because you&#8217;d be putting all the junk in windows.h available to your whole program).</p>
<p>Finally, we are using incremental links whenever possible. I remember a few versions of Visual Studio ago they were pretty broken, but they&#8217;re not giving us any problems. The only caveat is that if you modify a static library your program is linking with, it will force a full link. So they&#8217;re really only good for modifying the executable itself.</p>
<p>We&#8217;re not using any distributed builds. First of all, we don&#8217;t have enough computers to make it worthwhile. And second, I had horrible experiences with distributed builds in the past. They would help with a badly structured codebase, at the cost of longer incremental builds and mysterious spurious bad builds. Besides, once they&#8217;re in place, they tend to encourage even further disregard for keeping dependencies to a minimum.</p>
<h2>How about you?</h2>
<p>So, that&#8217;s it for the Power of Two codebase. How about you? Want to share your size, build times, or any other data?</p>




	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code&amp;title=The%20Measure%20Of%20Code&amp;bodytext=I%27ve%20gotten%20a%20lot%20of%20questions%20about%20how%20big%20our%20codebase%20is%2C%20how%20fast%20does%20it%20build%2C%20how%20many%20tests%20we%20have...%20Fear%20not%2C%20Gentle%20Reader%2C%20all%20your%20pressing%20questions%20will%20be%20answered%20here." title="Digg"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code&amp;title=The%20Measure%20Of%20Code&amp;notes=I%27ve%20gotten%20a%20lot%20of%20questions%20about%20how%20big%20our%20codebase%20is%2C%20how%20fast%20does%20it%20build%2C%20how%20many%20tests%20we%20have...%20Fear%20not%2C%20Gentle%20Reader%2C%20all%20your%20pressing%20questions%20will%20be%20answered%20here." title="del.icio.us"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code&amp;t=The%20Measure%20Of%20Code" title="Facebook"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code&amp;title=The%20Measure%20Of%20Code" title="Reddit"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code&amp;title=The%20Measure%20Of%20Code" title="StumbleUpon"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=The%20Measure%20Of%20Code&amp;body=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code" title="email"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fgamesfromwithin.com%2Fthe-measure-of-code&amp;partner=sociable" title="PDF"><img src="http://gamesfromwithin.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://gamesfromwithin.com/the-measure-of-code/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
