{"id":20689,"date":"2013-08-23T04:55:47","date_gmt":"2013-08-23T09:55:47","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20689"},"modified":"2015-07-01T03:43:47","modified_gmt":"2015-07-01T08:43:47","slug":"project-good-robot-part-4-the-clock","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20689","title":{"rendered":"Project Good Robot 4: The Clock"},"content":{"rendered":"<p>I worked at Activeworlds for a lot of years. Activeworlds is a social \/ gaming world along the lines of <a href=\"http:\/\/secondlife.com\/?gclid=CNvB3a-CjLkCFalcMgod4FAAtw\">Second Life<\/a> or <a href=\"http:\/\/www.roblox.com\/Landing\/Animated\">Roblox<\/a>.  It&#8217;s a virtual world with user-made content. The experience gave me some interesting phobias regarding CPU cycles.<\/p>\n<p>Perhaps some anecdotes would help.  For the sake of argument, let&#8217;s say these are all taking place around 2003 or so.<\/p>\n<p>Joe User is building himself a virtual office in Activeworlds. He wants dark windows with a heavy tint, but the object library only has these windows with 50% transparency. Joe doesn&#8217;t understand transparency. He&#8217;s not a graphics artist or a programmer. He&#8217;s just a regular person, and to him tinted windows are tinted because they&#8217;re &#8220;dark&#8221;. He tries changing the color of the window from the default blue to black, but confusingly it doesn&#8217;t help. He can still see through the window just fine. <\/p>\n<p>He makes a copy of the window to try another color when he has a eureka moment. Looking through both windows &#8211; one in front of the other &#8211; really makes a huge difference. What&#8217;s really happening is that the first window is blocking 50% of the outside color, and the second window is blocking 50% of the remainder for a final opacity of 75%. Joe doesn&#8217;t know this. All he knows is that this <em>looks nicer<\/em>. He makes another copy, and it&#8217;s better still! This is clearly the key to success. Two more windows perfect the look, giving him an overall opacity of 97% or so. He&#8217;s got five windows stacked up here.  He nudges them so they&#8217;re only a centimeter apart. <\/p>\n<p>If you&#8217;re a professional, your eye is probably twitching by this point. This isn&#8217;t art, it&#8217;s sabotage. Alpha surfaces (like our partly transparent windows) must be sorted before rendering. It&#8217;s a constant struggle to limit the number of transparent surfaces you&#8217;ve got in the scene and you want to be very careful about situations where the user will end up looking through multiple alpha polygons at the same time. The program has to get the distance to each surface, then shuffle them around and put them in order from furthest to closest before they can be drawn. All of this work must be done by your CPU. (Maybe new graphics cards have some trick for this, but in 2003 this load went right to the CPU.)<\/p>\n<p>But that&#8217;s not the bad part. <\/p>\n<p><!--more-->The <strong>bad<\/strong> part is when Joe grabs those five stacked windows and begins duplicating the group over and over to construct <strong>an office building<\/strong> out of them.<\/p>\n<p>At a lot of development studios, if a member of the art team did this you could go to their cubicle and set them on fire without breaking company policy.<\/p>\n<p>Meanwhile&#8230; <\/p>\n<p><!--more-->In another zone, Jane User is building herself a grand welcome area to her sea park. She couldn&#8217;t find any sea-themed stuff in the default library so she went online and searched for &#8220;3d models&#8221;. She found this awesome model of a dolphin balancing a ball on its nose. It was built in Maya or 3DS Max. She finds a converter and proceeds to take this <strong>thirty-thousand polygon<\/strong> dolphin and put it in her world as a statue, right near the welcome area.  In fact, she builds two of them, facing each other, to form a little archway that new visitors will walk through when they arrive. She overlaps the models so that the beach balls are both in the same spot, making it look like the two dolphins are holding up the same ball.  She stands back at a distance and admires her handiwork. <em>Not bad, not bad at all.<\/em><\/p>\n<p>She notices that this Activeworlds program is kinda slow all of a sudden. Oh well. Maybe they&#8217;ll get around to fixing this software one of these days. <\/p>\n<p>That&#8217;s not the fun part.<\/p>\n<p>The fun part is twenty minutes later when Bob Visitor arrives on his wobbly old 2001 laptop. He follows the path to the archway and walks through. As he passes under the beach ball(s), he&#8217;s actually occupying the bounding boxes of both dolphin statues at the same time. His computer is now performing collision checking on sixty thousand polygons and has basically stopped doing anything else. After a few seconds he figures Activeworlds has crashed. He closes the program. <\/p>\n<p><em>Stupid buggy Activeworlds.<\/em><\/p>\n<p>Then Jeff User comes along. He&#8217;s a kid. One of his friends tells him about this command you can put on objects that will cause them to display pictures from anywhere on the web. He&#8217;s figuring out this scripting language and he realizes you can set it up to show a whole bunch of images! He writes this long command to download all these gigantic (1280&#215;1024, big for the time period) desktop wallpapers and animate them on a wall. It&#8217;s slow as hell for some reason, but it looks sweet!  <\/p>\n<p>Sadly for the people walking by outside, it slows them down too, even though they can&#8217;t see inside Jeff&#8217;s house.<\/p>\n<p>Now, there were guidelines and rules and tutorials in place to encourage people not to do these destructive things, but you can probably guess how likely it is that young people will want to read technical documents before playing &#8220;a videogame&#8221;. This was exacerbated by the problem that people usually continued building and didn&#8217;t think about framerate until their computer began to slow down. And some people have a lot of tolerance when it comes to low framerates. And the icing on the cake is that everyone has vastly different computers. So one person with extreme tolerance for low framerate will use his brand-new lightning-fast computer and build until his framerate is in the single digits. Then other people show up with normal computers and it&#8217;s all madness and tears and crashing.<\/p>\n<p>As one of the programmers trying to keep this system going, this setup made me paranoid. In a videogame, if the art team uses a texture eight times larger than allowed, or if they blow through their polygon budget by a factor of ten, it&#8217;s no big deal to the coders. Maybe the program crashes, maybe it runs slow. Either way, it&#8217;s not your problem. If you&#8217;re feeling super-nice you&#8217;ll put in a warning message to let the artist know they screwed up. You can&#8217;t do this when you&#8217;re dealing with user content. Especially when you&#8217;re a small company and you can&#8217;t afford to drive people away for being bad at 3D graphics. <\/p>\n<p>I had to operate under the assumption that any system could begin devouring system resources at any time. Maybe too much collision detection. Or the animation system might begin shuffling massive texture billboards around in memory. Or the alpha sorter might go nuts. Or the raw polygon-pushing stuff. Or the character animation system. Anything. Anything can run out of CPU or memory at any time.<\/p>\n<p>The result is that you have to build your application like a tank. It needs to be able to absorb and mitigate any level of art asset insanity. <\/p>\n<p>The ramifications of this are kind of crazy. A regular programmer will nod sagely and suggest you deal with CPU usage spikes by putting the problem code in its own thread. But that doesn&#8217;t help when any code can become &#8220;problem code&#8221;, and running every single system in its own thread can introduce a whole new batch of problems.  <\/p>\n<p>It wasn&#8217;t until recently that I realized this paranoia was unusual. Even now, it&#8217;s really hard to make a system without asking myself, &#8220;But what if this takes a really long time for some insane reason?&#8221; <\/p>\n<p>Let&#8217;s say you&#8217;ve got a physics simulation running at a nice smooth 60 frames a second. Say we toss a ball and watch it drop:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr4_fps1.jpg' class='insetimage'   alt='gr4_fps1.jpg' title='gr4_fps1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>In an ideal world, if the computer is slow then we&#8217;ll get less frames, but we&#8217;ll just be seeing fewer timeslices of the exact same scene.  If we go down to 10 frames a second, then we&#8217;ll end up missing 5 out of every 6 frames, but the ball will still follow the same trajectory:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr4_fps2.jpg' class='insetimage'   alt='gr4_fps2.jpg' title='gr4_fps2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>But in reality, we end up with a badly mangled simulation that no longer works properly. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr4_fps3.jpg' class='insetimage'   alt='gr4_fps3.jpg' title='gr4_fps3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>We get a different parabola because this isn&#8217;t linear movement. If the program says, &#8220;I&#8217;ll do six frames worth of accelerating right now, and then I&#8217;ll apply six frames worth of movement to my position.&#8221; then we&#8217;ll end up with the red curve above. Calculating 1% interest a day isn&#8217;t the same as 30% interest every 30 days. <\/p>\n<p>And of course, what if there&#8217;s a wall in the way? The green line will bounce off it like a good physics simulation should, while the red line will skip over it. One frame it&#8217;s on the left side of the wall, and the next frame it&#8217;s on the other side of the wall, and we never got a frame where the ball was intersecting the wall and was available to bounce off of it.<\/p>\n<p>Now the obvious solution is to just go back and do the per-frame calculations.  If it comes time to update the ball and we&#8217;re five frames overdue, don&#8217;t just do one giant move times six. Do all six frames, one at a time, before going any further.<\/p>\n<p>That&#8217;s a pretty good solution. <strong>Unless the physics simulation is what is causing the slowdowns in the first place.<\/strong><\/p>\n<p>That seems like a funny idea when we&#8217;re talking about a single ball arcing through the air, but if we get a few thousand of them (say, debris from that giant robot you just blew up) and they all need to collide with the scenery (say, the building right behind the robot) then your &#8216;lil CPU might have some serious math homework to do. If you&#8217;re five frames late because you were running physics simulations last frame, then attempting to run an extra six frames of simulation THIS frame is not a solution, it&#8217;s the start of a death-spiral. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr4_hl2.jpg' class='insetimage'   alt='gr4_hl2.jpg' title='gr4_hl2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;m reasonably sure I&#8217;ve seen this death-spiral in commercial games. In <em>Half-Life 2: Episode 2<\/em> there&#8217;s a scene at the start of the game where a bridge collapses. My computer was on the low end of the spectrum when the game came out, and this scene spiked exactly like our hypothetical runaway physics simulation.  The frame rate cascaded downward, with each frame taking longer than the one before it util the game came grinding to a halt and the sound began stuttering. This ended in a long (ten second?) pause before the game snapped out of it again and returned to normal. I&#8217;m guessing the physics was trying to catch up, and each frame it was frantically trying to make up for the deficit, which only pushed it further in the hole. The only reason it didn&#8217;t crash the game was that the bridge collapse was scripted to take a fixed amount of time. If this was an ongoing thing there would have been no coming back.<\/p>\n<p>You can put in some kind of safety-valve trigger that will skip the physics if they start killing you, although detecting and isolating performance problems on the fly from within the thing you&#8217;re trying to measure can be problematic. And even if you guess right, skipping the physics means that you&#8217;ve got different parts of the game running at different framerates and at different levels of fidelity, which can lead to strange problems in other areas.<\/p>\n<p>But what if we just decided we didn&#8217;t care? What if instead of trying to detect, correct, and mitigate performance problems, we just built a program with no such safeguards? I&#8217;m going to find out. To wit:<\/p>\n<p><strong>This game is going to run at sixty frames a second.  Period.<\/strong><\/p>\n<p>Instead of treating 20fps as &#8220;not the best&#8221;, I&#8217;ll treat it like a failure state. If the game doesn&#8217;t have the power it needs, it won&#8217;t skip non-critical things to speed up. It won&#8217;t run parts of the system at a lower framerate. It won&#8217;t try to catch up. If the computer slows down, the game slows down. (And to be clear, the game won&#8217;t go faster if you have a fast computer. It&#8217;s capped at 60fps.) If for some reason the game is played on a machine that can only keep up 30 frames a second, it will feel like some sort of half-speed bullet time. <\/p>\n<p>This saves&#8230; well, it saves a ton of work. An unbelievable mountain of uncertain guesswork and difficult-to-test safeguards can just be eliminated. Since we&#8217;re not doing heavy-duty 3D, we can just assume the speed will be there.<\/p>\n<p>I don&#8217;t know.  I just clocked it, and at this point in the project I&#8217;m idle for about 730 milliseconds out of every second. So, I&#8217;m idle about three-quarters of the time. This is using a debug build with no optimizations.<\/p>\n<p>I don&#8217;t know if this is a good idea, but I&#8217;m going to try it. I can always add more sophisticated time management later if I need to. To a certain extent, this is like a man announcing that when he takes the dog for a walk, he&#8217;s no longer going to wear the crash helmet, safety harness, steel-toed boots, and bulletproof vest.  If nothing else, this will be a new experience for me. <\/p>\n<p>And yes I did just write a 2,000 word entry on a feature I&#8217;m NOT putting in. <\/p>\n<p>You can&#8217;t stop me! I&#8217;ve got the source code!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I worked at Activeworlds for a lot of years. Activeworlds is a social \/ gaming world along the lines of Second Life or Roblox. It&#8217;s a virtual world with user-made content. The experience gave me some interesting phobias regarding CPU cycles. Perhaps some anecdotes would help. For the sake of argument, let&#8217;s say these are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-20689","post","type-post","status-publish","format-standard","hentry","category-good-robot"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20689","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=20689"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20689\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}