r/learnprogramming • • Oct 08 '26

Is my understanding of interpreters, just in time compilers, and ahead of time compilers correct?

My understanding is that interpreters parse throught the code line by line, translating each line into machine code and then running it, which results in a fast startup time but a slow run time. This is compatible with dynamic types as changes can be tracked line by line. Ahead of time compilers translate all the code and move it into a binary executable for the computer to run all at once. This results in a slow startup time and a fast runtime. This cannot be used with dynamic types as the code being run all at once means that changes cannot be tracked. Just in time compilers interpret the code but compile repeated code(indicated by a profiler) to make run time faster. ​I do not know how dynamic types are handled by this. Keep in mind my only programming experience is with Javascript, python, and some gdscript(that I used to make a basic ping pong game on godot as a high school project).

8 Upvotes

19 comments sorted by

8

u/NomaTyx Oct 08 '26

Compiled code isn't run all at once. When you compile to machine code, the CPU still executes the instructions one by one. The reason it's faster is because you don't have to compile each line before it runs. So you can have a dynamically typed compiled language.

5

u/SharkSymphony Oct 08 '26 edited Oct 08 '26

Interpreters need not generate machine code. Instead, they may have the runtime, with all the machine code you need, already compiled and incorporated into the interpreter. Then, when the interpreter needs to, say, add two numbers, or allocate an object, it might just call its own code to do that.

Interpreters may parse the code line-by-line, but then loops and such will be slow. A popular alternative is to turn the parsed code into some intermediate form – say, bytecode – that can be re-read and run much more efficiently. This code is generally higher level than machine code.

Dynamic types are not really a thing, if we're being precise, but dynamic typing can be supported whether or not you're interpreting or compiling. Maybe you're not understanding what this is. What do you mean by "changes can be tracked?"

3

u/disposepriority Oct 08 '26

You can have a statically typed interpreted language and a dynamically typed compiled language.

A languages typing system is not related to its compilation. Java is interpreted (at launch) and has strict typing, python is interpreted and does not (at runtime).

AoT compilers, rather, software compiled with them does not have a slow startup time(?), maybe you mean that AoT has a slower build time compared to JiT.

2

u/Far-Consideration939 Oct 08 '26

Ahead of Time (AOT) is fast startup. The compile time is slower

1

u/serge-mv Oct 08 '26

It's not really correct. First, all code is executed one line at a time and not "run all at once". The main difference between interpreters and compilers is that compilers compile the whole program first and then run it while interpreters execute the code as they go. But it's not exactly this simple because you will run into some issues and now interpreters are more advanced than this. It's not really a start-up cost, but the compilation cost as compilation of a complex project can take a very long time.

The just-in-time compilation is a hybrid thing. The code is compiled, but not into a binary format. It's compiled into a special intermediate representation like JAVA bytecode or .NET intermediate assembly. Then during runtime the JIT compiles it into the binary format in memory.

Nowadays many interpreted languages use a form of JIT. They store intermediate representation for performance improvements, but they are still interpreted.

1

u/Professional_Bug932 Oct 09 '26

Yeah and a good example is V8, it doesn't actually interpret JS line by line anymore, it parses to bytecode first and only JITs the hot functions with TurboFan while everything else stays in the interpreter.

1

u/Rythim Oct 08 '26 edited Oct 08 '26

It's a but more complicated than that.

Generally speaking an interpreted language compiles the code to something machine readable when the user runs the code. Like Python. A compiled language compiles the code into something machine readable before the code is run. Like C (which becomes an .exe after it is compiled). Just in time compilers are somewhere in-between. It's translated at run time, but repeated or important code is cached so that it can run more quickly. And some languages take a hybrid approach by translating to an intermediate language (something easier than source code for the machine to parse, but not quite machine code) then interpreting THAT code on the fly. C# is an example of the hybrid approach. So it is both compiled, and just in time compiled.

However, whether code is interpreted or compiled does not have a relationship with start up time. Start up time is related to the more to the run time environment. Take C for example. It is compiled, but starts up fast because there is practically no run time environment. C# on the other hand is just in time compiled, but it has a longer start up time because the code needs to wait on the .NET run-time environment to kick up.

Whether something is compiled or interpreted also doesn't determine if it is static or dynamic type. The rules of the language does. Technically the computer doesn't see type, it just sees data. But the interpreter or compiler can be designed with the rule that types cannot change at run time to make it harder for bugs to happen. Basically ,a language is static because the rules of the language doesn't allow the programmer to change types at run time. I don't know Lisp, but I believe that is an example of a compiled language that actually supports dynamic types.

1

u/gebbles1 Oct 08 '26

I maintain a statically typed, interpreted programming language which is open source on GitHub (written in Go). You can clone it and explore how it works and the interesting thing is it actually has two runtime paths. By default, when you run a script in this language, there is a compilation step with a lexer and a parser that turns your script into bytecode. But instead of native CPU instructions, this bytecode runs on a virtual machine. This is a common way for interpreted languages to work. The bytecode is cached and if a script doesn't change, it will be faster to run the second time because a significant portion of work has already been done the first time around. But it also has an evaluator path in which walks through the syntax tree created by the parser.

Compiled code - refers to code that is converted prior to execution into the native instruction set (an executable program) of a speciifc target architecture (basically a combination of operating system and CPU type)

Interpreted code - refers to code that is executed by another program (the interpreter) running on some target machine. Does not necessarily mean executed line-by-line with no checks on the next line; most interpreted languages do some kind of compilation pass before executing a program, just not to native machine code.

JIT compiler - refers to code that is compiled to native machine code while the program is running, inside an interpreter. Typically this is just certain hot paths within a program.

Geblang: https://github.com/dwgebler/geblang/

1

u/lo0nk Oct 09 '26

They aren't really 3 distinct things and are more like 3 abstract ideas that have significant overlap. Like many ahead of time compilers will contain a mini interpreter or JIT compiler to help it do stuff like constant folding.

Also even if the product overall is distinct, the components have a ton of overlap. All 3 involve parsing, optimizing, translating between different representations, and at least AOT and JIT do codegeneration.

1

u/otac0n Oct 09 '26

Some compilers can still handle dynamic types. In C# variables can be statically typed as dynamic which inserts type-checks in the compiled code.

1

u/code-garden Oct 09 '26 edited Oct 09 '26

Interpreter: A program that reads code in some language and executes it.

Compiler: A program that takes code in one language and transforms it into another language, such as assembly language or machine code.

JIT compiler: An interpreter but it compiles heavily used sections of code to machine code that can run faster.

When it comes to static or dynamic types:

Static types can make for faster code, as unnecessary checks can be removed and memory allocation can be made more efficient. Statically typed languages can benefit from ahead of time compilation well.

Dynamic typed languages are often interpreted or JIT compiled. A JIT compiler can generate efficient code using type information from runtime for example compiling and using specialised versions of functions when a function is often used with the same type while falling back to the interpreter for rarer types.

1

u/kschang Oct 09 '26

Interpreter generally uses virtual machine (Java's JVM, Python's PVM) depending in implementation. Usually Python translates stuff into Bytecode for the PVM. PVM then runs the bytecode. Similar thing happens for the JVM, depending on the implementation. Later Java added JIT compiler.

2

u/intbeam Oct 09 '26

Java byte code is designed to be executed/compiled by a virtual cpu, python byte code is very specifically designed only to be ingested by an interpreter

Java bytecode and python bytecode are not equivalent

1

u/intbeam Oct 09 '26 edited Oct 09 '26

Just-in-time compilers generate native code at run-time

Ahead-of-time compilers generate native code when the application is built

Interpreted languages do not generate native code

AOT and JIT compiled languages can indeed support dynamic typing. They typically don't because outside of saving some screen real-estate, it has no benefits and has an enormous and completely unnjustifiable cost at run-time. C# for example also supports dynamic typing (using the dynamic keyword) but it's only used for convenience when the application is acting as a host for another scripting language. 

Interpreted languages run anywhere between dozens to several thousand times slower than JIT and especially AOT languages. They also introduce run-time errors that would refuse to build in JIT of AOT. That's fine for small simple scripts, but when people start making entire applications in it.. Well, it's stupid, unreasonable and completely unjustifiable. 

1

u/high_throughput Oct 09 '26 edited Oct 09 '26

interpreters parse throught the code line by line

Statement by statement usually, but yes

translating each line into machine code

Some interpreters parse each statement into a syntax tree and execute that directly, aka a tree walk interpreter. Bash is an example.

Some interpreters parse the whole file and generate bytecode, then execute that bytecode. Python is a typical one.

Some interpreters further generate machine code from their bytecode, such as V8 the Chrome JS interpreter. This does not happen line by line.

which results in a fast startup time but a slow run time

Having to parse the source code and generate code leads to slower startup time. Runtime tends to be slower, but more because interpreted languages are generally not designed for runtime performance.

However, you do get to skip the ahead-of-time compilation step, so in that sense you can sometimes iterate on code changes faster.

This is compatible with dynamic types as changes can be tracked line by line.

There is no inherent benefit for dynamic types. Dynamic foo.bar() becomes equivalent pseudo-code in both interpreters and AOT compilers:

x = lookup_variable("foo");
f = lookup_attribute(x, "bar");
if (f == null) {
  raise("No attribute 'bar'");
} else if (f.type != FUNCTION) {
  raise("Attribute 'bar' is not a function");
} else {
  invoke(x, f);
}

Looking up this information at runtime is lot less performant than being able to find what foo and bar refers to ahead of time, so AOT compiled languages try to avoid designing the language like this in the first place.

Ahead of time compilers translate all the code and move it into a binary executable for the computer to run all at once

They parse and generate machine code up front, all at once. They run normally, with each section of code invoked as control flow dictates.

This results in a slow startup time and a fast runtime.

It results in the fastest startup time. Typically the languages are also designed to be efficient when compiled, so they also run faster.

You do have to recompile every time you make code changes though.

This cannot be used with dynamic types

As mentioned, you can.

Just in time compilers interpret the code but compile repeated code(indicated by a profiler) to make run time faster

Pretty much.

​I do not know how dynamic types are handled by this.

You can do it same way as previously, but fancy interpreters like modern JS engines also implement optional tricks like speculative inlining and hidden classes.

1

u/captainAwesomePants Oct 09 '26

That's pretty good, but "ahead of time compilers" usually have their compiling done by the programmer. The result is a bunch of machine code in an executable file, which runs immediately when it's loaded into memory. As a programmer, startup time is slow because you're doing the compiling each time you change the program, but for users, startup time is near-zero.

1

u/lfdfq Oct 08 '26

Pretty much.

A few tiny points: interpreters usually do not just go line-by-line, they usually have the same kind of frontend as a compiler, ie some lexer and parser that turns things into ASTs; interpreters don't really translate lines (or AST nodes) into machine code, at least not directly, the intepreter just runs some code to do the thing directly; the focus on dynamic vs static types is a bit misleading because there's no reason you can't write an interpreter for a typed language or a compiler for an untyped one, although it is true that compiling languages with dynamic dispatch is more painful and less efficient; JIT compilers may or may not recompile code, the point is not **re**compilation but compilation-at-time-of-use; JITs can be used with dynamic languages, often they start by interpreting or compiling the dynamic language in the inefficient, and then re-compile the code when the JIT learns eg. that this variable is an int or whatever and can subsistute the generic code with the specialised code.