What is the memory hierarchy, what is the Disk Access Machine (DAM) model, and why do Binary Search Trees fail catastrophically in secondary storage?
Computer memory architectures are fundamentally governed by physical and economic constraints on latency and bandwidth.
1. The Memory Latency Gap:
- CPU Registers & L1/L2/L3 Caches: Operate at 0.5 to 10 nanoseconds, embedded directly onto the processor die;
- DRAM (Main Memory): Operates at 50 to 100 nanoseconds, limited by bus capacitance and row precharge delays;
- Solid-State Drives (NVMe Flash): Operate at 10 to 50 microseconds (10,000 to 50,000 ns), reading data in 4KB or 8KB flash pages;
- Hard Disk Drives (HDD): Operate at 5 to 10 milliseconds (5,000,000 to 10,000,000 ns) due to mechanical arm seek and rotational latency.
Secondary storage is between 100× (SSD) and 100,000× (HDD) slower than DRAM!
2. The Disk Access Machine (DAM) Model:
Formulated by Alok Aggarwal and Jeffrey Vitter in 1988, the DAM model formalizes external memory computation:
- Data resides on high-capacity secondary storage partitioned into blocks of size B (e.g., 4096 bytes);
- Internal memory can hold M elements (M/B blocks);
- CPU computation on data resident in internal memory is modeled as free; the performance metric is strictly the number of Block I/O transfers between disk and memory.
3. Catastrophic Breakdown of Binary Search Trees in External Memory:
In a balanced binary search tree (AVL or Red-Black tree) storing N records:
- Each node contains only 1 key and 2 child pointers (consuming ~32 bytes). An entire 4096-byte disk block would be loaded to access a single 32-byte node, wasting 99.2% of transfer bandwidth!
- Tree height is h ≈ log₂(N). For N = 16,777,216 records, h = 24 levels. Searching for a record requires 24 random block reads. On an HDD, 24 seeks take 24 × 10 ms = 240 ms per query (only 4 queries/sec)! B-Trees solve this by packing hundreds of keys into every 4KB block, reducing tree height to 3 levels.