{"id":15904,"date":"2012-05-15T05:37:16","date_gmt":"2012-05-15T10:37:16","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15904"},"modified":"2012-05-15T06:15:21","modified_gmt":"2012-05-15T11:15:21","slug":"project-octant-part-8-the-time-hole","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15904","title":{"rendered":"Project Octant Part 8: The Time-Hole"},"content":{"rendered":"<p>So let&#8217;s talk about data structures.  I&#8217;ve mentioned way back at the start of the project that there are certain costs to using an octree. An octree will make interfacing with ALL blocks a hundred times faster but make dealing with  a specific block several times slower. I&#8217;m curious how&#8230;<\/p>\n<p>Hang on. My program has been behaving oddly recently.  It&#8217;s like it will suddenly stop building new blocks and I&#8217;ll end up stuck on this island floating in empty space.  I&#8217;ve got the program set to aim for 60fps, and if one thing starts eating too much CPU then the block-building gets choked off.  Let&#8217;s see where these CPU cycles are going.<\/p>\n<p>I add a little benchmarking loop.  Right now there are just five systems running:<\/p>\n<p><!--more--><\/p>\n<ol>\n<li>Avatar: This moves the camera around and does a little super-cheap collision detection.\n<\/li>\n<li>World: During block generation, world allocates these tables of handy values to speed up building.  This part just looks for out-of-use tables and unloads them.\n<\/li>\n<li>Scene: This is the heavy hitter. It does those crazy <a href=\"?p=15843\">heavy-duty noise calculations<\/a>, places the blocks, maintains the octree, and turns the blocks into textured polygons.\n<\/li>\n<li>Window: This bit really does almost nothing. It checks for keypresses.\n<\/li>\n<li>Qt: Ah. This is where Qt gets a chance to process stuff.  Qt is the platform I&#8217;m using to write this.  Go back to <a href=\"?p=15758\">part 3<\/a> if you need a refresher.\n<\/li>\n<\/ol>\n<p>Let&#8217;s see where the time is going:<\/p>\n<p><!--more--><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant8_1.jpg' class='insetimage'   alt='octant8_1.jpg' title='octant8_1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I don&#8217;t have access to a microsecond timer here, so we have to make due with milliseconds. This is like wanting to measure how long a man&#8217;s stride is when he walks, but your ruler is only marked off in terms of kilometers. You have to measure a lot of individual steps to figure it out.  These measurements were taken over the course of a second. There&#8217;s a thousand milliseconds in a second, so we&#8217;re looking to account for those thousand milliseconds.  Note that there&#8217;s a tiny bit of overhead to taking these measurements and we&#8217;re likely to miss a tick-tock here or there on really short tasks, but this is good enough to give us a broad understanding of where the time is going.<\/p>\n<p>Note that the item &#8220;Qt &#038; Rendering&#8221; is there because I do my rendering <em>during<\/em> the bit where Qt gets a timeslice. The two items in parenthesis break that number down.  (Rendering) is how much time I spend shoving polygons at the graphics card, and (Qt) is how much time Qt eats doing&#8230; whatever it is that Qt does.<\/p>\n<p>Hm.<\/p>\n<p>This is not good. In fact, this is exactly what I was afraid of <a href=\"?p=15758\">back when I started using Qt<\/a>.  Over half of my CPU cycles are being eaten by Qt, doing&#8230; what? I have no idea.  Qt is in charge of I\/O, so it&#8217;s doing some keyboard and mouse processing.  But that stuff is so fast that I shouldn&#8217;t be able to measure it with this clock.  It&#8217;s also in charge of drawing that black box with the checkboxes and text in it. <\/p>\n<p>Could that be it? Is Qt burning six tenths of a second on a rectangle of text with a couple of checkboxes?  Hm.  If I get rid of the box then I won&#8217;t be able to see these results.  Let&#8217;s get rid of the checkboxes and see if that changes anything.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant8_2.jpg' class='insetimage'   alt='octant8_2.jpg' title='octant8_2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That made a big difference. Qt is now &#8220;only&#8221; using half of the CPU to draw this stupid black rectangle and text.  For contrast, during the <em>other<\/em> half of the second I&#8217;m rendering millions and millions of polygons.  Picture two guys in a library. In the same hour, one of them reads the complete three-volume set of <a href=\"http:\/\/en.wikipedia.org\/wiki\/The_Baroque_Cycle\">The Baroque Cycle<\/a> and the other guy reads a single <a href=\"http:\/\/en.wikipedia.org\/wiki\/Family_circus\">Family Circus<\/a> strip. <\/p>\n<p>Let&#8217;s do another test. Let&#8217;s give Qt a bunch more controls and see how it reacts.  <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant8_3.jpg' class='insetimage'   alt='octant8_3.jpg' title='octant8_3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;ve added a little text label, another text box, and a couple of progress bars. (I dunno. They look kind of like health bars or something.  Seems like a reasonable thing to expect if this was a game environment.)   And now Qt is eating 71% of our CPU. This would be funny if it wasn&#8217;t so sad. This makes it pretty clear why nobody&#8217;s ever tried to use this thing for a game. A real game interface would be even more complex than this.<\/p>\n<p>Keep in mind that these controls aren&#8217;t changing. I&#8217;m not altering the position of the faux-health bars or anything else that would create a need for them to be re-drawn every frame. The text boxes only update once a second. <\/p>\n<p>With performance like this, I might as well go use Visual Basic. (No, not really.)  Note that this doesn&#8217;t mean that Qt is <strong>bad<\/strong>.  It&#8217;s just built with different goals.  Getting a cross-platform windowing system to play nice with 3D rendering like this requires a <em>lot<\/em> of levels of abstraction.  A normal Qt application is just some kind of interface with buttons and sliders and whatnot, without any 3D rendering going on.  In those circumstances, the user isn&#8217;t going to notice a few missing milliseconds.  My program is sensitive to single-digit millisecond usage, but a human being generally isn&#8217;t going to notice until it&#8217;s in the hundreds, and they probably won&#8217;t care until it&#8217;s near a thousand. The performance needs of Qt are at least two orders of magnitude from the performance needs of a 3D game. <\/p>\n<p>I find <a href=\"http:\/\/labs.qt.nokia.com\/2010\/01\/11\/qt-graphics-and-performance-the-cost-of-convenience\/\">this page<\/a>, which seems to be from one of the developers behind Qt. It confirms my worst fears: This CPU cost is an inescapable reality of using Qt.  Even if all of those optimization techniques worked for me, and even if they applied to every little interface item, and even if I made maximum gains from all of them, it <em>still<\/em> wouldn&#8217;t do more than cut the CPU usage in half, which would still be ten times more than it should be. <\/p>\n<p>If I disable the Qt drawing entirely (and have it print out the timing info to the console window instead) then we get:<\/p>\n<p><code>FPS: 179<\/p>\n<p>Avatar: 0<br \/>\nWorld: 1<br \/>\nScene: 25<br \/>\nWindow: 0<br \/>\nQt&Render: 962<br \/>\n(Qt): 251<br \/>\n(Render): 711<br \/>\nQt ms per Frame: 1<\/code><\/p>\n<p>So the real overhead of using Qt is only ~1ms per frame.  That&#8217;s reasonable. It&#8217;s just that the Qt drawing tools are too slow to be useful. A shame, really.  &#8220;A platform-independent interface&#8221; was the main selling point of Qt for me. I&#8217;ve found a lot of other things to like about it since then, but losing the GUI is pretty much a deal-breaker. <\/p>\n<p>When my processing began choking off I wanted to come in here and look for ways to optimize the octree or something. But it looks like the first order of business is &#8220;stop using qt&#8221; if I care about speed. I had a bunch of ideas for how I might tackle <a href=\"http:\/\/www.sea-of-memes.com\/LetsCode32\/LetsCode32.html\">the crazy challenges that Goodfellow faced in part 32 of his series<\/a>.  Seems sort of pointless now.  There&#8217;s no reason to agonize over the aerodynamics of your car while you&#8217;ve got a half-ton of cinderblocks in the back seat. <\/p>\n<p>Oh sure, I <em>could<\/em> work on my optimizations like this, but the CPU drain of Qt is noisy, so measuring performance would be like trying to play Jenga on horseback.  Also, experience has taught me that trying to monitor performance by reading a continuous spew of text in a console window is really aggravating. <\/p>\n<p>I suppose I could take my code and go back to Visual Studio and <a href=\"http:\/\/www.libsdl.org\/\">SDL<\/a>, which is what Project Frontier used.  But that means shopping for a suite of image loading, interface-drawing, font-reading tools. Yuck. I don&#8217;t want to unravel some idiotic chain of dependencies.  I don&#8217;t want to download a dozen different <acronym title=\"Software Development Kit\">SDK<\/acronym>&#8216;s, spew their files all over my hard drive and then try to get them to compile and link with my project. I don&#8217;t want to have to choose between the package that only solves half the problem, the package that sucks to use, or the package that ties me to Windows. I don&#8217;t want to have to learn a new programming language. <\/p>\n<p>I just &#8211; this external packages stuff is such a dang killjoy for me.  I really, really, really hate it. It takes all the fun out of programming. When I was younger I tolerated it, but now I seem to have lost my patience.  I know why this problem exists and I understand why there aren&#8217;t easy solutions. I just don&#8217;t have the desire to put up with it these days.<\/p>\n<p>Maybe I&#8217;ll work a bit more on this project before I shelve it.  Maybe I&#8217;ll migrate back over to Visual Studio and just muddle along with no interface.  Maybe I&#8217;ll just stop here.  I don&#8217;t know. I&#8217;m going to walk away from it for a bit and see how I feel about it then.<\/p>\n<p>Onward?<\/p>\n<p>EDIT: I&#8217;ve been meaning to ask: For those of you who played around with Project Frontier, what was the biggest hassle in porting it?  I know the capitalized filenames were a problem. &#8220;Main.cpp&#8221; instead of &#8220;main.cpp&#8221;.  I was obliged to use the former professionally for years, and eventually it became a habit that I didn&#8217;t question.  I&#8217;ve since been making sure everything is lowercase, but what other headaches did you encounter? (Aside from, you know, bugs and stuff.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So let&#8217;s talk about data structures. I&#8217;ve mentioned way back at the start of the project that there are certain costs to using an octree. An octree will make interfacing with ALL blocks a hundred times faster but make dealing with a specific block several times slower. I&#8217;m curious how&#8230; Hang on. My program has [&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-15904","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\/15904","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=15904"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15904\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}