AlgoViz

Hash implementation

medium 18 min

A hash table is an array of buckets plus a hash function that turns each key into an index.

Where it shows up

The same pattern appears across everyday software.

  • Every Python program

    Module namespaces, object attributes, keyword arguments and JSON objects are dicts, and sets back membership tests everywhere. The interpreter's speed depends on the hash table design described here.

  • Database hash indexes and joins

    PostgreSQL offers hash indexes for equality lookups, and a hash join builds an in-memory hash table on the smaller table's join key, then probes it once per row of the larger table.

  • Caches: Memcached and Redis

    Both are giant in-memory hash tables from key to value. Redis grows its table incrementally, moving a few buckets on each operation so a resize never pauses the server.

  • Compiler symbol tables

    Compilers and interpreters map every identifier to its type, scope and address in a hash table, looked up each time the name appears in the source.

  • Counting and deduplication

    Word frequencies, unique visitors, seen-before checks in crawlers and graph searches: one hash map or set gives O(1) average per item instead of a scan.

  • URL shorteners and password checks

    A shortener maps short codes to long URLs in a hash table. Password storage also uses hashing, but with deliberately slow cryptographic hashes (bcrypt, Argon2) rather than the fast table hashes used here.