TRT
Sign in

A Hash Lookup From Key to Verified Value

Hash amy to slot 5, collide with bob, probe to slot 6, and see why load factor forces an early resize.

The Runtime Theory Team05 stages

trace / request.md

HASH THE KEYPICK THEHOME SLOTCOMPARESTORED KEYSPROBE ORCHAIN ONWARDRESIZE BEFOREFILLING

readyHash amy to a number such as 96717. The value depends only on key bytes, so repeats agree, while distinct keys may still collide.

Hash amy, land on occupied slot 5, verify inequality, and settle in slot 6. The hash proposes; equality disposes.

For the worked walkthrough, continue to the topic article and execution trace.

Stage-by-stage execution walkthrough

This request flow decomposes A Hash Lookup From Key to Verified Value into 5 sequential stages so you can trace where latency accumulates, which state transitions are irreversible, and where production failure modes occur.

Stage 1: Hash the key

Hash amy to a number such as 96717. The value depends only on key bytes, so repeats agree, while distinct keys may still collide. During this transition, the runtime enforces strict state invariants before handing control to the next stage: inputs are validated at the boundary, state mutations are ordered deterministically, and latency/resource costs are bounded so downstream consumers never observe a torn or partially initialized state.

Stage 2: Pick the home slot

Mask by capacity 8 to slot 5. It holds bob, so overwriting would lose data; the table must resolve the collision. During this transition, the runtime enforces strict state invariants before handing control to the next stage: inputs are validated at the boundary, state mutations are ordered deterministically, and latency/resource costs are bounded so downstream consumers never observe a torn or partially initialized state.

Stage 3: Compare stored keys

Bob does not equal amy, so the search continues. Equal hashes alone never prove equal keys; only the equality check decides. During this transition, the runtime enforces strict state invariants before handing control to the next stage: inputs are validated at the boundary, state mutations are ordered deterministically, and latency/resource costs are bounded so downstream consumers never observe a torn or partially initialized state.

Stage 4: Probe or chain onward

Linear probing steps to slot 6 and stores amy there. Chaining would instead append to the bucket list. Both lengthen future lookups. During this transition, the runtime enforces strict state invariants before handing control to the next stage: inputs are validated at the boundary, state mutations are ordered deterministically, and latency/resource costs are bounded so downstream consumers never observe a torn or partially initialized state.

Stage 5: Resize before filling

Past a load factor near 0.75 the table doubles and rehashes every entry, since slot positions depend on capacity. One resize costs linear time; doubling keeps inserts amortized constant. During this transition, the runtime enforces strict state invariants before handing control to the next stage: inputs are validated at the boundary, state mutations are ordered deterministically, and latency/resource costs are bounded so downstream consumers never observe a torn or partially initialized state.

Production invariants and failure modes

Average lookup stays constant through an even hash and a bounded load factor; collisions, packing, or mutated keys turn it linear. When debugging or capacity-planning this flow in production, instrument each stage boundary separately (measuring both p50 median and p99.9 tail latency), verify what happens when an intermediate stage times out or drops packets, and ensure retry or fallback paths preserve end-to-end idempotency.

Not started

Sign in to save your learning progress.

Sign in to save