{"id":15920,"date":"2012-05-17T05:33:12","date_gmt":"2012-05-17T10:33:12","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15920"},"modified":"2012-05-17T07:45:19","modified_gmt":"2012-05-17T12:45:19","slug":"project-octant-9-data-structures","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15920","title":{"rendered":"Project Octant 9: Data Structures"},"content":{"rendered":"<p>I&#8217;m still working on the project.  The stuff I&#8217;m working on now is a little boring and would mostly be a re-hash of stuff I&#8217;ve discussed before.  So rather than do that, let&#8217;s talk about data structures.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant9_2.png' class='insetimage'   alt='octant9_2.png' title='octant9_2.png'\/><\/td><\/tr><\/table><\/p>\n<p>Remember the little red cube.  We&#8217;ll see him again later.<\/p>\n<p>When a programmer sits down to write some software, the question comes, &#8220;How will I represent this problem in code?&#8221; This is actually one of my favorite parts of the job. (Or hobby, in this case. Unless you&#8217;re hiring? Do you want to fund development of this project? It has no gameplay, no plan, no support, and it doesn&#8217;t even work yet. Let me know if you&#8217;re interested!) If I need to store a historically important date like August 24, 1971, then there&#8217;s a lot of ways I could do it.  I could store it as a text string: &#8220;August 24, 1971&#8221;.  But then it would be hard to do math on it. (Say, to calculate how long it&#8217;s been between then and now.) I could store it as a group of three integers: 8, 24, and 1971. I could store it as the number of seconds elapsed since January 1, 1970: 51,840,000.  The latter is great for doing math (and is actually how most systems store time internally) but it means you have to do a messy conversion when you want to display the date to the end user.  Because if you display a date as 51,840,000 then the end user will find you and burn your house down.  <\/p>\n<p>What method will be fast? What will take up the least memory? What will make for clear, readable code? These are questions that programmers love to ponder before coming up with the wrong answer and making a mess of things. <\/p>\n<p>Which brings us back to the problem of storing large tables of data.  <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant9_5.png' class='insetimage'   alt='octant9_5.png' title='octant9_5.png'\/><\/td><\/tr><\/table><\/p>\n<p>We look at this and see a table. But internally, computers don&#8217;t &#8220;do&#8221; tables. They&#8217;re not really into 2-dimensional kind of stuff. Computer memory is a long, long line of values. If you&#8217;ve got four gigabytes of RAM, then you&#8217;ve got four billion little memory addresses in a single row, and it&#8217;s up to the programmer to make sense out of them.<\/p>\n<p><!--more--><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant9_3.png' class='insetimage'   alt='octant9_3.png' title='octant9_3.png'\/><\/td><\/tr><\/table><\/p>\n<p>This is how the table looks to the computer. Red red yellow blue red red blue yellow green blue green green yellow yellow green green. If I want something in the third column, second row, then I have to do a little math to figure out I&#8217;m really looking for box #7.  <\/p>\n<p>But then some cheeky programmer looks at the data and says, &#8220;I can&#8217;t afford the luxury of squandering sixteen whole boxes like this. What am I, Donald Trump? This isn&#8217;t a supercomputer with endless memory! You know what? I&#8217;ll bet there&#8217;s a better way to store this.&#8221; And then the programmer invents the quad tree.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant9_4.png' class='insetimage'   alt='octant9_4.png' title='octant9_4.png'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;ve already explained how these work way back in <a href=\"?p=15751\">part 2<\/a>, so let&#8217;s not go over that again. The point is that I can no longer look things up the way I did before.  If I want the third column, second row, then I have to look inside a box, inside a box, inside a box. There is no shortcut to getting there. It&#8217;s a tradeoff.  We&#8217;re trading speed, code clarity, and convenience in exchange for not using up so much dang memory.  That&#8217;s a lot to give up, and we wouldn&#8217;t even contemplate  this if not for the fact that 3-dimensional data (like our cube world) gets really, really big, really fast. Width times height times depth is a simple calculation with terrifying implications. <\/p>\n<p>But you don&#8217;t want to have to store the <em>entire<\/em> world in memory at once, not even in a tree.  It would be impractical.  In the case of an open-world game, the data wouldn&#8217;t even fit in memory, not even when using a quad\/octal tree.  Also, if the world was 2 kilometers wide (not very big) then every single lookup would take 11 hops. You&#8217;d need to look at the box-within-a-box-within-a-box, 11 levels deep.  <\/p>\n<p>So what we want is a hybrid system.  We want the convenient lookups of using a grid mixed with the memory savings of using a tree.  We want a grid&#8230; <em>of trees<\/em>.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant9_5.png' class='insetimage'   alt='octant9_5.png' title='octant9_5.png'\/><\/td><\/tr><\/table><\/p>\n<p>Ideally, your trees should have a maximum size of n, where n is the largest power of 2 that&#8217;s likely to be homogeneous. Look at your giant data set.  What&#8217;s the largest area of same-squares? If you never see an area larger than 16&#215;16 same-color squares, then there&#8217;s no reason to make your trees larger than that. <\/p>\n<p>Which brings me to the structure of project Octant:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant9_1.png' class='insetimage'   alt='octant9_1.png' title='octant9_1.png'\/><\/td><\/tr><\/table><\/p>\n<p>So when we want a particular cube, we do a little math to figure out what column it would be in.  We look up that column (if it&#8217;s available) and ask for the related node.  From the node we grab the octree, and from there we drill down to the cell in question.  So our worst-case scenario is:<\/p>\n<p>scene &raquo; column &raquo; node &raquo; octree16 &raquo; octree8 &raquo; octree4 &raquo; octree2 &raquo; octree1 &raquo; cell.<\/p>\n<p>That&#8217;s a lot of hops.  Things get really fun when one cell needs to look up the cell <em>right next to it<\/em>, and it takes 9 hops to reach its next-door neighbor.  <\/p>\n<p>I was rather worried about this. I mean, each empty cell needs to look up all six of each neighbors to see what faces it needs to draw.  (Since a cube has six faces.)  Six queries time nine hops sounded like a LOT of wasted time.  I added a bit of code to allow &#8220;backtracking&#8221;.  I made octrees aware of their parents so that the 2x2x2 octree would be able to reach up and ask the 4x4x4 octree for a particular cube.  If it didn&#8217;t have it, it would continue to pass the request up the chain.  I figured that since the vast majority of lookups were for cubes that were &#8220;next door&#8221;, I&#8217;d see some big savings.  Hopping up one level and down one level ought to be faster than going down through all nine levels. <\/p>\n<p>Turns out I was wrong.  The time needed to construct a single node went from 180ms to 170ms. That is a very small gain.  I expected some massive jump in performance, and instead I got what? A 6% boost?<\/p>\n<p>Still, this is exactly the sort of thing I wanted to play around with when I started the project. It&#8217;s sort of interesting to experiment with things and see how they behave. <\/p>\n<p>I&#8217;m not totally sold on the structure I outlined above.  It&#8217;s not terribly complex (by the standards of game engines) and I&#8217;m still fiddling with it, looking for where the performance bottlenecks might be. I might discover that this design is flawed in some way.  Or maybe I&#8217;ll come to the same conclusion <a href=\"http:\/\/www.sea-of-memes.com\/\">Goodfellow<\/a> did, and end up storing everything in a pure grid. We&#8217;ll see what we find.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m still working on the project. The stuff I&#8217;m working on now is a little boring and would mostly be a re-hash of stuff I&#8217;ve discussed before. So rather than do that, let&#8217;s talk about data structures. Remember the little red cube. We&#8217;ll see him again later. When a programmer sits down to write some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[233],"class_list":["post-15920","post","type-post","status-publish","format-standard","hentry","category-programming","tag-octant"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15920"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15920\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}