How a computer works

Rust for bioinformaticians

What this lecture is

A ground-up tour of what is inside the machine that runs your scripts.

To fully understand Rust, you need an idea of how a computer actually works.

What we’ll cover:

  • The big picture: CPU, RAM, registers, cache, storage
  • The memory hierarchy and the memory wall
  • Cache lines and why sequential access wins
  • How a program lays out memory: code, static data, heap, stack
  • Stack frames and function calls
  • Boxed vs unboxed values
  • The OS, syscalls, and the kernel/user-space split
  • What happens when you run a program, end to end
  • Why this matters for bioinformatics performance

The big picture — a computer is gears that move bits

One chip does the work; everything else feeds it or stores its results.

A motherboard rectangle containing labelled components: a CPU box with caches at upper centre, two narrow DDR4 RAM modules to its right, an NVMe SSD and a network card on the far right, a long GPU board across the lower half, and a power supply on the left. Thick and thin lines connect components, labelled memory bus 100 ns, PCIe 10 microseconds, and PCIe NIC varies. Schematic of a typical desktop or laptop: motherboard with CPU socket, RAM DIMM slots, NVMe storage, GPU, network card, and PSU, connected by buses of varying speed.

Figure 1

The same machine in the wild

An ASRock K7VT4A Pro motherboard photo with three overlaid annotation arrows labelling only the CPU socket on the left, the RAM DIMM slots on the upper right, and the storage (IDE/SATA) connectors on the lower right.

The three things you need to recognise: CPU, RAM, storage. Photo: Darkone, Wikimedia Commons, CC BY-SA 2.5.

Registers — the CPU’s hands

Registers are the tiny storage slots inside the CPU. The CPU can only do arithmetic on values that are sitting in registers — everything else has to be loaded in first.

  • About 16 general-purpose slots, 64 bits each — hold whatever the program is currently working with
  • A handful of special-purpose slots — one points at the next instruction, one points at the top of the stack
  • A separate set of wider slots for floating-point and vector maths
Schematic of the von Neumann architecture: a CPU block containing ALU, control unit, and a register file, connected to a Memory block by a bidirectional data bus; an I/O block also connected to the bus.

The von Neumann architecture (the standard design where code and data share the same memory, fetched into the CPU over a bus): the CPU has a small set of registers that exchange data with main memory over a bus.

RAM — where the program lives while it runs

RAM holds the running program and its working data.

  • Gigabytes — laptop: 16–64 GB; server: up to 1 TB
  • Volatile — contents vanish on power-off (save to disk)
  • Much faster than disk, much slower than the CPU

Working sizes in bioinformatics: human reference + BWA index ~8 GB; single-cell expression matrix 30+ GB; pangenome graph index 100+ GB.

The memory hierarchy — fast & small, or big & slow

You cannot have memory that is both huge and fast. Real machines stack several layers, fastest-and-smallest at the top.

A pyramid divided into horizontal bands. From top (smallest, fastest): registers, then L1 cache, L2 cache, L3 cache, then RAM, then SSD, then HDD, then network storage at the wide base. Each band labelled with typical capacity.

Figure 2

The memory wall — CPUs got fast, RAM didn’t

CPU clock speeds rose roughly a thousand-fold from 1980 to 2005. RAM latency improved by less than a factor of ten in the same period. So the gap between CPU and RAM has grown to hundreds of cycles.

A line chart, x-axis years 1980 to 2025, y-axis on log scale. One line climbs steeply for CPU clock speed; a second nearly-flat line for DRAM latency. The growing gap between them is shaded and labelled the memory wall.

Figure 3: CPUs got ~1000x faster; RAM did not.

Cache — bridging the gap

A cache is a small block of fast memory inside the CPU that automatically holds copies of recently-used (and likely-soon-to-be-used) bytes from RAM. You never ask for it explicitly; the hardware just does it.

  • L1 cache — ~32 KB per core, ~1 ns access
  • L2 cache — ~256 KB to 1 MB per core, ~3 ns
  • L3 cache — ~8 to 64 MB shared across cores, ~10 ns
  • All three together are still thousands of times smaller than RAM

The CPU — arithmetic + decisions

The central processing unit is the one component that actually runs code. Everything else just feeds it or stores its results.

A horizontal flow chart from register → L1 → L2 → L3 → RAM with each transition labelled with its access latency in nanoseconds. Each level coloured from green (fast) to red (slow).

From registers to RAM in nanoseconds — what the CPU actually waits on. Each arrow is a cache miss — the requested byte wasn’t in the faster level above, so the CPU has to fetch from the next level down.

A small schematic of a CPU package showing 4 cores in a 2x2 grid, each with private L1 cache, L2 cache, and a shared L3 cache at the bottom.

A CPU die — registers, ALU, L1/L2/L3 all live on the same silicon chip.

A small schematic of a DDR4 RAM stick: 8 memory chips arranged in a row on a PCB with edge connector at the bottom.

A RAM module — separate chip board sitting in a DIMM slot.

A cache line — 64 bytes at a time

When the CPU misses cache and fetches from RAM, it does not fetch one byte — it fetches a whole 64-byte block called a cache line.

A horizontal strip of 64 small square cells representing a region of RAM. The byte at offset 5 is highlighted as the byte the CPU has requested.

Step 1: CPU asks for byte at offset 5.

A cache line — what step 2 looks like

The full 64-byte cache line is shown being copied from RAM into the CPU cache as a single block, in one slow RAM access.

Step 2: the whole 64-byte line is fetched into the cache (one slow RAM trip).

Cache lines — and why sequential access wins

Subsequent reads from adjacent bytes within the same 64-byte line are served from the cache, marked as fast cache hits.

Step 3: nearby byte reads are cache hits, fast and free.

Sequential reads inside a line are essentially free.

Storage — SSD vs HDD vs RAM

Medium Latency Bandwidth Persistent? Capacity (laptop, 2026)
RAM (DDR4/5) ~100 ns ~30 GB/s no 16–64 GB
NVMe SSD ~100 µs ~3–7 GB/s yes 0.5–4 TB
SATA SSD ~200 µs ~0.5 GB/s yes up to 8 TB
HDD (spinning) ~10 ms ~0.15 GB/s yes up to 20 TB

Bar chart of common bioinformatics file sizes on a log scale, with horizontal markers for L3 cache, RAM, SSD.

Most real datasets overflow RAM; streaming is the rule.

Photo of a DDR3 RAM module — a long thin green PCB with memory chips along it and a notched gold edge connector at the bottom.

DDR3 RAM stick.

Photo of an NVMe M.2 SSD — a small green PCB about the size of a stick of gum with a single controller chip and 1-2 memory chips.

NVMe SSD (M.2 form factor).

Photo of a hard drive with the cover removed, showing the silver platter and the read/write arm hovering over it.

Hard disk drive with platter visible.

Photos (left to right): Alan Lorenzo, DDR3 RAM, CC BY-SA 3.0; Wikimedia user -stk, Toshiba M.2 SSD, CC BY-SA 4.0; Evan-Amos (Vanamo Media), laptop HDD exposed, CC BY-SA 3.0.

Inside the running program

A running program in memory

The OS sets up the same shape for every process: code at the bottom, heap (grows up), stack (grows down), with a big empty gap in between.

A tall rectangle representing one process's virtual address space, divided into horizontal bands: at the bottom '.text (machine code, read-only)', then a 'heap' band labelled 'big data lives here' with an upward arrow showing it grows toward higher addresses, then a big empty middle marked as unused virtual address space, then a 'stack' band labelled 'variables inside functions live here' with a downward arrow, and a 'kernel space' band at the top.

Figure 4: Every process gets the same private memory shape.

Stack — fast, structured, automatic

The stack is a fast, last-in-first-out region used for function calls. Each call pushes a fresh stack frame (its arguments and local variables) on top; returning pops it.

def gc_count(seq):
    return sum(1 for b in seq if b in "GC")

def main():
    seq = "ACGTACGT"
    n = gc_count(seq)      # push gc_count frame
    print(n)               # gc_count returned, pop

main()

Stack frames — push on call, pop on return

A stack diagram with a single frame for main() containing the local variable seq.

Step 1: main() running — one frame on the stack.

Stack frames — push on call, pop on return (step 2)

The stack now has two frames: main() at the bottom and gc_count() on top, holding its local variables.

Step 2: main() calls gc_count(seq) — a new frame pushed on top.

The stack has returned to a single main() frame; the gc_count() frame has been popped and the result is held in main's local n.

Step 3: gc_count() returns — its frame is popped; main continues.

Heap — flexible, slower, you manage the lifetime

A large region for data whose size or lifetime isn’t known at compile time.

  • Ask the allocator for n bytes, get a pointer back
  • Slower than stack pushes, but flexible — growing vectors, unknown-length strings, graphs
  • Someone must decide when to free it: you (C), a garbage collector (Python), or the compiler (Rust)
Two-column diagram. Left column shows a blue 'Stack' region containing three small fixed-size frames labelled process_read, parse_fastq and main, each with local variables. Right column shows a wider orange 'Heap' region containing several different-sized white rectangles representing allocations of varying sizes (a small String, a Vec<u8> of 1 KB, a Box<i64>, a 12 MB Vec<Record>, a HashMap node, a Box<[u8; 64KB]>) interspersed with small dashed 'free' gaps. Two blue pointer arrows from variables inside the stack frames point into allocations inside the heap. A box at the bottom shows the C malloc/free and Rust Box::new allocator API. Annotations on the right read 'any size, any lifetime', 'you decide when to free', 'more bookkeeping than the stack'.

Tidy stack frames on the left; the heap on the right holds allocations of any size, with pointers from stack-frame variables reaching into them.

Boxed vs unboxed — what the value actually costs

In C or Rust, 42 lives directly in its 4-byte slot. In Python, the variable is a pointer; the value is wrapped in a heap object with a type tag and a refcount.

Two columns. Left: C/Rust shows a single 4-byte box holding 42 directly. Right: Python shows a small pointer on the stack pointing into the heap to a larger object containing a type tag, a reference count, and the value 42.

Same 42, very different memory.

  • A million-int list: ~60 MB in Python vs ~4 MB in C/Rust

The operating system — referee and gatekeeper

Your program is not alone on the machine. The OS decides which process gets the CPU when, hands out memory, and is the only thing allowed to touch hardware directly.

  • Kernel — the privileged core that runs the scheduler, file system, network stack
  • User space — where ordinary programs run, with no direct hardware access
  • Syscall — the only legal way for your program to ask the kernel to do something
Three stacked bands: top labelled User space containing three process boxes firefox, vim, your_program; middle band Kernel space labelled Linux/macOS/Windows kernel containing process scheduler, file system, memory manager, network stack; bottom band Hardware containing CPU, RAM, disk, network. Downward arrows from each user-space process into the kernel labelled with example syscalls open data fa, mmap 8 MiB, send packet, then thick arrows from kernel to each hardware box.

Figure 5: Only the kernel touches hardware; user code goes through syscalls.

OS vs software — syscalls are the doorway

When your Python code does f = open("data.fa"), the runtime issues an open syscall — a controlled jump into the kernel, which checks permissions, locates the file, and returns a file descriptor.

Putting it together — what happens when you run a program

  1. You type ./my_tool data.fa in a shell
  2. The shell asks the kernel to create a new process (fork + exec)
  3. The kernel loads your binary from disk into a fresh chunk of RAM
  4. It sets up the program’s memory layout: code, static data, an empty heap, an empty stack
  5. It places one register (rip) at the start of main and schedules the CPU to run you
  6. Your code runs, calling functions (stack), allocating data (heap), making syscalls (kernel)
  7. main returns; the kernel reclaims your memory and tells the shell you’re done

Why this matters for bioinformatics

  • Research (BWA, minimap2): big indexes, tight loops
  • Clinical (DeepVariant, GATK): hours not days per patient
  • Environmental (Kraken2): TB-scale metagenomes, streaming
  • Public health (Nextstrain): thousands of samples per week

Why latency matters — clinical turnaround

Same machine speed, very different acceptable wait times depending on the question:

Scenario Acceptable turnaround
ER: suspected sepsis — which antibiotic? < 1 hour
ICU: drug-resistance variant calling hours
Rare-disease workup: full WGS analysis days
Cancer panel: tumour mutation report days to a week
Public-health outbreak: SARS-CoV-2 surveillance days
Cohort study / GWAS: hundreds of genomes weeks-months
Research method development months

The bottom rows are tolerant of slow code. The top rows are NOT — every second your pipeline takes is a second the clinical decision is waiting on you. Rust’s “compile-time-free of a class of bugs, runtime-fast” combination is most valuable up there.

What’s next

  • Setup: intro/install.qmd — Rust, cargo, VS Code
  • Day 1, lecture 1: day1/lec1-why-rust.qmd — Why Rust for bioinformatics
  • Day 1, exercises: GC content, base counts, complement, Phred score, Hamming distance