WEBVTT 00:00:00.000 --> 00:00:05.305 You've seen a little bit of JavaScript code and in this section; I wanna talk 00:00:05.305 --> 00:00:10.473 about what connect to that world to the machine code in the CPU. So, this is 00:00:10.473 --> 00:00:15.705 beyond generally the topic of computer languages. So a programmer works in some 00:00:15.705 --> 00:00:20.652 computer language and computer languages are said to have high quote, high level 00:00:20.652 --> 00:00:25.722 features and that means things like loops and if statements and strings. Those are 00:00:25.722 --> 00:00:30.545 facilities that are more complicated, that are most present in a really simple 00:00:30.545 --> 00:00:35.307 machine code on the CPU. So the programmer writes what it's called source code in 00:00:35.307 --> 00:00:39.623 whatever computer language they are writing. So here is an example of some C++ 00:00:39.623 --> 00:00:43.993 source code, this assigns a string into a variable and then puts and exclamation 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 00:00:48.145 --> 00:00:52.624 JavaScript code we've seen A lot of these languages at the syntactic level and of 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 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 00:01:04.232 --> 00:01:10.157 called a Compiler. And, the Compiler is a separate program. And what the compiler 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, 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 00:01:20.215 --> 00:01:24.645 source code where there is an if statement. And there is not a specific 00:01:24.645 --> 00:01:29.555 instruction in a machine code for an if statement but maybe there's a sequence of 00:01:29.555 --> 00:01:34.404 five machine code instructions that in effect make up an if statement and so the 00:01:34.404 --> 00:01:40.991 compiler does that kind of expansion. So, just an example Firefox is written in C++ 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 00:01:49.835 --> 00:01:54.706 in. Of the many source files that adamant, the great body of source code that makes 00:01:54.706 --> 00:01:59.387 out Firefox and proves that essentially, firefox.exe. Firefox.exe is the output of 00:01:59.387 --> 00:02:04.449 the compiler to just translate all of that source code. Couple of things to point out 00:02:04.449 --> 00:02:09.180 about this The compilation only has to happen once so somebody with source code, 00:02:09.180 --> 00:02:13.340 someone makes Fi refox [inaudible] and then you can just send out Firefox 00:02:13.340 --> 00:02:17.728 [inaudible]. The end users don't need the source code and they don't need the 00:02:17.728 --> 00:02:22.231 compiler so the compilation can just sort of to be done, done at the factory in 00:02:22.231 --> 00:02:26.579 effect. Another fact, another feature that this doesn't work backwards, So, I can 00:02:26.579 --> 00:02:30.954 distribute firefox.exe all through this machine code but there's not something 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 00:02:35.437 --> 00:02:39.813 is not perfectly, you can try to do it imperfectly but ideally, to produce a new 00:02:39.813 --> 00:02:44.296 version you who really wants the source code. So, let's talk about the source code 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 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 00:02:53.774 --> 00:02:58.549 done in JavaScript. So, add a few lines or an If statement to change some behavior 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 00:03:03.560 --> 00:03:07.982 version of Firefox that has this, this little change that you've put in the 00:03:07.982 --> 00:03:12.315 source code. And just as were, we're talking about source code. There's also 00:03:12.315 --> 00:03:16.818 give you, gives you a sense of what Open Source software means. So, Open Source 00:03:16.818 --> 00:03:22.140 software is a format where the program is distributed but also there's access to the 00:03:22.140 --> 00:03:26.993 original source code to the program and usually does come with a license saying, 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 00:03:31.788 --> 00:03:36.525 whatever, this program, feel free. So, the two main features of Open Source software 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. 00:03:41.200 --> 00:03:46.901 But the other feature is it comes with a sort of freedom. Because if the software 00:03:46.901 --> 00:03:51.031 is close source and you want some different feature there is some bug that 00:03:51.031 --> 00:03:55.050 you need implemented you're really dependent on the vendor who holds the 00:03:55.050 --> 00:03:59.069 source code like do they feel like implementing that feature. In contrast 00:03:59.069 --> 00:04:03.639 with open source software it's like there is no handcuffs you know the source code 00:04:03.639 --> 00:04:07.989 is available and if there is something that you really wanted to do or say the 00:04:07.989 --> 00:04:12.779 original vendor goes out of business then because you got the source code you could 00:04:12.779 --> 00:04:16.909 or you know maybe pay someone to make a change and produce your own custom 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 00:04:21.534 --> 00:04:25.615 policy. So often there's, there's many different types of license terms for open 00:04:25.615 --> 00:04:29.666 Source software but most often they require in some cases that if you take the 00:04:29.666 --> 00:04:33.569 source code and add some feature that you then offer those changes back to the 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 00:04:37.916 --> 00:04:42.017 benefit from your work. In, in the details on how that can be done is complicated. 00:04:42.017 --> 00:04:45.920 There's, there's many different forms of it. I'm gonna talk about open source 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 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 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 00:05:01.476 --> 00:05:06.012 another very big category of languages. We come in to call Dynamic Languages Or 00:05:06.012 --> 00:05:10.437 sometimes interpreter languages. And there's sort of a big strategy, there are 00:05:10.437 --> 00:05:14.530 many different languages with many different qualities and I'm just kinda 00:05:14.530 --> 00:05:18.955 sort of [inaudible] for this large category So examples of Dynamic Languages 00:05:18.955 --> 00:05:24.275 currently are Java and JavaScripts and Python. So one way that these can work is 00:05:24.275 --> 00:05:30.125 that they are implemented by what is called an interpreter as supposed to the 00:05:30.125 --> 00:05:36.500 compiler case I was describing earlier so an interpreter is a program which takes in 00:05:36.500 --> 00:05:42.675 code written in some language and sort of quote, unquote runs it so. Best example is 00:05:42.675 --> 00:05:47.300 that there is this computer language called JavaScript that actually we've been 00:05:47.300 --> 00:05:51.925 using and web browsers which are programs include as part of them a Java Script 00:05:51.925 --> 00:05:56.607 interpreter and so when the web browser comes across a page with some JavaScript 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 00:06:01.406 --> 00:06:07.550 going to do. So, an available detail, the way an interpreter works. Is that it, it 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 00:06:11.296 --> 00:06:15.599 JavaScript code. If the interpreter wanted t run this, well, we'll look at the first 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. 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 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 00:06:28.102 --> 00:06:32.425 does the next line and so on. So the compiler does this big bulk translation 00:06:32.425 --> 00:06:36.822 from the source code into machine code. But it's not really running code, it's 00:06:36.822 --> 00:06:41.219 just doing a translation to be run in the future. In contrast, the interpreter 00:06:41.219 --> 00:06:45.616 really lives in the moment Just going through the code line by line and just 00:06:45.616 --> 00:06:50.440 doing it. So let me talk a little about the differences between these two and I 00:06:50.440 --> 00:06:55.552 should tell you as a disclaimer there are many computer languages. There is no best 00:06:55.552 --> 00:06:59.993 language. The old joke is that. Really when, you know, you're on this seminar and 00:06:59.993 --> 00:07:03.751 you are arguing about the pros and cons or something. Ultimately, the answer is just 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 00:07:07.147 --> 00:07:11.331 language for the situation with all the, with all the feature. So broadly speaking, 00:07:11.331 --> 00:07:16.214 compiler code does tend to run faster and when you put, when you compile C++ code 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 00:07:21.158 --> 00:07:26.844 has been stripped out as much as possible. On the other hand, dynamic or interpreter 00:07:26.844 --> 00:07:32.097 languages tend to have more features. More features that make their programmers job a 00:07:32.097 --> 00:07:37.287 little bit easier or put in another way. Broadly speaking, programmers can tend to 00:07:37.287 --> 00:07:42.193 get more work done per hour in dynamic languages than they do in compiled 00:07:42.193 --> 00:07:47.893 languages. However there is no free lunch. It is also the case that dynamic language 00:07:47.893 --> 00:07:53.196 code tends to run slower on the CPU in terms of just getting work done compared 00:07:53.196 --> 00:07:58.500 to a compiled language, compiled source code that was going to do the same thing. 00:07:58.670 --> 00:08:03.050 So, an example of this, a great example of this is memory management. So, memory 00:08:03.050 --> 00:08:07.886 management refers to the problem in the code of knowing how many bytes are needed 00:08:07.886 --> 00:08:12.323 to store something And in particular, for how long. Well, I need these bytes for 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 00:08:17.101 --> 00:08:21.424 those bytes for something else. And that is actually a kind of a complicated 00:08:21.424 --> 00:08:26.077 problem. In C and C++ memory management is to a degree manual and that just means 00:08:26.077 --> 00:08:31.103 that the programmer, they're producing all these lines of codes to do stuff and there 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 00:08:36.011 --> 00:08:40.445 guide the memory management so there is some programmer effort required. In 00:08:40.445 --> 00:08:45.293 contrast most dynamic language have what's called automatic memory management and 00:08:45.293 --> 00:08:50.463 this means that. No programmer input is required that the lower on the stacks that 00:08:50.463 --> 00:08:55.605 the language is gonna notice automatically when memory is needed; when memory is not 00:08:55.605 --> 00:09:00.708 needed anymore it can be recycled and just take on that whole problem. So, this fits 00:09:00.708 --> 00:09:04.599 within the, the, the trend of dynamic languages that I was mentioning earlier. 00:09:04.599 --> 00:09:08.642 So this is an example of how well, you know, because the programmers has less to 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 00:09:12.839 --> 00:09:16.781 great example of that. However, you can also imagine this is one of the things 00:09:16.781 --> 00:09:21.029 that makes dynamic language codes run a little slower because the CPU now has this 00:09:21.029 --> 00:09:25.380 additional bookkeeping problem where it's looking at memory and trying to figure out 00:09:25.380 --> 00:09:31.208 of when it can be reused. So, very broadly speaking the trend for computer 00:09:31.208 --> 00:09:37.768 programming today is towards dynamic languages that the trade off that. The 00:09:37.768 --> 00:09:42.448 programmer will get things done a little quicker at the expense of using a little 00:09:42.448 --> 00:09:47.128 more CPU or maybe a little more memory, that's a pretty attractive payoff for many 00:09:47.299 --> 00:09:51.865 for many problems. In this, maybe a little unintuitive you can think off. We'll 00:09:51.865 --> 00:09:56.145 what's the scariest quality for a computer program and oftentimes, it's the 00:09:56.145 --> 00:10:00.540 programmer that having skilled programmers working on a problem who, you know, 00:10:00.540 --> 00:10:05.010 working on those, working on these do main. A lot of times there are hard to 00:10:05.010 --> 00:10:10.046 find and so using up their hours. Using few of their hours looks attractive. In 00:10:10.046 --> 00:10:15.214 contrast the CPU only we want programs to run as fast as possible but Cpus are 00:10:15.214 --> 00:10:18.899 fairly, and if it got some fairly, effective and there was even this funny 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 00:10:22.988 --> 00:10:26.775 year gets a little bit cheaper, but if you think of the balance between the 00:10:26.775 --> 00:10:30.460 programmer cost and the CPU cost of getting something done, that means in 00:10:30.460 --> 00:10:34.196 terms of the share of the pie, the programmer is in a fact of getting more 00:10:34.196 --> 00:10:38.285 expensive each year. A little bit more scarce. So, this fits the pattern that on, 00:10:38.285 --> 00:10:42.346 on, on, on whole, dynamic languages are becoming a little bit more popular. Just 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 00:10:46.504 --> 00:10:50.982 Just in Time compiler and this is sort of the natural synthesis of the compiler and 00:10:50.982 --> 00:10:55.353 interpretative strategies of trying to get the best of both worlds. It's like, well, 00:10:55.353 --> 00:10:59.138 we'd like to have a dynamic language with all these features, all these 00:10:59.138 --> 00:11:03.882 programmer-friendly features but we wanted to run fast and so the JIT will take parts 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 00:11:07.827 --> 00:11:13.244 [inaudible] the two approaches. It turns out this works pretty well. So all modern 00:11:13.244 --> 00:11:18.642 browsers now have jets for JavaScript code so actually when you are running 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 00:11:24.963 --> 00:11:30.219 a very physical way on the fly, taking those scraps of JavaScript code and 00:11:30.219 --> 00:11:35.390 compiling them on the fly, Anyway it, You do not get exactly the formats of compiled 00:11:35.390 --> 00:11:39.220 code but it gets fairly close. So, this is, this is actually how most dynamic 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 00:11:43.102 --> 00:11:43.460 but it.