A translator walks your text down to instructions, one form at a time
A program is text until a compiler or interpreter lowers it: tokens, a tree, a shared middle form, then machine code.
The processor only runs instructions. You write text. Follow one line across the gap. It starts as bytes in a file, which the processor cannot read. The translator splits it into words, called tokens. It works out the grammar and builds a tree: the sum sits under the assignment. It checks the types fit, and rewrites the tree in a simple middle form. Here the optimiser notices that one plus two is always three. Last, it emits an instruction for one specific chip. That middle form is shared. Many languages lower into it and many chips are served from it, so the hard parts are built once.
From source to instructions · Going deeper · 1 of 2
The front-end pipeline
Lexing, parsing, type checking and lowering to an intermediate representation are the same in compilers, interpreters and editor tooling: linters and language servers reuse the front end. LLVM IR, GCC’s GIMPLE and JVM bytecode all play the role of the shared form.
From source to instructions · Going deeper · 2 of 2
Optimising, then picking instructions
The middle end is a chain of passes, each taking the middle form and returning a better one: constant folding, dead-code removal, inlining, loop transforms. The back end then selects instructions and decides which values get one of the 16 registers on x86-64. A spill to the stack inside a hot loop can erase every other gain.
Compiled, interpreted, JIT
Compiled, interpreted, JIT: the same translation at different times
Translate ahead of time for speed, interpret for instant start, or ship bytecode and compile the hot parts while running.
Every language has to translate. What differs is when. Ahead of time: the whole program becomes native code before it ever runs. Nothing is left to do at run time, so this is the baseline for speed. Interpreted: nothing happens beforehand, so it starts instantly. But each step is translated as it runs, every time, which costs ten to a hundred times more per operation. In between: compile once to portable bytecode, start by interpreting it, and recompile the busiest parts to native code while the program runs. Once warm, that gets close to the baseline.
Compiled, interpreted, JIT · Going deeper · 1 of 3
Three contracts, side by side
The boundaries blur in practice. Go compiles ahead of time but ships a runtime for its lightweight threads and garbage collector. Python caches bytecode in __pycache__ but still interprets it. The JavaScript engine in Chrome has four compiler tiers.
Compiled, interpreted, JIT · Going deeper · 2 of 3
Linking: joining the pieces
Each source file compiles alone into an object file with holes where other files’ functions go. The linker builds one table of who defines what, then patches every hole with a final address. “Undefined reference” means no file defined it. Static linking copies library code in; dynamic linking lets the system patch it at start-up, so one copy of a library serves every program.
Compiled, interpreted, JIT · Going deeper · 3 of 3
Function calls and the stack frame
Every call pushes a frame: the return address, saved registers and local variables. A calling convention fixes where arguments travel; on 64-bit Linux and macOS the first six go in registers. Writing past a local buffer can overwrite the return address, which is “smashing the stack”; a random canary checked on return is the standard guard.
How a JIT works
A JIT starts slow, watches the program, then bets on what it saw
A JIT compiles only hot code, specialised to the types it observed, and falls back when the bet fails.
Follow one function that is called a million times. At first the runtime just interprets it, slowly, while counting calls and noting the types that arrive. At about a thousand calls it compiles the function to native code. It has only ever seen arrays of floats, so it compiles for exactly that, and leaves one cheap check, a guard, in case it is wrong. Roughly twenty times faster. At ten thousand calls it compiles again, harder: about a hundred times the interpreter. Then one day a caller passes a string. The guard fails, the native code is thrown away, and the function drops back to the interpreter.
How a JIT works · Going deeper
Tier-up and de-optimisation
Thresholds are around 1,000 calls for a baseline tier and 10,000 for an optimising one. A JIT can be bolder than an ahead-of-time compiler because it sees real behaviour: a branch never taken is left out of the fast path. Start-up pays for both interpreting and profiling, which is why short benchmarks need a warm-up.
Check yourself
A benchmark calls a function 400 times on a runtime that compiles hot code after 1,000 calls, and reports it as slow. What did it measure?
It stopped before the tier-up, so every call was interpreted, with profiling on top. Benchmarks on a JIT need a warm-up.
Language design choices
Every language is four choices, and what it refuses is its character
Control or safety, speed or expressiveness, static or dynamic types, manual or managed memory: four independent positions.
A language is defined as much by what it refuses as by what it allows. Four questions cover most of it. How much raw control over memory, against how much safety? C lets you do anything. Python checks every access. Fast to run, or fast to write? Each step toward fewer lines costs roughly ten times at run time. Are type mistakes found before the program runs, or while it runs? And who frees memory: you, the runtime, or, in Rust, the compiler? Watch Rust across the four. It sits beside C on two and far from it on the others. The choices are independent.
Language design choices · Going deeper
The four axes as a grid
C exposes undefined behaviour: use an address that is not yours and anything may happen. Python and JavaScript hide raw pointers and bounds-check every access. Rust keeps pointers but tracks them at compile time, and fences the unchecked operations inside explicit unsafe blocks.
Static and dynamic typing
Static typing catches the mistake at build time; dynamic typing meets it in production
A type checker moves a class of failures from the moment a user runs the line to the moment you build.
This code asks a number for its length. Every language agrees that is a mistake. They differ on when you find out. A statically typed language checks before the program runs, and the compiler simply refuses to build it. The mistake never ships. A dynamically typed language builds happily. The mistake surfaces only when that exact line runs, which may be in front of a user, unless a test happened to exercise it. Gradual checkers, added to dynamic languages, catch it wherever you wrote the types down, and nowhere else.
Static and dynamic typing · Going deeper
Across the program’s lifetime
Gradual typing, such as TypeScript or Python with mypy, lets a project move checks earlier without a rewrite. The catch: types are enforced only where you annotated. Modern checkers also narrow as the code branches: a value that is “string or null” becomes plain string inside an if that rules null out.
Memory management
Someone must free memory: you, the runtime, or the compiler
Manual freeing risks three classic bugs, a garbage collector costs pauses, and ownership moves the work into the compiler.
Every program asks for memory and must give it back. The question is who does that. In C you do. Free it and keep using it, and you are reading memory that now belongs to something else. Forget to free it, and the program leaks. Free it twice, and the bookkeeping is corrupted. In Java, Go, Python and JavaScript the runtime does it. Memory that nothing points at any more waits until a collector comes by and frees it. The price is a short pause. In Rust the compiler does it. Every value has one owner, and where that owner goes out of scope the compiler inserts the free. No pause, but stricter rules before your code will build.
Memory management · Going deeper · 1 of 2
Three strategies compared
Manual management, tracing collection and ownership solve the same problem at different points in the lifecycle. Concurrent collectors such as Go’s and Java’s ZGC keep the stop-the-world part to single-digit milliseconds even on heaps of hundreds of gigabytes. Rust gets no pause at all but rejects programs whose lifetimes it cannot prove.
Memory management · Going deeper · 2 of 2
Most objects die young
Across decades of measurements, most allocations become unreachable shortly after they are made. A generational collector keeps a small young area, typically 5 – 10% of the heap, collected often and cheaply, and promotes survivors to an old area collected rarely.
Garbage collection
Garbage is whatever the program can no longer reach
A tracing collector marks everything reachable from the roots and frees the rest. Counting references is simpler but leaks cycles.
How does a collector know what is safe to free? It starts from the roots: the variables the program can touch right now. It follows every pointer from there, and marks each object it reaches. A leads to B and C, and C leads to D. Whatever was never marked cannot be reached by any code, ever again, so it is freed. The other approach is to count references and free an object when its count hits zero. That has no pause. But look at E and F: they point at each other, so each count stays at one, and they are never freed. One observation makes tracing cheap: most objects die young, so collectors look at new memory far more often than old.
Garbage collection · Going deeper · 1 of 2
Mark and sweep
Marking is a graph traversal from the roots; sweeping is a linear walk of the heap. Marking costs in proportion to live data, sweeping in proportion to the whole heap. Compacting collectors add a third phase that moves live objects together to defeat fragmentation.
Garbage collection · Going deeper · 2 of 2
Reference counting and cycles
Swift, Python and C++ shared pointers count references. To break cycles they offer a weak reference that does not raise the count, and Python also runs a periodic tracing collector for cycles the programmer forgot.
Check yourself
Nothing else points at C or D. Which kind of collector can free them?
C and D each hold a count of 1, so counting never reaches zero. A trace from the roots never reaches them, so they are swept.
Programming paradigms
Four ways to organise the same computation
A paradigm is a default shape for code: commands, objects, transformations, or a description of the result.
Here is one small task, the sum of the squares from one to n, written four ways. Imperative: a list of commands that change a running total. Object-oriented: the data lives inside a thing, and you ask that thing for the answer. Functional: a chain of transformations, where nothing is changed, only produced. Declarative: you describe the result you want, and the system decides how to get it. The answer is the same each time. What changes is how you think about the problem, and real programs mix all four.
Programming paradigms · Going deeper · 1 of 3
The same task in four paradigms
Modern languages mix all four, and the choice is per module rather than per language: imperative input and output, an object-oriented domain model, functional transformations, embedded SQL for queries.
Programming paradigms · Going deeper · 2 of 3
Closures: functions that remember
A closure is a function that carries the variables it was created with: a pointer to code plus a small record of captured values, kept on the heap so they outlive the call that made them. C has none, which is why C callbacks always pass an extra pointer of user data.
Programming paradigms · Going deeper · 3 of 3
What an object looks like in memory
An object is a block of fields plus a hidden pointer to its class’s table of methods, the vtable. A method call is two memory loads: fetch the table, fetch the slot, then call. Go interfaces and Rust trait objects carry the table pointer beside the data instead.
Complexity and Big O
How the work grows matters more than how fast each step is
Big-O names how work grows with input. At a million items, that shape is the difference between a millisecond and 17 minutes.
Take a million items and a machine that does one step every nanosecond. Constant work, like a hash map lookup, takes one step regardless. Logarithmic work, like searching a sorted list by halving, takes about twenty. Linear work touches every item once: a millisecond. A good sort is a little more: twenty milliseconds. Quadratic work, comparing every pair, takes seventeen minutes on the same machine. And exponential work, trying every combination, is hopeless: thirty items already take a second. Notice the scale: each mark is a thousand times the one before.
Complexity and Big O · Going deeper · 1 of 4
The growth curves
O(f(n)) means that for large enough n the work grows no faster than a constant times f(n). Constants are dropped because they vary by machine. The curves show why a quadratic loop hidden in a nightly job eventually brings it down as data grows.
Complexity and Big O · Going deeper · 2 of 4
Sorting: the classic trade-offs
No comparison sort can beat n log n on average: that is a mathematical floor. Real libraries ship hybrids: Python’s Timsort detects runs that are already sorted, and C++ uses quicksort with fallbacks. Stable means equal items keep their order, which matters when you sort by one key and then another.
Complexity and Big O · Going deeper · 3 of 4
Graphs: breadth or depth first
Breadth-first search uses a queue and explores level by level, which finds shortest paths. Depth-first search uses a stack and follows one branch to its end, which finds cycles and orderings. The only structural difference is queue versus stack. Dijkstra’s shortest path is breadth-first with a priority queue.
Complexity and Big O · Going deeper · 4 of 4
Recursion and the stack
A recursive function adds a stack frame per call, so deep recursion can run out of stack. If the recursive call is the very last step, some compilers reuse the frame: tail-call optimisation. Scheme and OCaml guarantee it; Rust and the JVM do not.
Check yourself
Comparing every customer with every other takes 1 second for 1,000 customers. About how long for 100,000?
100 times the customers means 100 × 100 = 10,000 times the pairs: 10,000 seconds, nearly three hours. That is O(n²).
Core data structures
Four shapes hold most of the world’s data
An array, a hash map, a tree or a queue: choose by the operation you do most often.
An algorithm is a procedure. A data structure is the shape it works on, and the right shape can make a hard problem trivial. Four cover most needs. An array keeps items side by side, so jumping to any position is one step, but inserting in the middle means shifting everything after it. A hash map uses the key itself to compute where the value lives, so finding by key is one step, but it keeps no order. A balanced tree keeps everything sorted, and finds an item in a handful of steps. Database indexes are trees. A queue or stack only adds and removes at the ends, which is exactly what task lists and undo need.
Core data structures · Going deeper
Costs of each operation
Big-O is the floor of the conversation, but memory layout decides the wall clock. A scan of a small array that sits in cache beats a tree lookup that misses cache, even though the tree is better on paper.
Hash maps
A hash map finds a key in one step because the key computes its own address
Hash the key, pick a slot, store it there. Collisions become short lists, and the table doubles before they grow long.
Most programs lean on a hash map, so look inside one with eight slots. To store Alice, run her name through a hash function and take the remainder by eight. That gives three, so she goes in slot three. To find her later, do the same sum and look in slot three. No searching. Bob hashes to seven. So do Carol and Dave. That is a collision, and the slot simply keeps a short list of the three. Short lists are fine; long ones are not. So when the table is three quarters full it doubles to sixteen slots and places every key again. That one insert is slow, and it is paid back by all the fast ones around it.
Hash maps · Going deeper
Chaining, probing and hash flooding
Chaining keeps a small list per slot; open addressing probes neighbouring slots instead. Either way a load factor near 0.75 triggers a rehash, which is O(n) once and O(1) amortised. Attackers can choose keys that all collide, so Python, Rust and Go randomise the hash seed at start-up.
Summary
A language is a translator plus a set of refusals
Text becomes instructions; four choices shape the language; algorithms and data carry over to all of them. Next: distributed systems, when one machine is not enough.
Put the lesson on one page. In the middle, the mechanical part: source text goes through a translator and comes out as instructions, ahead of time, on the fly, or a mix. Underneath, the part with opinions: four choices about control, speed, types and memory. What a language refuses on each is what makes it feel the way it does. And on top, the part that does not belong to any language: how the work grows, and the shape of the data. Those carry over unchanged to every language you will ever use. Each popular language is also a written standard, which is why the same JavaScript runs in three companies’ browsers.
Summary · Going deeper
The map of this lesson
Source text goes down one of a few paths to running instructions, and every language takes a position on four trade-offs. That choice of path and position is roughly the personality of every language you will meet.
Lesson complete
You finished “Instructions”
Answer the questions along the way to see your score here.