liveBook · Manning

Map

As explained in Latency

A lookup table is a data structure that stores precomputed values indexed for rapid retrieval.

A map stores values by key so a caller can retrieve a value without scanning every entry. A hash map is one implementation. It can hold ordinary application state, or serve as a lookup table for results computed before a latency-sensitive request arrives. section 4.8 section 7.6

The performance gain is about growth, not a guaranteed lookup time. Hash-table search is O(1) on average, versus O(n) for a linear scan of an array. Collisions can make hash-table search O(n) in the worst case. Big O describes how work grows with input size, not how many milliseconds an operation takes. section 7.2

pub struct KVStore {
 data: Mutex<HashMap<String, String>>,
}

impl struct KVStore {
 pub fn get(&self, key: &str) -> Option<String> {
   self.data.lock().unwrap().get(key).cloned()
 }

 pub fn put(&self, key: String, value: String) {
   self.data.lock().unwrap().insert(key, value);
 }
}

A Rust key–value store wraps a HashMap<String, String> in a Mutex. Its get method locks the map and returns a cloned value if the key exists; put locks it and inserts a pair. section 4.8

Local reads on a replica

A primary stores pairs such as (a, 1) and broadcasts later writes. A new replica first receives a snapshot, then applies updates, allowing its local map to answer GET requests. Before joining, its GET requests find nothing. The in-memory map provides the backing store, not durable storage. section 4.8

For a precomputed lookup table, do the expensive calculation at build time, startup, or another convenient time; store the result under a key; then look it up on the request path. Storage-usage statistics are one candidate when calculating them on demand could take seconds. The trade-off is freshness: periodically computed values can become stale, while incremental updates can keep a more current snapshot without repeating the full calculation. section 7.6

How Latency covers it

How deep it goes
Shows synchronized Rust get and put operations and compares lookup complexity; does not develop hash-table internals.
Examples
Uses a Rust primary–replica key–value store, an array-scan comparison, and precomputed storage-usage statistics.
What it assumes
Assumes familiarity with keys, values, and basic code while explaining lookup complexity through a linear-scan comparison.
What you can do after
Build it from scratch as a synchronized in-memory Rust key–value store, or evaluate lookup tables for avoiding expensive request-time computation.
How it connects
Connects maps to hash tables, replication, precomputation, caching, and algorithmic complexity.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.