{"id":48101,"date":"2019-09-19T06:00:34","date_gmt":"2019-09-19T10:00:34","guid":{"rendered":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=48101"},"modified":"2019-09-19T07:07:04","modified_gmt":"2019-09-19T11:07:04","slug":"game-programming-vexations-part-4-high-level-vs-low-level","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=48101","title":{"rendered":"Game Programming Vexations Part 4: High Level vs. Low Level"},"content":{"rendered":"<p>Languages are usually described in terms of being &#8220;high level&#8221; or &#8220;low level&#8221;. This is usually presented as a tradeoff, and as a programmer you&#8217;re obliged to pick your poison.<\/p>\n<h3>High vs. Low Level Programming<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/assassins_creed_high.jpg' width=100% alt='Wow. This game must have been written in a very high level language. I&apos;m getting vertigo.' title='Wow. This game must have been written in a very high level language. I&apos;m getting vertigo.'\/><\/div><div class='mouseover-alt'>Wow. This game must have been written in a very high level language. I&apos;m getting vertigo.<\/div><\/p>\n<p>If you&#8217;re not a programmer, then I need to make it clear that these two concepts are probably the opposite of what you&#8217;d expect. A high-level language sounds like something for advanced programmers, and a low-level language sounds like something for beginners. But in classic engineer thinking, these paradigms are named from the perspective of the machinery, not the people using them.<\/p>\n<p>A low-level language is said to be &#8220;close to the metal&#8221;. Your code is involved with manipulating individual blocks of memory and worrying about processor cycles. It&#8217;s very fussy work and it takes a lot of code to get anything done, but when you&#8217;ve got it written you can be confident that it will be incredibly efficient<span class='snote' title='1'>Assuming you&#8217;re knowledgeable and experienced, you didn&#8217;t create any major bugs, and the limitations of the target platform were made clear to you and were accurate. You know, the usual.<\/span>.\u00a0<\/p>\n<p>A high-level language allows you to express complex actions using very simple bits of easily-written code. It&#8217;s easy to write, but often wastes processor cycles and memory. How much? There are arguments all the time over whether the overhead for language X is significant or trivial<span class='snote' title='2'>And I&#8217;ll bet your viewpoint depends on your domain.<\/span>.<\/p>\n<p>If you want to output the phrase &#8220;Hello World!&#8221; to the console, then here is how you do that using assembler, the lowest of the low-level languages:<\/p>\n<p><!--more--><\/p>\n<pre lang=\"asm\">\r\nSECTION .DATA\r\n\r\n   hello: \u00a0 \u00a0 db 'Hello world!',10\r\n\r\n   helloLen:\u00a0 equ $-hello\r\n\r\n\r\n\r\nSECTION .TEXT\r\n\r\n   GLOBAL _start\u00a0\r\n\r\n_start:\r\n\r\n   mov eax,4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n   mov ebx,1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n   mov ecx,hello\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n   mov edx,helloLen\u00a0\r\n\r\n   int 80h\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n\r\n\r\n   mov eax,1\u00a0\u00a0\u00a0\u00a0\r\n\r\n   mov ebx,0\u00a0\u00a0\u00a0\u00a0\r\n\r\n   int 80h\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\r\n<\/pre>\n<p>That&#8217;s a great big wall of inscrutable nonsense to perform a very simple task.\u00a0<\/p>\n<p>The BASIC programming language is extremely high-level and was a mainstay of computer education back in the 1980s. Here&#8217;s that same task implemented in BASIC:<\/p>\n<pre lang=\"blitzbasic\">100 PRINT \"Hello World!\"<\/pre>\n<p>That&#8217;s a pretty big difference in both size and readability. For reference, C++ falls somewhere between these two extremes. It&#8217;s considered a pretty low-level language by today&#8217;s standards.\u00a0I know I&#8217;ve covered this topic before on the site, but for those who are foggy on the details, here is&#8230;<\/p>\n<h3>Yet Another Terrible Car Analogy&trade;<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/stock_toy_car_on_map.jpg' width=100% alt='Not to scale.' title='Not to scale.'\/><\/div><div class='mouseover-alt'>Not to scale.<\/div><\/p>\n<p>You can imagine driving directions for a road trip from New York to L.A. The low-level version would tell you when to leave, what specific roads to take, when to stop for fuel, where to stop for food, and how fast to travel on each road to optimize fuel usage and synchronize with local traffic lights. A high-level version of the directions would be &#8220;Drive west until you hit the coast and then head south.&#8221;\u00a0<\/p>\n<p>The high-level directions are easy to write, easy to read, and are unlikely to contain any errors. The low-level directions are time-consuming to write, difficult to follow, prone to mistakes, but massively more efficient if they&#8217;re written properly<span class='snote' title='3'>And in this case, &#8220;properly&#8221; means basically &#8220;perfectly&#8221;.<\/span>.\u00a0<\/p>\n<p>In computer science, this is usually portrayed as an either-or type deal. If you want to do things the easy way, then you have to abandon efficiency. If you want efficiency, it will come at the expense of more work. In C++ you can make your programs very fast and keep your memory usage low, but your code will be massive, dense, and difficult to maintain. In Java it&#8217;s easier to write and maintain the code, but you&#8217;ll waste a lot of memory and processor cycles<span class='snote' title='4'>Actually, the resources wasted are completely trivial and never something you have to worry about. Just ask a Java programmer.<\/span>.<\/p>\n<p>What sort of language you use depends a great deal on the type of problem you&#8217;re trying to solve. If you&#8217;re writing an operating system or device driver, then you&#8217;ll probably want to use something low-level. If you&#8217;re writing something fun and simple with lots of user interface elements, then you want a higher-level language.\u00a0In my personal experience, writing interface code (dialog buttons, lists, scrolling text boxes, etc.) are pure torture to write in a low-level language. <\/p>\n<p>The problem with games<span class='snote' title='5'>And maybe lots of other domains. I dunno. I can&#8217;t speak for them.<\/span> is that you kind of need both. You want the guts of your rendering pipeline to be written as close to the metal as possible. On the other hand, a lot of your code is going to be dedicated to expressing the rules of the game: Hitpoints, damage, inventory, etc. That code usually isn&#8217;t very costly<span class='snote' title='6'>Depends on the genre, obviously.<\/span> in terms of processor cycles. It doesn&#8217;t need to be expressed in low level terms, and it&#8217;s often extra work to do so.\u00a0<\/p>\n<h3>So Let&#8217;s Use Two Languages!<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/stock_road_slow.jpg' width=100% alt='We&apos;ve decided to implement the project in two different languages, both of which are slow.' title='We&apos;ve decided to implement the project in two different languages, both of which are slow.'\/><\/div><div class='mouseover-alt'>We&apos;ve decided to implement the project in two different languages, both of which are slow.<\/div><\/p>\n<p>Developers have sometimes routed around this problem by using two languages. C++ will be used to make the costly stuff like rendering, physics, and audio. Then the gameplay stuff will be offloaded to a scripting language like UnrealScript, <a href=\"http:\/\/www.cipscis.com\/skyrim\/tutorials\/beginners.aspx\">Papyrus<\/a>, or <a href=\"https:\/\/www.lua.org\/about.html\">LUA<\/a><span class='snote' title='7'>There&#8217;s also a second reason for using a scripting language, which is that it allows end-users to make gameplay mods without giving them access to the engine.<\/span>. This allows the developer to use the harder language when speed counts and an easy language when it doesn&#8217;t, although it has the obvious drawback of having the game implemented in two different languages. Also, you can&#8217;t really shove all of your performance-critical functionality into a box like that. In a complex system, there&#8217;s not always a clear line between the &#8220;engine&#8221; and the &#8220;game&#8221;.<\/p>\n<p>Sometimes you&#8217;ve got a lot of game objects (like particles) that are so numerous that you have to think very hard about how they&#8217;re processed and you need to be very careful with their memory footprint. Sometimes you&#8217;ve got other game objects (like a bounding box to trigger a cutscene) that are few in number and require no special performance considerations. Sometimes objects start at one extreme and migrate to the other as your design changes. It really sucks if you realize you need to migrate some code from one side of the wall to the other, because that means re-implementing everything in the other language. It would be nice if we could make the transition from &#8220;convenient to write&#8221; to &#8220;fussy and finely-tuned&#8221; without needing to rewrite all of the code in a different language. <\/p>\n<p><a href=\"?p=42804\">I spent some time learning C# and Unity last year<\/a>, and I found myself questioning the inevitability of this tradeoff between processing efficiency and programmer expressiveness. I was amazed at how much faster it was to work in an environment with so many convenience features, but then I&#8217;d run into a situation where the language <a href=\"?p=42796\">wouldn&#8217;t let me ask simple questions or take reasonable steps to ensure performance<\/a>. These two things seem to be orthogonal, and I don&#8217;t see why we can&#8217;t have both. <\/p>\n<p>Okay, I can understand why we can&#8217;t do both <strong>at the same time<\/strong>. But I don&#8217;t see why our language must limit us to one particular level of expression. Sometimes it would be nice to just PRINT &#8220;Hello World!&#8221; and other times you need to crawl down into the guts of the engine to allocate and\u00a0 manually manipulate individual bytes of memory.<\/p>\n<p>In computer games, you don&#8217;t always need speed and ruthlessly frugal memory management. But when you <b>do<\/b> need those things, you <b>really<\/b> need those things. As the program grows in complexity and the performance needs become more clear, more of the systems will need to migrate from the &#8220;easy to write and manage&#8221; side to the &#8220;fussy and difficult to understand&#8221; side of things. Not only do you have to re-write the code in the new language, but you have to build a bridge between the two so your slow-ass scripting language can hand a tricky job off to your cryptic low-level language. And if you&#8217;re dealing with complex objects with lots of data &#8211; like Space Marines or space orcs &#8211; then you have to describe that data in both languages and keep those bits of code synchronized. <\/p>\n<p>This isn&#8217;t an impossible task or anything. Coders do it all the time. But it would be nice if you didn&#8217;t <strong>have<\/strong> to. <\/p>\n<p>A language designed specifically for games could <strong>perhaps<\/strong> allow us to be exacting when performance is crucial, and expressive when it isn&#8217;t. Sometimes you need direct memory access so you can worry about the placement of individual bytes, and sometimes you just want to process some simple data without having to think about the hardware.\u00a0<\/p>\n<h3>Good Programmers<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/stock_bad_programmer.jpg' width=100% alt='I know it&apos;s arrogant to make snap judgement about the skills of other programmers before you&apos;ve seen their work, but I feel safe in thinking that maybe this guy isn&apos;t a top-tier coder.' title='I know it&apos;s arrogant to make snap judgement about the skills of other programmers before you&apos;ve seen their work, but I feel safe in thinking that maybe this guy isn&apos;t a top-tier coder.'\/><\/div><div class='mouseover-alt'>I know it&apos;s arrogant to make snap judgement about the skills of other programmers before you&apos;ve seen their work, but I feel safe in thinking that maybe this guy isn&apos;t a top-tier coder.<\/div><\/p>\n<p>The second most controversial thing that Jon Blow has said about Jai is that it&#8217;s a language designed for &#8220;good programmers&#8221;. (The <b>most<\/b> controversial thing was his announcement to build the language in the first place.) This rubbed a lot of people the wrong way, since it seems to imply that all those other languages are for &#8220;bad programmers&#8221;.\u00a0<\/p>\n<p>Direct memory manipulation is a classic example. It&#8217;s tough to do properly, and very easy to make mistakes. The compiler usually can&#8217;t help you identify mistakes in memory management, which leaves you to face the problem on your own. Worse, memory management problems cause catastrophic bugs like crashes or slowdowns.\u00a0<\/p>\n<p>(Direct memory manipulation is a bit of an oversimplification for what I&#8217;m talking about here. I&#8217;m not just talking about allocating memory and managing it yourself, I&#8217;m also talking about allowing the programmer to specify the layout of things in memory, gain direct memory access to things their code didn&#8217;t allocate, and copy arbitrary blocks of memory from A to B.)<\/p>\n<p>Back in the bad old days of C, this problem was really common. Every single string manipulation required manual memory management. Even if you just want to print &#8220;Hello World&#8221; to the screen, you need to take responsibility for those 12 bytes of memory<span class='snote' title='8'>One byte for every character in the message, plus one extra to mark the ending of the string. As you can imagine, that &#8220;one extra byte&#8221; business made it very easy to make subtle mistakes.<\/span> and keep track of them yourself. If you make a mistake in managing memory like this, you&#8217;ll generally crash or experience bewildering buggy behavior. It&#8217;s like trying to prepare a sandwich when you don&#8217;t have pre-sliced bread and your only knife is an industrial buzzsaw. <i>This is a simple task. Why does it need to be so dangerous?<\/i><\/p>\n<p>Some languages try to protect the programmer. They impose structures and syntax to make it difficult or impossible to make these kinds of blunders. Effectively, the language takes away your buzzsaw. When coders complain that they need a buzzsaw, people explain that you shouldn&#8217;t be using a buzzsaw anyway because they tend to cause more problems than they solve.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/stock_buzzsaw.jpg' width=100% alt='Dangit. I cut this this board twice and it&apos;s STILL too short!' title='Dangit. I cut this this board twice and it&apos;s STILL too short!'\/><\/div><div class='mouseover-alt'>Dangit. I cut this this board twice and it&apos;s STILL too short!<\/div><\/p>\n<p>I don&#8217;t like the narrative that the world is flooded with idiot programmers, but it is an observable truth that software has a lot of bugs in it, and statistically those bugs are the result of a small handful of common mistakes. It makes sense to take those hard-learned lessons and use that knowledge to build safer languages in the future.\u00a0<\/p>\n<p>This leads back to the classic programmer argument:<\/p>\n<p>Alice: This tool is terrible! Look at how many problems it causes!<\/p>\n<p>Bob: The tool is fine. You&#8217;re just bad at using it!<\/p>\n<p>Not every company can afford to hire expert-level coders for every language. Sometimes the most qualified person to maintain that legacy C code is the woman with two decades of Java experience. It would be nice if we could afford our own cryogenically frozen greybeard C coder and we could thaw him out every couple of years when something needs to be changed, but the only thing more expensive than cryo tanks is greybeard C coders.<\/p>\n<p>So we build a language that takes away the buzzsaw and gives the coder safety scissors. That won&#8217;t help us maintain this ancient C code, but it will help us to avoid leaving these sorts of messes for future coders.<\/p>\n<p>But the buzzsaw DID have legitimate uses, and if you take away the tool entirely then you forever lose the ability to solve those problems. That helps all those other programmers who struggle with the tool, but it leaves you weaponless when you come up against a problem that requires it.\u00a0<\/p>\n<p>So we have this perceived trade-off between expressiveness and performance. When it comes to manipulating strings, C++ offers a great example of a case where we can have both. The C++ standard libraries offer specific types for manipulating strings. You can use these types when you just want to shuffle some text data around when you&#8217;re not worried about performance. If you are concerned about performance<span class='snote' title='9'>Like, you have a program that needs to chew through gigabytes of log files for whatever reason.<\/span>, you can always pull out the buzzsaw and go back to the classic C style of handling text. I can imagine a language designed for games that offers this sort of choice everywhere. It would be a complicated language, but it would probably be less complicated than two unrelated languages being used in tandem, which is where we are right now.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/stock_saw_thumb.jpg' width=100% alt='And thus began a chain of events in which everyone involved would learn a valuable lesson.' title='And thus began a chain of events in which everyone involved would learn a valuable lesson.'\/><\/div><div class='mouseover-alt'>And thus began a chain of events in which everyone involved would learn a valuable lesson.<\/div><\/p>\n<p>Those safety-scissor languages<span class='snote' title='10'>Please don&#8217;t take this as mocking towards those languages. I&#8217;m not saying languages like C# or Java are for kids or that you can&#8217;t do serious work with them. I&#8217;m just trying to explain this divide without throwing too much jargon at the non-technical readers.<\/span> are designed with the built-in assumption: &#8220;The programmer might make mistakes, so let&#8217;s limit their ability to hurt themselves.&#8221; Saying Jai is designed for &#8220;Good Programmers&#8221; doesn&#8217;t mean people using those other languages are lesser programmers, it means <b>the language is going to assume the programmer knows what they&#8217;re doing and isn&#8217;t going to try to save them from themselves.<\/b> Jai programmers will be just as flawed and error-prone as programmers in other languages because we&#8217;re all human, but Jai is designed to allow you to make those mistakes because sometimes a buzzsaw really is the best tool for the job.<\/p>\n<p><b>Shamus, doesn&#8217;t it seem sort of rude to refer to the language as being for &#8220;good programmers&#8221;?<\/b><\/p>\n<p>Doesn&#8217;t it seem sort of rude for all these other languages to assume you&#8217;re <b>not<\/b> a good programmer? If we&#8217;re going to get offended by the assumptions built into these languages then we&#8217;ll never run out of things to get mad about.<\/p>\n<p>Again, domain is everything. A chef shakes his head and talks about the crazy old times when they had to make sandwiches with buzzsaws, while the carpenters are like, &#8220;Why does it need to be so hard to get my hands on a simple buzzsaw these days? Do you have any idea how hard it is to cut through oak with a bread knife?&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Languages are usually described in terms of being &#8220;high level&#8221; or &#8220;low level&#8221;. This is usually presented as a tradeoff, and as a programmer you&#8217;re obliged to pick your poison. High vs. Low Level Programming If you&#8217;re not a programmer, then I need to make it clear that these two concepts are probably the opposite [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-48101","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/48101","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=48101"}],"version-history":[{"count":20,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/48101\/revisions"}],"predecessor-version":[{"id":48121,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/48101\/revisions\/48121"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=48101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=48101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=48101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}