# gebi/cowmap architecture

> Architecture diagram of the GitHub repository gebi/cowmap, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gebi/cowmap
- Repository: https://github.com/gebi/cowmap

## Overview

This is a generic, in-memory concurrent Go map using 64 sharded HAMT roots. Clients invoke lookup, iteration, insertion, and deletion through Map. Each operation hashes and routes a key to one shard. Reads load an atomic root and traverse immutable bitmap nodes, while writers clone affected paths and publish them with CAS retries. Collision buckets handle identical hashes, and iterators traverse per-shard root snapshots. The graph represents the single sampled runtime file; no external persistence or service layer exists.

Main entry points: [`cowmap.go`](https://github.com/gebi/cowmap/blob/main/cowmap.go) (Map Operations)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Map API"]
  node_map_api["Map Operations<br/>[cowmap.go]"]
  node_hashing["Hash Strategy<br/>[cowmap.go]"]
  node_reader["Snapshot Reader<br/>[cowmap.go]"]
  node_iterator["Snapshot Iterator<br/>[cowmap.go]"]
end

subgraph group_concurrency["Concurrency State"]
  node_writer["CAS Writers<br/>[cowmap.go]"]
  node_roots[("Shard Roots<br/>[cowmap.go]")]
  node_backoff["CAS Backoff<br/>[cowmap.go]"]
end

subgraph group_hamt["HAMT Engine"]
  node_hamt_nodes["Bitmap Trie Nodes<br/>[cowmap.go]"]
  node_path_copy["Path Copying<br/>[cowmap.go]"]
  node_deletion["Structural Deletion<br/>[cowmap.go]"]
  node_collisions["Collision Buckets<br/>[cowmap.go]"]
end

node_client(("Client"))

node_client -->|"invokes"| node_map_api
node_map_api -->|"hashes keys"| node_hashing
node_map_api -->|"dispatches reads"| node_reader
node_map_api -->|"dispatches iteration"| node_iterator
node_map_api -->|"dispatches writes"| node_writer
node_reader -->|"loads roots"| node_roots
node_reader -->|"traverses trie"| node_hamt_nodes
node_reader -->|"scans buckets"| node_collisions
node_iterator -->|"loads snapshots"| node_roots
node_iterator -->|"iterates nodes"| node_hamt_nodes
node_iterator -->|"iterates buckets"| node_collisions
node_writer -->|"CAS publishes"| node_roots
node_writer -->|"inserts paths"| node_path_copy
node_writer -->|"deletes paths"| node_deletion
node_writer -->|"retries with"| node_backoff
node_path_copy -->|"clones paths"| node_hamt_nodes
node_path_copy -->|"creates buckets"| node_collisions
node_deletion -->|"prunes paths"| node_hamt_nodes
node_deletion -->|"updates buckets"| node_collisions
node_hashing -->|"routes by hash"| node_roots
node_roots -->|"stores immutable roots"| node_hamt_nodes

click node_map_api "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_hashing "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_reader "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_iterator "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_writer "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_roots "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_backoff "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_hamt_nodes "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_path_copy "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_deletion "https://github.com/gebi/cowmap/blob/main/cowmap.go"
click node_collisions "https://github.com/gebi/cowmap/blob/main/cowmap.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_map_api,node_hashing,node_reader,node_iterator,node_client toneBlue
class node_writer,node_roots,node_backoff toneAmber
class node_hamt_nodes,node_path_copy,node_deletion,node_collisions toneMint
```

## Components

### Map API

- [Map Operations](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [Hash Strategy](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [Snapshot Reader](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [Snapshot Iterator](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`

### Concurrency State

- [CAS Writers](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [Shard Roots](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [CAS Backoff](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`

### HAMT Engine

- [Bitmap Trie Nodes](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [Path Copying](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [Structural Deletion](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`
- [Collision Buckets](https://github.com/gebi/cowmap/blob/main/cowmap.go): `cowmap.go`

### Other components

- **Client**

## Connections

- Client → Map Operations: invokes
- Map Operations → Hash Strategy: hashes keys
- Map Operations → Snapshot Reader: dispatches reads
- Map Operations → Snapshot Iterator: dispatches iteration
- Map Operations → CAS Writers: dispatches writes
- Snapshot Reader → Shard Roots: loads roots
- Snapshot Reader → Bitmap Trie Nodes: traverses trie
- Snapshot Reader → Collision Buckets: scans buckets
- Snapshot Iterator → Shard Roots: loads snapshots
- Snapshot Iterator → Bitmap Trie Nodes: iterates nodes
- Snapshot Iterator → Collision Buckets: iterates buckets
- CAS Writers → Shard Roots: CAS publishes
- CAS Writers → Path Copying: inserts paths
- CAS Writers → Structural Deletion: deletes paths
- CAS Writers → CAS Backoff: retries with
- Path Copying → Bitmap Trie Nodes: clones paths
- Path Copying → Collision Buckets: creates buckets
- Structural Deletion → Bitmap Trie Nodes: prunes paths
- Structural Deletion → Collision Buckets: updates buckets
- Hash Strategy → Shard Roots: routes by hash
- Shard Roots → Bitmap Trie Nodes: stores immutable roots
