Just In Time (JIT) Compilers - Computerphile

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ย. 2022
  • A look at why (under certain circumstances) JIT Compilers can be so much faster. Dr Laurence Tratt of KCL takes us through the details.
    More about Laurie: bit.ly/C_LaurenceTratt
    / computerphile
    / computer_phile
    This video was filmed and edited by Sean Riley.
    Computer Science at the University of Nottingham: bit.ly/nottscomputer
    Computerphile is a sister project to Brady Haran's Numberphile. More at www.bradyharan.com

ความคิดเห็น • 567

  • @obiwanjacobi
    @obiwanjacobi ปีที่แล้ว +1367

    Do Just In Time Compilers come with Almost Too Late Debuggers?

    • @jesterflint9404
      @jesterflint9404 ปีที่แล้ว +40

      underrated. xD

    • @AndreaZzzXXX
      @AndreaZzzXXX ปีที่แล้ว +18

      great comment 🙂

    • @Mr.Leeroy
      @Mr.Leeroy ปีที่แล้ว +26

      never too late debuggers

    • @HrHaakon
      @HrHaakon ปีที่แล้ว +4

      Well, Java's debugger is also just in time, since it can attach over the network and talk to a running server... :p

    • @Komoto415
      @Komoto415 ปีที่แล้ว +5

      I think you misspelled "people"

  • @allanrichardson3135
    @allanrichardson3135 ปีที่แล้ว +222

    There was a feature in the very first version of FORTRAN, intended to mollify the Assembler users of the 1950s about “efficiency,” which allowed the programmer to “assist” the compiler in compiling efficient code for nested loops. Basically, it consisted of a FREQUENCY and an EQUIVALENCE statement. The programmer would specify an estimated number of inner loop executions per outer loop execution with FREQUENCY, and the pattern of memory reusability with EQUIVALENCE. Combined with other information collected during the compilation, FORTRAN could optimize when subscripts could be kept as the equivalent values in an index register, when they could be kept as index values but stored during a loop, or when they actually had to be kept in memory as subscripts (eg when the subscript value had to be printed out). These statements allowed the compiler to generate code closer in efficiency to what an Assembler programmer could produce, while wasting less time in the compiler doing the optimizing.
    These statements were always optional, and were often added after test runs to produce a production version of a program. Later compilers which required less optimization (because computers were faster and had more storage) retained the ability to accept these statements (for compatibility), but ignored them.
    These features helped overcome the bias of Assembler programmers against high level languages (known as “automatic” programming at the time), so that FORTRAN and other high level languages became successful.

    • @monad_tcp
      @monad_tcp ปีที่แล้ว +7

      The most ironic thing of all things about compilers is SSA - single statement assigment. It turns your damned mutable imperative program into a pure functional data-flow program ! its beautiful.
      Because that's the only way to optimize.

    • @pranaypallavtripathi2460
      @pranaypallavtripathi2460 ปีที่แล้ว +3

      @Allan Richardson that was some very nice to read piece of history 👍

    • @monad_tcp
      @monad_tcp ปีที่แล้ว +5

      @@pranaypallavtripathi2460 yep I never would know that what's essentially PGO - profile guided optimization. was possible in Fortran. Ahead of its time in all senses.
      C on the other hand, designed by comitee, but hacked down, literally from BCPL.
      Ambiguous parsing syntax, because why not, right?

    • @allanrichardson3135
      @allanrichardson3135 ปีที่แล้ว +1

      @@monad_tcp Actually it wasn’t possible in the FORTRAN language (ie the program being written in FORTRAN), but the COMPILER, written in Assembler (for the machine in question, initially the IBM 701) could optimize the generated code with these “hints” from the programmer. New terminology appeared as time went on, but the essence remained.
      Speaking of ambiguous parsing syntax, only two hard examples of ambiguity existed in FORTRAN that I could find: one in the DO statement and one in the FORMAT statement, and they existed in all versions of the language because, for compatibility, users would not allow the “space” character to have any delimiting function. Except as part of the text of an alphanumeric item in the FORMAT statement (and later in CHAR form constants), the spacing between actual language elements was DISREGARDED, and the syntax was defined so that the separation between elements did not affect the compiler.
      Example 1, the DO statement, used to create an iterative loop: the general form is DO = , [ , ]
      where = label number of a statement, value 1 through 32767,
      which ends the range of the loop,
      = the name of a simple integer variable, consisting of one letter (I-N) followed by zero through 5 characters which can be letters (A-Z) or digits,
      And , , and can be integer constants [-]#…#, subscripted integer variables (subscript[s]) or expressions. But the arithmetic statement format is [(subscript[s]) = expression. Therefore the DO statement “DO 35 I = 1 , 100” written with no spaces (which is how the compiler sees it) as DO35I=1,100 could also be an arithmetic statement up until the first comma, after which if DO35I can be divided into “DO 35 I = 1,” where 35 is a valid statement number and I is a non-conflicting integer variable name, it is compiled as a DO statement; otherwise it is an arithmetic statement, which may contain errors.
      Likewise, the FORMAT statement, used in I/O operations, is defined as “FORMAT()”
      But if “FORMAT” is the name of a variable, which is valid (FORTRAN has no “reserved” words), and it has subscripts, the compiler will not be sure this is not intended to be an arithmetic statement until it reaches the matching “)” and sees no “=“ sign after it!
      Amazing what they were able to do with so little computing power and math theory!

  • @juliuszkocinski7478
    @juliuszkocinski7478 ปีที่แล้ว +89

    That's maybe the best explanation why Python, Java and other canonically interpreted langs can differ in performance so much depending on the interpreter/standard/environment.

    • @igorthelight
      @igorthelight ปีที่แล้ว

      Java is compiled to bytecode than JIT'ed. JavaScript is interpreted and then JIT'ed
      Python is just interpreted. It can be JIT'ed using PyPy.

    • @pierrefley5000
      @pierrefley5000 ปีที่แล้ว +6

      @@igorthelight Almost no language is just interpreted (except for batch files or shell scripts, maybe?). Most interpreters first compile to a simplified internal format (like bytecode). It doesn't make sense to call Java "compiled to bytecode" and Python "just interpreted" when Python does the same thing.

    • @igorthelight
      @igorthelight ปีที่แล้ว

      @@pierrefley5000 Fair point!

    • @robsku1
      @robsku1 ปีที่แล้ว

      @@pierrefley5000 Well, you can say "compiled to bytecode and then interpreted", if you want to. But since bytecode is just a different, more machine readable representation of the written code, that's not so big distinction. And it's more of a translation than compilation; and it has been there from the very early 8-bit microcomputers and their BASIC - nobody is saying that they weren't "just interpreted" because they were translated into byte-code first.
      But yeah, batch and shell scripts are, AFAIK and I'm really confident about this, parsed and interpreted everytime it runs, there's no translation to byte-code involved at all. Rest of the interpreted languages, I don't know if there's a single one that doesn't have some kind of internal byte-code representation that the written code is first translated/compiled into, but if there is then it has to be a fringe case really ;) It just doesn't make sense for interpreter to parse the written representation of the code every single time that piece of code is ran.

    • @yancgc5098
      @yancgc5098 10 หลายเดือนก่อน +1

      @@robsku1 Well, there was no translation to bytecode for early 8-bit BASIC interpreters. All they did was parse the source code and perform its behavior directly.

  • @mytech6779
    @mytech6779 ปีที่แล้ว +398

    The downside of JIT is the final behavior is not locked down and determinant in a way that can be practically tested with certainty, so runtime behavior can't be effectively verified for critical applications.
    eg Some fool at Boeing tried to use Java for a critical embedded control, worked fine most of the time but about 1% of the tests would have failed activation because the JVM was busy doing stuff when the signal came in.
    If a game drops a frame here or there or misdraws a few pixels, it's a minor annoyance if it is even noticed, If a jet doesn't retract its landing gear or an automated defibrillator(medical) glitches, it will be noticed and more than an annoyance.

    • @mage1over137
      @mage1over137 ปีที่แล้ว +60

      This is also why you don't use a hammer as a scalpel.

    • @lucbloom
      @lucbloom ปีที่แล้ว +1

      Oof

    • @mytech6779
      @mytech6779 ปีที่แล้ว +35

      @@mage1over137 First one must know the difference between the hammer and scalple.

    • @daruiraikage
      @daruiraikage ปีที่แล้ว +4

      I dont get it, so all java programs that run on JIT compilation are prone to failure and unexpected behaviour at runtime for 1%(or less) cases?

    • @user-di5wj8is7i
      @user-di5wj8is7i ปีที่แล้ว +82

      @@daruiraikage When the Java Virtual Machine de-fragments and frees memory (garbage collection) it will sometimes pause the program while it does so. Garbage Collection is inevitable in a long-running program. You can delay it giving it a larger memory buffer. However, giving it a larger buffer will make the pauses longer when it finally decides to do so. Most of the time it's difficult to effectively control when garbage collection happens. If you somehow disable garbage collection during execution of a critical section, but you use more memory than is available in the buffer, your program will crash.
      Essentially, languages with Garbage Collection (most JIT languages) are il-suited for real-time applications. i.e. Java, JavaScript, Python, C#, Golang, Ruby, Haskell, Php, Perl should not be used in real-time or critical environments.

  • @animatrix1851
    @animatrix1851 ปีที่แล้ว +123

    It would be great if you could talk about the "scary how clever they've gotten", it's exciting to hear a statement like that so would love to know more

    • @MLeoDaalder
      @MLeoDaalder ปีที่แล้ว +7

      I'm pretty sure they already have videos about, for example, the Spectre and Meltdown vulnerabilities in CPUs.

  • @gmpassos
    @gmpassos ปีที่แล้ว +54

    The compiler is removing the loop because the “a” variable is not being used after the loop! If you print the “a” variable at the end the “time” will be different.

    • @the_real_glabnurb
      @the_real_glabnurb ปีที่แล้ว +9

      Yes, additionally he just adds a constant, 5, up to 1e7 times, which is just a multiplication that fits perfectly fine in a single 32-bit number multiplication instruction, so the speed should also be constant.

    • @Ghost-Raccoon
      @Ghost-Raccoon ปีที่แล้ว +12

      Yes and I think he also implied that with saying the loop just got compiled away. But it could have been pointed out better.

    • @AliceErishech
      @AliceErishech ปีที่แล้ว +3

      I'm not sure how Pypy works since I don't use Python but it should still be possible to more or less optimize the loop away. The function call inside the loop can be abstracted to a constant number instead (since it runs the same function with the same inputs) and from there you can just multiply that constant by the number of loops and add that total value all at once. Of course, this isn't to say that Pypy would actually do this, just that it's possible. I don't know whether it actually would in this case or not.

    • @xwaltranx
      @xwaltranx ปีที่แล้ว +3

      assuming a was used and loop haven't been removed, would the jit optimize the f for the int values or optimize it just for f(2,3) (since it is only used for that particular values) ? assuming jit optimizing methods for particular values how does it know when to stop optimizing? does it a have storage limit for "optimized" methods etc. it feels like it might take away the performance if there are too many variations

    • @prdoyle
      @prdoyle ปีที่แล้ว +3

      Absolutely. Benchmarking JIT compilers is tricky.

  • @XLucas600
    @XLucas600 ปีที่แล้ว +44

    The comparison between Just In Time compilation and out of order pipelines is brilliant. Never though about it before.

    • @ralf391
      @ralf391 ปีที่แล้ว +6

      CPU cores are hardware interpreters for their ISA. Their µcode looks very different from their machine code. Been like this since system/360.

    • @hayden.A0
      @hayden.A0 10 หลายเดือนก่อน

      @@ralf391 That's a genuinely interesting way of putting it! Makes it much easier to see how out-of-order cores can be considered JIT compilers in a way

  • @Roxor128
    @Roxor128 ปีที่แล้ว +101

    A major use for JIT compilation: emulators. Huge performance boosts were gained by using JIT instead of straightforward interpretation for translating the machine code of the emulated system into the host's machine code. Especially important for emulating more recent systems. Of course, what constitutes "recent" depends on when the post was written. When I first heard of it being used, it was the Playstation 2 and Gamecube that it was being applied to (possibly the Wii as well), and there wasn't any way of emulating the PS3, or Xbox360 yet. With the last couple of crops of consoles using x86 processors, emulating one might have more in common with running Windows in a virtual machine instead.

    • @octopirate-bak
      @octopirate-bak ปีที่แล้ว +12

      gamecube and wii have the same processor architecture and so use the same emulator (dolphin)

    • @Roxor128
      @Roxor128 ปีที่แล้ว +1

      @@octopirate-bak Yeah, I knew Dolphin can do both. I just wasn't sure when it had Wii emulation capability added.

    • @hosono3918
      @hosono3918 ปีที่แล้ว +3

      Switch already has great emulators, running at faster than the original hardware on many games. It's great.

    • @flTobi
      @flTobi ปีที่แล้ว

      @@hosono3918 well, they both use JIT compilers too for ARM -> x86 translation

    • @igorthelight
      @igorthelight ปีที่แล้ว +2

      "and there wasn't any way of emulating the PS3, or Xbox360 yet" - so what about RPCS3 and Xenia?

  • @snickers10m
    @snickers10m ปีที่แล้ว +153

    Hey Brady, please ask Dr. Tratt to change his computer password; the video shows him typing it in at 3:18. While the risk is low (camera angle is pretty flat), a dedicated attacker might be able to figure out his password from his finger movements and use it. Better safe than sorry.
    Excellent video btw!

    • @chitwansingh
      @chitwansingh ปีที่แล้ว +5

      +

    • @wich1
      @wich1 ปีที่แล้ว +16

      I was about to say... typing in a password on camera, bold move. Hope he changed it before the video to something specifically for the purpose of the video

    • @rikwisselink-bijker
      @rikwisselink-bijker ปีที่แล้ว +62

      He commented in another thread he rotated his password (and that doing so is a habit after interviews like this).

    • @Howtheheckarehandleswit
      @Howtheheckarehandleswit ปีที่แล้ว +1

      +

    • @wlockuz4467
      @wlockuz4467 ปีที่แล้ว

      Came here to say the same!

  • @wlockuz4467
    @wlockuz4467 ปีที่แล้ว +51

    We need a video of Dr. Tratt explaining how scary the processors have got as soon as possible!

    • @klobiforpresident2254
      @klobiforpresident2254 ปีที่แล้ว +1

      Somewhere in my office sits Intel's five thousand page SDM. I may not know if a processor is scary but this, this is.

  • @bazoo513
    @bazoo513 ปีที่แล้ว +32

    A decent optimizing compiler will detect that that loop has no side effects (including output) and that the result of summation isn't used anywhere, and will simply throw the whole thing out. To prevent that outputting the final sum would suffice.

    • @TheHuesSciTech
      @TheHuesSciTech ปีที่แล้ว +2

      The example in the video was a silly toy example. The idea is that JIT works, even in situations where static optimization would have no idea what's happening.

    • @bazoo513
      @bazoo513 ปีที่แล้ว

      @@TheHuesSciTech Yes, yes, I know.

  • @justsurajp
    @justsurajp ปีที่แล้ว +75

    maybe I'm being too skeptical, but the typing at @3:20 is a bit risky? They showed the keystrokes of the password in the video. somebody with enough expertise in this area could potentially figure out what his password is based on the fingers he used for the keystrokes! I can already see he used a "shift" key for an uppercase letter in there!

    • @BlueTJLP
      @BlueTJLP ปีที่แล้ว +8

      I also thought that I would have requested a black box over it just to be safe

    • @Mr.Leeroy
      @Mr.Leeroy ปีที่แล้ว +15

      Pretty sure I heard that someone was successful in decoding keyboard keystrokes by sound alone.
      So better put on you tin foil hats..

    • @ConOvS8
      @ConOvS8 ปีที่แล้ว +8

      I came to the comments to see if anyone else had picked up on this

    • @simonsandstrom7999
      @simonsandstrom7999 ปีที่แล้ว +2

      Hunter2

    • @laurencetratt9060
      @laurencetratt9060 ปีที่แล้ว +47

      I appreciate the concern, but there's no need to worry. What you can't see (since its output wasn't used) is that there was a second camera pointing at my keyboard. I rotated my password after the recording, as I tend to do in such situations anyway.

  • @jaosberg
    @jaosberg ปีที่แล้ว +9

    First video I've seen with Laurence Tratt. He's great!

  • @miquelrosetjulia7971
    @miquelrosetjulia7971 ปีที่แล้ว +24

    Maybe you want to blur the part of the video where Dr. Laurence Tratt logs in to his pc...

    • @Gvozd111
      @Gvozd111 ปีที่แล้ว

      There is no way anyone cam guess the password from that footage. Maybe length and rough positions for some keys but I don’t think it reduces the search field considerably

    • @stargazer7644
      @stargazer7644 ปีที่แล้ว

      @@Gvozd111 Would you put money on that? Before you answer, maybe you should get off your phone and watch it again in 1/4 speed on a 4K monitor. I can clearly see all the right hand letters, and I can tell what keyboard row and finger he's using for each letter on the left hand. This is slightly more difficult because he's a 4 finger typist and moves his hands along the keyboard, but it narrows the left hand characters down to just a few possibilities. And that's assuming the password isn't so simple that you don't just guess the left hand letters based solely on the known right hand ones. 😁

  • @Christian-px3zt
    @Christian-px3zt ปีที่แล้ว +1

    I looove computerphile. i work in construction but everytime i watch one of your videos I realize what i really wanna do in my life... thx you guys so much!

  • @FTropper
    @FTropper ปีที่แล้ว +31

    I always wondered why the JVM for example is not able to dump the current compilation state into a file. So next time you load the same program you don't have to start the JIT process from scratch. If you have a big webapplication in java it takes quite a long time from startup to a point where your application runs a same speed it did last time.

    • @IcyShower
      @IcyShower ปีที่แล้ว +5

      I also wondered the same thing, but in the end you can't just dump and reload pieces of compiled code. JVM initializes everything lazily and initialization order affects application correctness so every time you start your Java app, the JVM must ensure it's initialized in correct order, even if something has changed (e.g. environment variables or whatever). OTOH, if you invest enough engineering effort then you can actually reuse some compiled code if you check initialization order (and other important stuff) first, so your app reaches peak performance much quicker. Azul ReadyNow! implements that and it's a paid technology. OpenJDK authors are also working on improving JVM startup as part of Project Leyden.

    • @rideorhitchhike3347
      @rideorhitchhike3347 ปีที่แล้ว

      There are some languages out there that can do this. Check out Pharo and Glamorous Toolkit (and other Smalltalk family languages, like Self).

  • @aaron552au
    @aaron552au ปีที่แล้ว +13

    I'm fairly sure it's impossible to have a fully-static implementation of a javascript compiler that also completely implements the ECMAScript spec. This is true for most "JIT/interpreted languages"
    For example: Runtime reflection on aribtrary data types is often a key feature in JIT/interpreted languages and this is in general incompatible with static compilation.
    Another example is runtime evaluation of generated source code. A compiler could embed itself into the compiled program and invoke itself for these cases but then it's no longer statically compiled and you effectively just have a JIT compiler.

  • @sergeyrykovanov6396
    @sergeyrykovanov6396 ปีที่แล้ว +4

    Great and full of insights as always! Just gave a lecture about JIT yesterday, posted this video to students.

  • @michamaj6290
    @michamaj6290 ปีที่แล้ว +11

    Nice Framework laptop!

  • @nazifilbek
    @nazifilbek ปีที่แล้ว +12

    3:20 who will be the first to tell the password

  • @adarshsharma8039
    @adarshsharma8039 14 วันที่ผ่านมา

    Was confused about this for a long time. This video cleared most of my doubts.

  • @FinaISpartan
    @FinaISpartan ปีที่แล้ว +25

    Static compilers can have just as much info as JIT when given runtime info. This is often done using a technique called PGO and usually leads to the absolute fastest type of programs when combined with other static compilation optimizations (LTO, binary optimization [bolt], etc...)

    • @rodrigobarraza
      @rodrigobarraza ปีที่แล้ว

      Very cool!

    • @godnyx117
      @godnyx117 ปีที่แล้ว +1

      You can also use JIT in some parts in a program compiled with and AOT.

  • @peterfireflylund
    @peterfireflylund ปีที่แล้ว +31

    Tratt, I've been a fan of your writing (and some of your research) for years!
    The work you did on the (missing) warm up of JITs with the PyPy guys is downright amazing :)
    (The stuff I'm not a fan of? Mostly the style of language you chose for your Converge language -- I agree with pretty much all your goals for it, of course. De gustibus non est disputandum...)

  • @lowellthoerner1209
    @lowellthoerner1209 ปีที่แล้ว +11

    Rocking the Framework laptop ✊️⚙️🛠

  • @johnstoner2
    @johnstoner2 ปีที่แล้ว +6

    Nice framework laptop! so tempted to buy one.

    • @RunOfTheTrill
      @RunOfTheTrill ปีที่แล้ว +1

      That was the first thing I noticed lol

  • @devsnek
    @devsnek ปีที่แล้ว +24

    as someone who works on the v8 js engine in chromium I quite enjoyed this video. people often think jits are a lot scarier than they really are and education in this area is always appreciated! also shout-out for using a framework laptop :)

    • @daliborin
      @daliborin ปีที่แล้ว

      Hi, he said that javascript can be compiled, but in the past I was googling and it's not possible to make binary file out of js code. So is it possible or not? And if not, why it's not a thing?

    • @authenticallysuperficial9874
      @authenticallysuperficial9874 ปีที่แล้ว

      @@daliborin a machine only ever runs machine code. eventuality, *something* is compiled.

    • @jcfawerd
      @jcfawerd ปีที่แล้ว

      @@daliborin quickjs

    • @zilog1
      @zilog1 ปีที่แล้ว

      Snek
      Reminder: protogens go beep boop

    • @ArsenGaming
      @ArsenGaming ปีที่แล้ว +2

      @@daliborin He meant that it's possible to compile any language, or interpret any language. That means that a compiler could theoretically be created for JS, but I don't know if anyone has done it.

  • @soranuareane
    @soranuareane ปีที่แล้ว +12

    This video just explained something I've known about (and leveraged) for _many_ years now, but never truly understood, in a simple bite-sized way. Thank you!

    • @adamuk73
      @adamuk73 ปีที่แล้ว +6

      Surely a byte-sized way? I'll see myself out 😉😁

    • @josephgaviota
      @josephgaviota ปีที่แล้ว

      @@adamuk73 👍

  • @KeyYUV
    @KeyYUV ปีที่แล้ว +8

    Brave of him to type his password on camera.

  • @Synthetica9
    @Synthetica9 ปีที่แล้ว +9

    Some of this is also possible with static compilation, look up Profile Guides Optimisation (PGO)

  • @totlyepic
    @totlyepic ปีที่แล้ว +287

    While this guy obviously understands the subject well, the example they chose is quite silly. A statically compiled language like C will also optimize out such a loop; it doesn't really show off how JITs can excel so much as it shows how bad the more naive approach of interpreters is.

    • @benloud8740
      @benloud8740 ปีที่แล้ว +81

      Yeah. He demonstrated the difference between interpreted vs compiled, but not necessarily JIT. To show the advantage of a JIT he really needed to show a case where the program execution isn't known until runtime. Like a dynamic function call where a static compiler can not deduce the target so must generate a procedure call, where as a JIT can inline the target. Thats critical to make Java run fast when every method is virtual and most are very tiny, so inlining is crucial. Its the reason why static compiled Java performs badly. It needs a JIT to work well. C++ on thr other hand, with templates and virtual calls being relatively rare, much more information is known at compile time so it doesnt have the problem.

    • @justingifford4425
      @justingifford4425 ปีที่แล้ว +17

      I love this channel but wow this was a terrible way to demonstrate the advantages of JIT. All he did was demonstrate the problem with purely interpreted languages, not why JIT is useful.

    • @jameswright4732
      @jameswright4732 ปีที่แล้ว +16

      I disagree. Yes, this kind of problem is something a traditionally compiled language can optimize out. But JIT is primarily used for accelerating languages intended to be interpreted, which he demonstrates perfectly fine here. I'm not sure what else you were expecting to be shown.

    • @gloverelaxis
      @gloverelaxis ปีที่แล้ว +3

      @@benloud8740 imo his example didn't really demonstrate the difference between interpreted vs compiled, either. that was the difference between the translator making optimisations, vs making no optimisations. an interpreter can choose to implement optimisations in the way that it chooses instructions in the host language, just as well as a compiler can choose to implement optimisations in the way that it chooses instructions for the target language. the only important/qualitative difference is whether those optimisations are done statically or dynamically. you could absolutely make an interpreter that creates & remembers program-dependent optimisations ahead-of-time / statically, and then uses them upon actually evaluating the guest-language program. a compiler just has the advantage that it can target a less abstract language than the language the compiler itself is written in - but even then, compilers can't translate to CPU hardware microcode! that means that the CPU is effectively an interpreter (host lang: microcode. guest lang: instruction set) which is performing optimisations

    • @heaslyben
      @heaslyben ปีที่แล้ว +1

      I was hoping to see some coin flips to choose data types -- at dev time vs at runtime before the loop vs inside the loop.

  • @prdoyle
    @prdoyle ปีที่แล้ว +2

    Trace-based compilation has a separate lineage starting from the HP "Dynamo" compiler. That was revolutionary at the time.

  • @linuxsbc
    @linuxsbc ปีที่แล้ว +4

    I see you're using a Framework! I love it!

  • @onlyeyeno
    @onlyeyeno ปีที่แล้ว

    Thanks for another great video that masterfully balances on the gap between technical depth and superficial overview :)
    Best regards

  • @XFourty7
    @XFourty7 ปีที่แล้ว +33

    You can disable the E-Cores in bios if you wanted to have more comparable results. For the best comparison, disable all but 1 P-Core + hyperthreading and set it to a fixed frequency.

    • @Mr.Leeroy
      @Mr.Leeroy ปีที่แล้ว +2

      not to buy them at all is even easer.

    • @stefanalecu9532
      @stefanalecu9532 ปีที่แล้ว +1

      @@Mr.Leeroy you can't avoid them even on x86 now, so good luck with RISC-V

    • @simon7719
      @simon7719 ปีที่แล้ว

      Or just accept that it's more than likely the future and therefore downright relevant for a language/compiler researcher to have.

    • @XFourty7
      @XFourty7 ปีที่แล้ว +1

      @@simon7719 What I said has nothing to do with JIT, it's just an explanation of how to set up the hardware to it shows a consistent duration (Time) requirement for any testing.
      Skip to 4:44 and listen to what he says about his laptop. I'm not sure if you're familiar with the last few generations of Intel CPUs, but most of the variants of the last 2 generations have "E-Cores" (E for Efficiency) and "P-Cores" (P for Performance). These are both a power saving method for doing background tasks as well as a way to make the core count look "better" to consumers to compete with AMD's core/thread counts.

    • @Mr.Leeroy
      @Mr.Leeroy ปีที่แล้ว

      @@simon7719 "more than likely" part is debatable. Especially when CPUs with efficiency cores draw more power than previous gen and the thing that is supposed to choose between E/P cores for your load is Windows.

  • @964tractorboy
    @964tractorboy ปีที่แล้ว +1

    Great video. Another contributor I'd like to see more of.

  • @BlueTJLP
    @BlueTJLP ปีที่แล้ว +9

    The video quality is superb! It's really noticeable how clear the image is

    • @kayakMike1000
      @kayakMike1000 ปีที่แล้ว +2

      They did up their game, it looks great!

    • @frankfox4366
      @frankfox4366 ปีที่แล้ว

      Yeah. It is 4k.

  • @alexlandherr
    @alexlandherr ปีที่แล้ว

    It was nice learning a few new things about the JVM, as it’s one of my main languages.

  • @0GRE7
    @0GRE7 ปีที่แล้ว +5

    Very interesting. Would love to hear more details on how those optimizations work, i.e. why exactly makes that much difference between the two compilers. What does the optimized program look like?

  • @mmsbludhound873
    @mmsbludhound873 ปีที่แล้ว +46

    I've heard many times that JIT compilers can possibly optimize the code during runtime to make it faster than statically-linked code, and I believe that. However I've yet to see a real-world example of that happening, even in frequent loops. I'd really appreciate it if someone could point me to an example where the JIT code is indeed faster (without the linked code being artificially hampered).

    • @jensrenders4994
      @jensrenders4994 ปีที่แล้ว +26

      I have some code that fits billions of curves to n datapoints each (once per pixel in a large image). n is unknown before runtime, but it is constant once it is read in. We wrote it in C and in Python using numba as the JIT. The python version is slightly faster. The reason is that the python version knows the size of all the used arrays at compile time (which is at runtime) and the C compiler does not and cannot know this. One option to make the AOT version faster is to compile many versions, one or each possible value of n. Obviously that is quite limited.

    • @mmsbludhound873
      @mmsbludhound873 ปีที่แล้ว +3

      ​@@jensrenders4994 Thank you for the example! Does the source happen to be available so I can play around with it?

    • @jensrenders4994
      @jensrenders4994 ปีที่แล้ว +8

      @@mmsbludhound873 This is part of unpublished research so cannot share it immediately. It is supposed to be published at some point though.

    • @mmsbludhound873
      @mmsbludhound873 ปีที่แล้ว

      @@jensrenders4994 Ahh gotcha.

    • @SkyyySi
      @SkyyySi ปีที่แล้ว +1

      Try searching for some math scripts and just run them with both an interpreter and a JIT compiler

  •  ปีที่แล้ว +2

    finally someone uses one of the two editors and it's even the one, that can be used without surgical alteration of the editing hands...I think that's a first for computerphile.

    • @ragreenburg
      @ragreenburg ปีที่แล้ว

      Emacs has many packages that have modal editing. No need for that surgery anymore! Just install evil, Meow, boon, etc.

  • @bitcrawl
    @bitcrawl ปีที่แล้ว +6

    This channel is an unknown treasure 🙌

    • @bbuggediffy
      @bbuggediffy ปีที่แล้ว +13

      No it is not. It has 2.2 million subscribers.

    • @Yassinius
      @Yassinius ปีที่แล้ว +1

      I'd say they’re pretty well established

  • @qazplm1689
    @qazplm1689 10 หลายเดือนก่อน

    tnx Dr Laurence Tratt. you have answers 10 questions of mine I was searching in a 10 mins video

  • @reintsh
    @reintsh ปีที่แล้ว +1

    Nice that you indicated what I already said long ago: JIT actually is JTL.

  • @petrutarabuta5617
    @petrutarabuta5617 ปีที่แล้ว +6

    Super explanation. Thanks!

    • @madeline-onassis
      @madeline-onassis ปีที่แล้ว

      yes - absolutely fascinating and very well explained!!

  • @ELECTROMIST
    @ELECTROMIST 3 หลายเดือนก่อน

    i really love this video!

  • @Originalimoc
    @Originalimoc ปีที่แล้ว +5

    The framework laptop. Nice.

  • @yevgeniygorbachev5152
    @yevgeniygorbachev5152 ปีที่แล้ว +6

    The "dynamic analysis" can't possibly enumerate all valid code paths, so how can JIT make sure its optimizations are non-breaking?

    • @capability-snob
      @capability-snob ปีที่แล้ว +1

      Tratt's student cfbolz once answered this question "we add lots of ifs and prey". If you're fortunate, all of those ifs can be hoisted outside of the hot loop, but there are obviously times they must remain. There are a lot of approaches to reducing the number of things you need to check, too.

    • @goughy000
      @goughy000 ปีที่แล้ว +3

      Using the same example from the video. They have a function "f" in Python, whilst being just 1 line of code we see it doing 2 very different things (addition of numbers, or string concatenation)
      At runtime, there's a big loop constantly calling the function f with just numbers, we know it's always numbers because the numbers were hardcoded.
      The JIT can spot this, create and compile a function at runtime in machine code that just deals with numbers (let's call this new function f_numbers) and replaces the code in the loop to call this new f_numbers function.
      Elsewhere the code is left as-is using the original interpreted function, f. This ensures that other code paths that aren't guaranteed to be using numbers continue to work
      It's not the best example because this could have been spotted without running the program but hopefully that makes sense.
      The main thing to realise is that when JIT compiles a method, it doesn't completely replace the original, but creates an optimised one for particular use cases alongside it, and the new compiled one will only be used where it knows it's safe to do so

    • @circuit10
      @circuit10 ปีที่แล้ว +3

      It will add "guards" that check that all the assumptions are still valid, and if they are it continues to the optimised fast path, otherwise it will jump to a "deoptimise" function that will invalidate the compiled code and fall back to running it in an interpreter
      So this way it can assume things that are probably true based on the behaviour it has seen so far, but not break if that changes, which a static compiler can't do because it can't see how the program will behave at runtime

  • @AJ-et3vf
    @AJ-et3vf ปีที่แล้ว

    Great video. Thank you

  • @mldy1
    @mldy1 ปีที่แล้ว +2

    python 3.11 actually uses a similar strategy to a JIT by doing an optimized lookup for the addition operator based on what types that function is frequently called with.

  • @Djellowman
    @Djellowman ปีที่แล้ว

    Very good video!

  • @mage1over137
    @mage1over137 ปีที่แล้ว +14

    CPython does have a JIT compiler, Numba uses one. Sure it's a bit limited, but you can get 13x speed on math heavy code.

    • @QnJhbQ
      @QnJhbQ ปีที่แล้ว +9

      gotta crunch the numba's innit?

    • @benjaminv3748
      @benjaminv3748 ปีที่แล้ว

      I used Numba for a big data analysis project in particle physics, very math heavy. With the near-trivial extension to distribute the work (built-in feature prange() ), I managed to get >100x speedup and >10x faster than existing C++-code (though to be fair, it was not terribly optimized).
      Point is, it maintained readability perfectly and cut execution time from several minutes to seconds!

  • @jp10a
    @jp10a 11 หลายเดือนก่อน

    More videos with this guy please :)

  • @sanderbos4243
    @sanderbos4243 ปีที่แล้ว

    I finally get it now, yay!

  • @laughingvampire7555
    @laughingvampire7555 2 หลายเดือนก่อน

    *a compiler takes a file and produces another file.* the language in the input file is called source code and the language in the output file is called target. the source and target can be the same language. some people call this a specific name like compiler-compiler or transpiler but they don't need to because is just a compiler.
    *an interpreter takes a file and produces actions described in the file.* the JIT can compile to a bytecode or to assembly in both cases produces faster execution however if the source programming language isn't designed with zero cost features then the speed the JIT produces won't be same as a zero cost programming language. There are very small number of usecases like inside a loop in which a JIT can produce code faster than a zero cost language.

  • @FreddyRangel85
    @FreddyRangel85 ปีที่แล้ว

    I very much appreciate the VIM here with Airline/Powerline

  • @Nnyerix
    @Nnyerix ปีที่แล้ว +7

    This video is LeJIT!

  • @christopherguy1217
    @christopherguy1217 ปีที่แล้ว +48

    In C# and .Net languages in general, the JIT compiler compiles the IL code once before running. So. My understanding is that the IL code can be run cross platform as the compilation happens on the target platform.
    What you described seems to be a runtime optimizer or something. In any case it's a great time to be developing software.

    • @XtroTheArctic
      @XtroTheArctic ปีที่แล้ว +4

      I came to write a similar comment about C# and .NET. Also, it's very interesting to hear the word "compiler" for an interpreted language such as javascript. I don't know why he uses that word.

    • @monad_tcp
      @monad_tcp ปีที่แล้ว +8

      @@XtroTheArctic "interpreted language such as javascript"
      watch the video again.
      there's NO SUCH a thing as "interpreted language".
      Languages are statically typed or dynamically typed. They are not compiled/interpreted, that's environment.

    • @monad_tcp
      @monad_tcp ปีที่แล้ว +21

      No, what he described is a JIT compiler, that's how they work actually.
      JIT compilers are runtime optimizers.
      Your misconception is that it is not the JIT compiler that produce the IL on the dotnet machine, its the C# compiler itself that does that.
      The JIT compiler only works with IL.
      Also, the System.Expression has a lambda compiler that's separated and also is able to make IL code. (so is the mono.cecil library)
      The F# compiler also uses an entire different mechanism to produce optimized IL for functional code. The F# compiler is written in F#.
      The JIT compiler doesn't produce IL, it consumes IL and produces machine code, at RUNTIME.
      But it might also do AOT if you use Roslyn compiler, Roslyn is an hybrid compiler, it can do both, JIT/AOT and PGO, which is profile guided optimization.
      You can have 98% of the performance of C++ for 25% of the cost.
      I'm in the middle of porting everything I have in C++ to dotnet7.
      The performance of the new Dotnet7 (with stack types and spans) is so amazing that it beats the Java Azul compiler, the best JIT for years. But not anymore.
      Exciting days for the dotnet community.

    • @XtroTheArctic
      @XtroTheArctic ปีที่แล้ว +6

      @@monad_tcp I kindly suggest you to research this to learn more, if you want. Have a nice day.

    • @monad_tcp
      @monad_tcp ปีที่แล้ว +11

      @@XtroTheArctic No, I don't need to research this, I work on this research field.
      I have a MSc in compiler development.
      Your teacher is wrong.
      There's no compiler/interpreter languages.
      There never was. The first Fortran was interpreted, then a compiler was made. The very first compiler.
      This is a simplification some teachers used to teach kids.

  • @Syntax753
    @Syntax753 ปีที่แล้ว

    Brilliant!

  • @laszlofabian
    @laszlofabian ปีที่แล้ว +4

    In the modern C toolchains, there is the first part, compiling into the object file and there happens the first layer of optimization, the second level of "static" optimization when all object files are put into one "bucket", until this point i fully agree that the JIT can be more efficient. However, there is also the LTO (Link Time Optimization), this also has multiple stages, and this will look from the linker perspective and you can consider it as "dynamic" optimization. Off course you can seriously hinder it with improper pointer handling, especially function pointers and recastings, as those will break the path of data and function accesses and in this area JIT might be more efficient than the modern C compiled binary. My background is more C focused, happy to learn more about the higher level solutions like JIT.

    • @Hamachingo
      @Hamachingo ปีที่แล้ว +4

      The whole idea and power of JIT compilation is that it enables code written by plebs without exactly that knowledge of the inner working to execute much faster. But it also scales so a very skilled programmer can spend more of their time on the interesting stuff instead of optimising for performance everywhere.

    • @TheHuesSciTech
      @TheHuesSciTech ปีที่แล้ว +1

      Even LTO is completely unaware what data or parameters the program will be run with. PGO (profile-guided optimization) is what you should be touting as the C competitor to JIT; but the problem is that PGO requires you to generate profiles, which is a manual step the developer has to go through. Whereas, as @Hamachingo points out, JIT can see things that PGO can see but LTO can't, but it also just works "for free".

    • @laszlofabian
      @laszlofabian ปีที่แล้ว

      @@TheHuesSciTech Thanks for this, i was not aware of the PGO, i learned something today.

  • @NorthWay_no
    @NorthWay_no ปีที่แล้ว +2

    How about a discussion on JITs versus compiler code instrumentation and if there are still advantages that JITs hold that are out of reach for what the compiler can get from repeated runtime statistics?

    • @TheHuesSciTech
      @TheHuesSciTech ปีที่แล้ว

      JIT just happens "for free", whereas PGO (which is what I assume you're referring to) requires manual effort from the developer. I feel like that's the biggest difference, and that's coming from a PGO fanboy like me!

  • @belst_
    @belst_ ปีที่แล้ว +4

    PGO is basically JIT but for static compilation :)

  • @billykotsos4642
    @billykotsos4642 ปีที่แล้ว

    really cool !

  • @laughingvampire7555
    @laughingvampire7555 2 หลายเดือนก่อน

    JVM optimizations come from the Strongtalk system which was a strong typed Smalltalk implementation because Self's optimizations weren't enough.

  • @davidfrischknecht8261
    @davidfrischknecht8261 ปีที่แล้ว +6

    I believe .NET also has a JIT compiler.

  • @zilog1
    @zilog1 ปีที่แล้ว +5

    What i have notices about me playing minecraft, is that after i have started playing for a while, it does seem to get a bit faster after ive played for about an hour or so. I wonder if this is what im seeing in action.

    • @TheHuesSciTech
      @TheHuesSciTech ปีที่แล้ว +5

      My instinct is that it seems weird that it would take a whole hour before it took effect; I'd expect the JIT gains to be observed much sooner than that. But I have no specific evidence to give to you on that.

    • @DFPercush
      @DFPercush ปีที่แล้ว +6

      Yeah I don't think it would take Java an hour to figure out how to run its code. It's probably just that you've got all the chunks around you loaded, and you're getting more cache hits on the entities that have to update. I found that garbage collection would still make the game stutter every few seconds though.

  • @jell_pl
    @jell_pl ปีที่แล้ว +5

    "big team in most cases" - funny to hear from someone showing pypy (where work on jit was done in quite small team) as an example :)
    same with amazing luajit2 which afair was done mostly by a single person?

    • @igorthelight
      @igorthelight ปีที่แล้ว

      But how much time those small team needed to create their first working version? ;-)

  • @peterninefingers
    @peterninefingers ปีที่แล้ว +2

    Please cover Ahead of Time compilation.

  • @jamesrobinson6330
    @jamesrobinson6330 ปีที่แล้ว +8

    Nice Framework laptop

  • @ajs41
    @ajs41 ปีที่แล้ว +1

    Just discovered this channel, thanks to the Tom Scott association.

  • @TheGleb123
    @TheGleb123 ปีที่แล้ว +14

    Adding a constant to a variable a constant amount of times is a very poor example of JIT optimization. For any proper compiler that's just a constant, no computation required at run time. Is there any proof that JIT-compiler can beat a proper optimizing compiler in terms of speed of the program?

    • @stephenkamenar
      @stephenkamenar ปีที่แล้ว +3

      it's obvious that jit can beat a compiler for dynamic languages like python.
      imagine your variable are from json data over the network. the static code will have no clue they're only ever going to be ints. but the jit can know that

    • @TheGleb123
      @TheGleb123 ปีที่แล้ว +2

      @@stephenkamenar I'm glad that you pointed out that it's only valid for dynamic languages. So in a sense JIT doesn't make a program faster, it just makes it less slow compared to a statically typed language.

    • @stephenkamenar
      @stephenkamenar ปีที่แล้ว +1

      @@TheGleb123 it can probably be faster than static languages too. idk i'm no expert. it's just very obvious for dynamic languages.

    • @classawarrior
      @classawarrior ปีที่แล้ว +1

      @@TheGleb123 That's not necessarily true.
      One example where a JIT can speed up a statically typed language is where it can make more informed decisions (guided by runtime data collection) about whether to inline a function or not, or whether to optimize a function for size or based on some other metric.
      An ahead-of-time compiler will typically just use some heuristics or guesswork to make such decisions. Because which is faster really depends on how often things are called, and the intricacies of CPU and memory caching behaviour etc. Measuring at runtime is the only real way to know which decision results in better performance in practice.

  • @EebstertheGreat
    @EebstertheGreat ปีที่แล้ว

    Are there actual static compilers for the whole of javascript (not just a subset)? That compile it down to a binary?

  • @yash1152
    @yash1152 ปีที่แล้ว

    3:44 thanks a LOT for increasing font size - it became watchable at 240p on a smartphone display :)

  • @cromefire_
    @cromefire_ 13 วันที่ผ่านมา

    Case and point that languages can be both compiled and interpreted: Java native images. There the graalvm native image compiler takes java bytecode (so Java or Kotlin or something else compiled for the JVM) and then compiles it into a static binary, which is then much smaller, faster to start up, but (especially without PGO) it can be quite a bit slower.

  • @craig7m
    @craig7m ปีที่แล้ว +3

    I would agree that the code isn't the best. In the example, will the optimizer notice that the a(variable) is not used after the loop and remove the variable and loop? I have seen in C, C++ and C#, the optimizer sometime removes "wait" loops. Will the results be the same if line 10 "print (a)" is added?

    • @cobaltno51
      @cobaltno51 ปีที่แล้ว

      ...but that is the whole point of optimization? Btw, I think he mentioned this happening, but I admit, that he could have been clearer.

    • @classawarrior
      @classawarrior ปีที่แล้ว

      @@cobaltno51 I think they were hoping for an example where the optimization was based on analysing the runtime behaviour of the program - i.e. the kind of optimization only a JIT would perform.

  • @isanrodrigueztrimino6701
    @isanrodrigueztrimino6701 ปีที่แล้ว +3

    is he using a framework laptop?

    • @jcfawerd
      @jcfawerd ปีที่แล้ว +2

      He is

  • @barneylaurance1865
    @barneylaurance1865 ปีที่แล้ว

    PHP got a JIT a couple of years ago. I'm not sure if it was worth it though, lots of people say it might not be. Part of the problem is that a typical PHP program just starts up, takes a web request, gets some info out of a database and generates a response, then shuts down. So there isn't much time for the JIT to optimize it.

  • @sophiamarchildon3998
    @sophiamarchildon3998 ปีที่แล้ว

    Another advantage of JIT over statically pre-compiled files that are then distributed (apps, games, etc.) is that the optimization can be specifically tailored to exactly your machine. The exact CPU features, cache sizes, memory size/bandwidth/latency, etc.
    Statically pre-compiled on the other end must be compatible with a lot of different systems and configurations (some machines are 32 bits, some 64 bits, some have SSEx extension, some don't, etc.) This makes the programs both bigger and slower. There are detection, tricks, and such available to mitigate this problem but not eliminate it. JIT rules in that aspect.

  • @animanaut
    @animanaut ปีที่แล้ว +4

    the loop in this example is pretty much static and only executed once per run. does this mean the jit is doing some optimizations before the first run?

    • @syntro6607
      @syntro6607 ปีที่แล้ว +2

      If I understand correctly, the JIT is doing optimizations within the loop (as the loop is being iterated through)

    • @geolykt
      @geolykt ปีที่แล้ว +1

      ​@@syntro6607 This however is not something only JIT can do, an AOT-compiler would be to do that just as well. Perhaps JIT even optimizes it before the loop is executed, although that is unlikely given that such optimizing is resource-intensive and should rather be done while the method is being executed

    • @user-di5wj8is7i
      @user-di5wj8is7i ปีที่แล้ว +1

      @@syntro6607 With java, there is a command-line parameter -XX:CompileThreshold
      "By default, in the server JVM, the JIT compiler performs 10,000 interpreted method invocations to gather information for efficient compilation. For the client JVM, the default setting is 1,500 invocations."

  • @sebastianrasor
    @sebastianrasor ปีที่แล้ว +1

    Love to see a Framework laptop

  • @martixy2
    @martixy2 ปีที่แล้ว

    Oh, man, guy is dropping little hints to interesting bits and then leaves us hanging.

  • @timonburkard3481
    @timonburkard3481 ปีที่แล้ว +4

    Aren't there reentrancy concerns with JIT? - I.e., does JIT have to be treated like self-modifing code?

    • @Salzui
      @Salzui ปีที่แล้ว +2

      I think they use one of many possible workarounds like checking every so often if there is a new compiled version of a function available and only then replacing the function pointer.
      Remember that JIT is not just a theory, its in use for a long time, like ~25 Years of Java for example.

  • @Martmists
    @Martmists ปีที่แล้ว +1

    The most insane form of optimization I've seen in any language is APL, if anyone's curious just search for "Dyalog 18: The interpretive advantage". They dive into how they can shave off nanoseconds just based on how they optimize, it's crazy stuff.

  • @7days280
    @7days280 ปีที่แล้ว

    Wow, thanks

  • @zilog1
    @zilog1 ปีที่แล้ว

    im glad more people are using a framework laptop nice!

  • @aoi_mizma
    @aoi_mizma ปีที่แล้ว +6

    I wonder if it is "realistically" possible to make some of the dynamically typed languages as compiled machine code without just resorting to intermediate representations + runtime (like how Python does?). Would it not create way too many permutations of the code if the data structure is complicated and could hold any number of possible types associated to it?
    If things are statically typed, I think being interpreted or compiled is just a matter of design decisions (both can be a feature of the language), but if some program can take in any type of data and permutate the actual operation during runtime depending on the input, is there a point at which it really becomes non-beneficial to completely compile down to machine code?

    • @Howtheheckarehandleswit
      @Howtheheckarehandleswit ปีที่แล้ว +6

      It is absolutely the case that it can be quite detrimental to performance to have a function that could have one of many different interpretations, with no way to know which one to use until it is called. However, static compilation generally still will improve performance, since a static compiler will be able to find a way to pack all the possible implementations of that function into a dispatch table, at which point each of those implementations would be able to be optimized as normal.

    • @aoi_mizma
      @aoi_mizma ปีที่แล้ว +1

      I wander if someone has done some study on how large the the dispatch table will become to accomodate all permutations of a dynamically typed language (of course it will change depending on how the program is written etc.).

    • @Howtheheckarehandleswit
      @Howtheheckarehandleswit ปีที่แล้ว +1

      @@aoi_mizma I imagine it would just be the total number of combinations of possible input types

    • @ashwinalagiri-rajan1180
      @ashwinalagiri-rajan1180 ปีที่แล้ว

      @@Howtheheckarehandleswit or we could analyze the parts where they're being used and which operators/functions they're used with and massively reduce the number of permutations

    • @Howtheheckarehandleswit
      @Howtheheckarehandleswit ปีที่แล้ว

      @@ashwinalagiri-rajan1180 This is a hypothetical compiler for a dynamically typed language. It’s not possible to see all of the types that are actually used at compile time.

  • @sorvex9
    @sorvex9 ปีที่แล้ว

    Finally understand JIT tracing

  • @leekmuynuts139
    @leekmuynuts139 ปีที่แล้ว

    King Terry's compiler just in time and ahead of time rip terry davis

  • @ethan7930
    @ethan7930 ปีที่แล้ว +1

    If you had a non deterministic function, but most often returns a value, say
    def func():
    if random.random() > 0.999:
    return 4
    return 5
    Would the JIT optimize the randomness away?

    • @satannstuff
      @satannstuff ปีที่แล้ว

      That's pseudo-random in most implementations, so it theoretically could do that and get the correct result but it probably won't.

  • @jcfawerd
    @jcfawerd ปีที่แล้ว +1

    Oh, framework laptop, a man of taste I see.

  • @revengerwizard
    @revengerwizard ปีที่แล้ว +7

    I understand citing as examples for JIT compilation Java and Javascript engines, but I think a bit more visibility and research should also have LuaJIT, it's pretty astouning the progress which has been made in that project.

  • @stan-15
    @stan-15 ปีที่แล้ว

    Bruv. Stop reading my mind, bruv. Was just searching this up not long ago to learn more and u come out with a vid on it?

  • @kayakMike1000
    @kayakMike1000 ปีที่แล้ว +2

    You can unroll loops and exploit better branch prediction behavior.

  • @valadimirX
    @valadimirX ปีที่แล้ว

    Thanks

  • @ewerybody
    @ewerybody 26 วันที่ผ่านมา

    Veery interesting👍 thanks!
    I would have liked to see what's happening under the hood a little. So, what it compiles to compared to without the jit maybe

  • @abdelhakimakodadi3073
    @abdelhakimakodadi3073 ปีที่แล้ว +14

    Couldn't static compilers do the same thing by running the program one or multiple times after the first compilation? If that's not possible, what prevents them from doing so? If this is possible, could it eliminate the worm up problem?

    • @charismaticaazim
      @charismaticaazim ปีที่แล้ว

      I don't c a technical feasibility challenge to recompile machine code (for compiled languages) @ run time, except for the overhead in performance.
      PS:
      Since static compilers (C, C++) convert it to machine code before execution they lose out on the benefit of JIT. This is preferable for low powered embedded systems, where power utilisation is imp.
      However, Java is an example of a language that converts to bytecode (intermediate representation) & has a JIT compiler to optimize the code @ run time.

    • @mytech6779
      @mytech6779 ปีที่แล้ว +9

      The advantage of JIT(not well demonstarted in the video) is that the runtime input is not known until runtime. Different types of data inputs or outputs each time the program is used.
      The downside of JIT is the final behavior is not verifiable for critical applications.

    • @JamesLDCarragher
      @JamesLDCarragher ปีที่แล้ว +2

      Static compilers don't know the input the program will take or even whether the program will ever terminate. But you could let the compiler observe some human guided runs and gain further knowledge about the program. This knowledge, however, is nothing but assumptions and might be wrong in later, actual runs. The compiler could perform some optimizations based on these assumptions, though, and may be able to make the compiled program faster.

    • @user-lt9oc8vf9y
      @user-lt9oc8vf9y ปีที่แล้ว +5

      That exists and as Daniel pointed out it's called profile guided optimization or pgo. The main drawbacks are overfitting and slow compilation times.
      casual static compilation makes assumptions about your code which got hard coded by some compiler engineers.
      Those assumptions might very well be wrong for your use case but how do you know the stuff that pgo just measured is representative of future executions?
      Well, that's kind of hard since you don't really know what pgo measured exactly.

    • @lucbloom
      @lucbloom ปีที่แล้ว +4

      @@capturedflame this.
      C/C++ needs more design beforehand, too.
      Javascript and Python are perfect for free-flow adhoc dev.
      This reflects in the way you optimize it. “Just do it for me” versus “show me what I should focus on to improve.

  • @chris2746
    @chris2746 ปีที่แล้ว +2

    I've heard of "Just in time debuggers" too, is that the same basic idea?

    • @TheHuesSciTech
      @TheHuesSciTech ปีที่แล้ว +1

      No. "Just-In-Time debugging is a feature that launches the Visual Studio debugger automatically when a program, running outside Visual Studio, encounters a fatal error. Just-In-Time debugging allows you to examine the error before the application is terminated by the operating system." That has nothing to do with having several goes at compiling code based on observed usage patterns, which was what was talked about in this video.

  • @tulsatrash
    @tulsatrash ปีที่แล้ว

    Cool!

  • @MrBlaq
    @MrBlaq ปีที่แล้ว

    Great explanation!

  • @CricketThomas
    @CricketThomas ปีที่แล้ว +6

    Why didn’t you use c# as the example where you could have seen the compiled intermediate language? That would’ve made a great example.

    • @TTaiiLs
      @TTaiiLs ปีที่แล้ว

      Cant most languages compile to LLVM tho?

    • @NathanHedglin
      @NathanHedglin ปีที่แล้ว

      @@TTaiiLs C# doesn't compile to LLVM. Like Java it compiles to its own bytecode that is then JIT at runtime.

  • @DanteWolfwood
    @DanteWolfwood ปีที่แล้ว +5

    When has a JIT compiled program ever been faster than a statically compiled program from something like C?

    • @niks660097
      @niks660097 ปีที่แล้ว +1

      never, maybe if someone wrote bad code in C in better code in other language, if you are actually using power of C with full pointers and cache control, doesn't matter the magic nothing can beat C..

    • @hardlyb
      @hardlyb ปีที่แล้ว +1

      It can be faster, by putting heavily used code together so it can all reside in the icache. And, as in the toy example in the video, for languages like Python with generic functions, it can decide to have the JIT just compile to specialized versions (a static compiler can, of course, do the same thing, but if you have binary functions like + with N cases, you wind up with N-squared specializations, which can take a lot of room).

    • @DanteWolfwood
      @DanteWolfwood ปีที่แล้ว

      @@hardlyb I'm not trying to be annoying but my question wasn't can. I'd like to hear of a real world application and not just theory.

    • @user-xl5kd6il6c
      @user-xl5kd6il6c ปีที่แล้ว

      @@DanteWolfwood It's not just theory. It's simply that there are optimizations that are always possible in C, but that doesn't mean any C developer will do them.
      It's like trying to say that assembly is always faster than C if done properly. It's true, but it would require you to always do optimal code for it.

    • @DanteWolfwood
      @DanteWolfwood ปีที่แล้ว

      @@user-xl5kd6il6c This still doesn't answer my question.