1 00:00:00,000 --> 00:00:05,305 You've seen a little bit of JavaScript code and in this section; I wanna talk 2 00:00:05,305 --> 00:00:10,473 about what connect to that world to the machine code in the CPU. So, this is 3 00:00:10,473 --> 00:00:15,705 beyond generally the topic of computer languages. So a programmer works in some 4 00:00:15,705 --> 00:00:20,652 computer language and computer languages are said to have high quote, high level 5 00:00:20,652 --> 00:00:25,722 features and that means things like loops and if statements and strings. Those are 6 00:00:25,722 --> 00:00:30,545 facilities that are more complicated, that are most present in a really simple 7 00:00:30,545 --> 00:00:35,307 machine code on the CPU. So the programmer writes what it's called source code in 8 00:00:35,307 --> 00:00:39,623 whatever computer language they are writing. So here is an example of some C++ 9 00:00:39,623 --> 00:00:43,993 source code, this assigns a string into a variable and then puts and exclamation 10 00:00:43,993 --> 00:00:48,145 mark on the end of that string. You know what? Some, it's kinda similar to the 11 00:00:48,145 --> 00:00:52,624 JavaScript code we've seen A lot of these languages at the syntactic level and of 12 00:00:52,624 --> 00:00:57,927 their kinda similar to use of quotes and semicolons or what not. So how do you get 13 00:00:57,927 --> 00:01:04,232 from the C++ source code say to running on a CPU? So one strategy used is what's 14 00:01:04,232 --> 00:01:10,157 called a Compiler. And, the Compiler is a separate program. And what the compiler 15 00:01:10,157 --> 00:01:15,186 does is it looks at all of the source code and it does a bulk translation over to a, 16 00:01:15,186 --> 00:01:20,215 to create a big body of machine code. So for example maybe there is a part of the 17 00:01:20,215 --> 00:01:24,645 source code where there is an if statement. And there is not a specific 18 00:01:24,645 --> 00:01:29,555 instruction in a machine code for an if statement but maybe there's a sequence of 19 00:01:29,555 --> 00:01:34,404 five machine code instructions that in effect make up an if statement and so the 20 00:01:34,404 --> 00:01:40,991 compiler does that kind of expansion. So, just an example Firefox is written in C++ 21 00:01:41,296 --> 00:01:49,835 and so, to make a new version of Firefox someone runs the C++ compiler and it reads 22 00:01:49,835 --> 00:01:54,706 in. Of the many source files that adamant, the great body of source code that makes 23 00:01:54,706 --> 00:01:59,387 out Firefox and proves that essentially, firefox.exe. Firefox.exe is the output of 24 00:01:59,387 --> 00:02:04,449 the compiler to just translate all of that source code. Couple of things to point out 25 00:02:04,449 --> 00:02:09,180 about this The compilation only has to happen once so somebody with source code, 26 00:02:09,180 --> 00:02:13,340 someone makes Fi refox [inaudible] and then you can just send out Firefox 27 00:02:13,340 --> 00:02:17,728 [inaudible]. The end users don't need the source code and they don't need the 28 00:02:17,728 --> 00:02:22,231 compiler so the compilation can just sort of to be done, done at the factory in 29 00:02:22,231 --> 00:02:26,579 effect. Another fact, another feature that this doesn't work backwards, So, I can 30 00:02:26,579 --> 00:02:30,954 distribute firefox.exe all through this machine code but there's not something 31 00:02:30,954 --> 00:02:35,437 that will read that in and go backwards to give me a big body of source code. Really 32 00:02:35,437 --> 00:02:39,813 is not perfectly, you can try to do it imperfectly but ideally, to produce a new 33 00:02:39,813 --> 00:02:44,296 version you who really wants the source code. So, let's talk about the source code 34 00:02:44,296 --> 00:02:48,881 a little bit. So if you wanna add a feature of fix a bug to Firefox, real way 35 00:02:48,881 --> 00:02:53,774 to do that is to go back to the original source code and rework it just what we've 36 00:02:53,774 --> 00:02:58,549 done in JavaScript. So, add a few lines or an If statement to change some behavior 37 00:02:58,549 --> 00:03:03,560 and some way into source code and then run the compiler again to make in effect a new 38 00:03:03,560 --> 00:03:07,982 version of Firefox that has this, this little change that you've put in the 39 00:03:07,982 --> 00:03:12,315 source code. And just as were, we're talking about source code. There's also 40 00:03:12,315 --> 00:03:16,818 give you, gives you a sense of what Open Source software means. So, Open Source 41 00:03:16,818 --> 00:03:22,140 software is a format where the program is distributed but also there's access to the 42 00:03:22,140 --> 00:03:26,993 original source code to the program and usually does come with a license saying, 43 00:03:26,993 --> 00:03:31,788 hey here's the source code if you wanna make your own version or if it's about 44 00:03:31,788 --> 00:03:36,525 whatever, this program, feel free. So, the two main features of Open Source software 45 00:03:36,525 --> 00:03:40,970 is one, it's, it's typically distributed for free so it's just, you know, cheap. 46 00:03:41,200 --> 00:03:46,901 But the other feature is it comes with a sort of freedom. Because if the software 47 00:03:46,901 --> 00:03:51,031 is close source and you want some different feature there is some bug that 48 00:03:51,031 --> 00:03:55,050 you need implemented you're really dependent on the vendor who holds the 49 00:03:55,050 --> 00:03:59,069 source code like do they feel like implementing that feature. In contrast 50 00:03:59,069 --> 00:04:03,639 with open source software it's like there is no handcuffs you know the source code 51 00:04:03,639 --> 00:04:07,989 is available and if there is something that you really wanted to do or say the 52 00:04:07,989 --> 00:04:12,779 original vendor goes out of business then because you got the source code you could 53 00:04:12,779 --> 00:04:16,909 or you know maybe pay someone to make a change and produce your own custom 54 00:04:16,909 --> 00:04:21,534 version. And so it comes with this sort of deep, you could take this as an insurance 55 00:04:21,534 --> 00:04:25,615 policy. So often there's, there's many different types of license terms for open 56 00:04:25,615 --> 00:04:29,666 Source software but most often they require in some cases that if you take the 57 00:04:29,666 --> 00:04:33,569 source code and add some feature that you then offer those changes back to the 58 00:04:33,569 --> 00:04:37,916 community. So sort of in the same way that you've benefited from others work they can 59 00:04:37,916 --> 00:04:42,017 benefit from your work. In, in the details on how that can be done is complicated. 60 00:04:42,017 --> 00:04:45,920 There's, there's many different forms of it. I'm gonna talk about open source 61 00:04:45,920 --> 00:04:50,020 software a little bit later on but I just want to mention it now because it really 62 00:04:50,020 --> 00:04:54,170 gets up this idea of what is the source code of a program versus just having the 63 00:04:54,170 --> 00:05:01,476 .exe or something. So that's why I just talked about C++. So there's, a whole 64 00:05:01,476 --> 00:05:06,012 another very big category of languages. We come in to call Dynamic Languages Or 65 00:05:06,012 --> 00:05:10,437 sometimes interpreter languages. And there's sort of a big strategy, there are 66 00:05:10,437 --> 00:05:14,530 many different languages with many different qualities and I'm just kinda 67 00:05:14,530 --> 00:05:18,955 sort of [inaudible] for this large category So examples of Dynamic Languages 68 00:05:18,955 --> 00:05:24,275 currently are Java and JavaScripts and Python. So one way that these can work is 69 00:05:24,275 --> 00:05:30,125 that they are implemented by what is called an interpreter as supposed to the 70 00:05:30,125 --> 00:05:36,500 compiler case I was describing earlier so an interpreter is a program which takes in 71 00:05:36,500 --> 00:05:42,675 code written in some language and sort of quote, unquote runs it so. Best example is 72 00:05:42,675 --> 00:05:47,300 that there is this computer language called JavaScript that actually we've been 73 00:05:47,300 --> 00:05:51,925 using and web browsers which are programs include as part of them a Java Script 74 00:05:51,925 --> 00:05:56,607 interpreter and so when the web browser comes across a page with some JavaScript 75 00:05:56,607 --> 00:06:01,406 code in it, it can use the interpreter to run that code to make it do whatever it's 76 00:06:01,406 --> 00:06:07,550 going to do. So, an available detail, the way an interpreter works. Is that it, it 77 00:06:07,550 --> 00:06:11,296 deals with the program one line at a time. So, if, so here's a little bit of 78 00:06:11,296 --> 00:06:15,599 JavaScript code. If the interpreter wanted t run this, well, we'll look at the first 79 00:06:15,599 --> 00:06:19,749 line and it would look at what that line says and then the interpreter would do it. 80 00:06:19,749 --> 00:06:23,951 So in this case the interpreter would say oh well, I guess I need a variable name a 81 00:06:23,951 --> 00:06:28,102 and I need to put a one in it. So, once it summed at that line it goes ahead and it 82 00:06:28,102 --> 00:06:32,425 does the next line and so on. So the compiler does this big bulk translation 83 00:06:32,425 --> 00:06:36,822 from the source code into machine code. But it's not really running code, it's 84 00:06:36,822 --> 00:06:41,219 just doing a translation to be run in the future. In contrast, the interpreter 85 00:06:41,219 --> 00:06:45,616 really lives in the moment Just going through the code line by line and just 86 00:06:45,616 --> 00:06:50,440 doing it. So let me talk a little about the differences between these two and I 87 00:06:50,440 --> 00:06:55,552 should tell you as a disclaimer there are many computer languages. There is no best 88 00:06:55,552 --> 00:06:59,993 language. The old joke is that. Really when, you know, you're on this seminar and 89 00:06:59,993 --> 00:07:03,751 you are arguing about the pros and cons or something. Ultimately, the answer is just 90 00:07:03,751 --> 00:07:07,147 it depends. Like there's not a best language, it's like well, what's the best 91 00:07:07,147 --> 00:07:11,331 language for the situation with all the, with all the feature. So broadly speaking, 92 00:07:11,331 --> 00:07:16,214 compiler code does tend to run faster and when you put, when you compile C++ code 93 00:07:16,214 --> 00:07:21,158 and produce that EXE, it's sort of in a sense lean. A lot of detail and decisions 94 00:07:21,158 --> 00:07:26,844 has been stripped out as much as possible. On the other hand, dynamic or interpreter 95 00:07:26,844 --> 00:07:32,097 languages tend to have more features. More features that make their programmers job a 96 00:07:32,097 --> 00:07:37,287 little bit easier or put in another way. Broadly speaking, programmers can tend to 97 00:07:37,287 --> 00:07:42,193 get more work done per hour in dynamic languages than they do in compiled 98 00:07:42,193 --> 00:07:47,893 languages. However there is no free lunch. It is also the case that dynamic language 99 00:07:47,893 --> 00:07:53,196 code tends to run slower on the CPU in terms of just getting work done compared 100 00:07:53,196 --> 00:07:58,500 to a compiled language, compiled source code that was going to do the same thing. 101 00:07:58,670 --> 00:08:03,050 So, an example of this, a great example of this is memory management. So, memory 102 00:08:03,050 --> 00:08:07,886 management refers to the problem in the code of knowing how many bytes are needed 103 00:08:07,886 --> 00:08:12,323 to store something And in particular, for how long. Well, I need these bytes for 104 00:08:12,323 --> 00:08:17,101 here on line six but maybe on line twenty. I don't need them anymore and I can reuse 105 00:08:17,101 --> 00:08:21,424 those bytes for something else. And that is actually a kind of a complicated 106 00:08:21,424 --> 00:08:26,077 problem. In C and C++ memory management is to a degree manual and that just means 107 00:08:26,077 --> 00:08:31,103 that the programmer, they're producing all these lines of codes to do stuff and there 108 00:08:31,103 --> 00:08:36,011 is going to be times when they maybe to add some lines to do some thinking to help 109 00:08:36,011 --> 00:08:40,445 guide the memory management so there is some programmer effort required. In 110 00:08:40,445 --> 00:08:45,293 contrast most dynamic language have what's called automatic memory management and 111 00:08:45,293 --> 00:08:50,463 this means that. No programmer input is required that the lower on the stacks that 112 00:08:50,463 --> 00:08:55,605 the language is gonna notice automatically when memory is needed; when memory is not 113 00:08:55,605 --> 00:09:00,708 needed anymore it can be recycled and just take on that whole problem. So, this fits 114 00:09:00,708 --> 00:09:04,599 within the, the, the trend of dynamic languages that I was mentioning earlier. 115 00:09:04,599 --> 00:09:08,642 So this is an example of how well, you know, because the programmers has less to 116 00:09:08,642 --> 00:09:12,839 do they're able to just get stuff done a little quicker, so memory management is a 117 00:09:12,839 --> 00:09:16,781 great example of that. However, you can also imagine this is one of the things 118 00:09:16,781 --> 00:09:21,029 that makes dynamic language codes run a little slower because the CPU now has this 119 00:09:21,029 --> 00:09:25,380 additional bookkeeping problem where it's looking at memory and trying to figure out 120 00:09:25,380 --> 00:09:31,208 of when it can be reused. So, very broadly speaking the trend for computer 121 00:09:31,208 --> 00:09:37,768 programming today is towards dynamic languages that the trade off that. The 122 00:09:37,768 --> 00:09:42,448 programmer will get things done a little quicker at the expense of using a little 123 00:09:42,448 --> 00:09:47,128 more CPU or maybe a little more memory, that's a pretty attractive payoff for many 124 00:09:47,299 --> 00:09:51,865 for many problems. In this, maybe a little unintuitive you can think off. We'll 125 00:09:51,865 --> 00:09:56,145 what's the scariest quality for a computer program and oftentimes, it's the 126 00:09:56,145 --> 00:10:00,540 programmer that having skilled programmers working on a problem who, you know, 127 00:10:00,540 --> 00:10:05,010 working on those, working on these do main. A lot of times there are hard to 128 00:10:05,010 --> 00:10:10,046 find and so using up their hours. Using few of their hours looks attractive. In 129 00:10:10,046 --> 00:10:15,214 contrast the CPU only we want programs to run as fast as possible but Cpus are 130 00:10:15,214 --> 00:10:18,899 fairly, and if it got some fairly, effective and there was even this funny 131 00:10:18,899 --> 00:10:22,988 effect where Moore's Law keeps making it more truth. Where the CPU in a fact each 132 00:10:22,988 --> 00:10:26,775 year gets a little bit cheaper, but if you think of the balance between the 133 00:10:26,775 --> 00:10:30,460 programmer cost and the CPU cost of getting something done, that means in 134 00:10:30,460 --> 00:10:34,196 terms of the share of the pie, the programmer is in a fact of getting more 135 00:10:34,196 --> 00:10:38,285 expensive each year. A little bit more scarce. So, this fits the pattern that on, 136 00:10:38,285 --> 00:10:42,346 on, on, on whole, dynamic languages are becoming a little bit more popular. Just 137 00:10:42,346 --> 00:10:46,504 the last thing I'll mention is that there's, I think a thing called a JIT, a 138 00:10:46,504 --> 00:10:50,982 Just in Time compiler and this is sort of the natural synthesis of the compiler and 139 00:10:50,982 --> 00:10:55,353 interpretative strategies of trying to get the best of both worlds. It's like, well, 140 00:10:55,353 --> 00:10:59,138 we'd like to have a dynamic language with all these features, all these 141 00:10:59,138 --> 00:11:03,882 programmer-friendly features but we wanted to run fast and so the JIT will take parts 142 00:11:03,882 --> 00:11:07,827 of the code and try and compile them on the fly and so I try I to sort of 143 00:11:07,827 --> 00:11:13,244 [inaudible] the two approaches. It turns out this works pretty well. So all modern 144 00:11:13,244 --> 00:11:18,642 browsers now have jets for JavaScript code so actually when you are running 145 00:11:18,642 --> 00:11:24,963 JavaScript code even for this class inside of the browser there was a jet that was in 146 00:11:24,963 --> 00:11:30,219 a very physical way on the fly, taking those scraps of JavaScript code and 147 00:11:30,219 --> 00:11:35,390 compiling them on the fly, Anyway it, You do not get exactly the formats of compiled 148 00:11:35,390 --> 00:11:39,220 code but it gets fairly close. So, this is, this is actually how most dynamic 149 00:11:39,220 --> 00:11:43,102 languages were at first. And I'll say well, it's an act the value of research 150 00:11:43,102 --> 00:11:43,460 but it.