feat: affinity model for locality-preserving HAMT keys (RFC 0002)#61
Merged
Conversation
… 0002) Introduces AffinityKey(parentID, fileID) = SHA256(parentID)[:4] + SHA256(fileID)[4:] so files sharing the same parent directory share the top 3 routing levels of the trie. This collapses incremental-backup metadata rewrites from O(N·depth) to O(depth) for a flat directory of N changed files. - hamt: new Insert/Lookup/Delete signatures accept parentID alongside fileID; LeafEntry gains a PathKey field so buildNode can split leaves without re-deriving routing keys; LookupByFileID added as an O(N) fallback for path resolution - engine: BackupManager.parentIndex tracks fileID→parentID during scan so lookupMetaByFileID can resolve AffinityKey lookups; falls back to LookupByFileID for entries not seen in the current scan (incremental backup case) - core: Snapshot.HAMTVersion=2 tags new snapshots; LeafEntry.PathKey stored for correct leaf-split routing of affinity-keyed entries
a4c4dba to
a739c74
Compare
a739c74 to
0ce24dc
Compare
rmanibus
added a commit
that referenced
this pull request
Mar 10, 2026
There was a problem hiding this comment.
Pull request overview
Implements RFC 0002 by switching HAMT routing from file-only hashing to affinity-aware keys so siblings share upper trie levels, reducing incremental metadata churn, plus adds benchmarks and documentation to validate locality effects.
Changes:
- Introduces
AffinityKey(parentID, fileID)routing and updates HAMT/engine call sites to pass parent context. - Adds end-to-end and unit/benchmark coverage (script + Go test/bench) to measure node-write reduction.
- Updates snapshot/model metadata to record HAMT versioning and stores per-leaf
PathKeyfor compatibility/operations.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| scripts/benchmark/affinity.sh | Adds an E2E benchmark script that compares clustered vs scattered incremental updates by counting node/* objects written. |
| rfcs/0002-affinity-model.md | Adds the RFC describing the affinity key scheme and intended versioning/migration behavior. |
| pkg/store/pack.go | Adds debug logging around pack buffering and catalog flush statistics. |
| internal/hamt/hamt.go | Implements affinity routing, extends Tree API (Insert/Lookup/Delete), and adds a walk-based lookup fallback. |
| internal/hamt/hamt_test.go | Updates HAMT unit tests for the new Tree API signature. |
| internal/hamt/affinity_bench_test.go | Adds a proof-style test and benchmarks comparing affinity vs legacy routing costs. |
| internal/engine/backup_scan.go | Wires affinity-aware lookups/inserts/deletes through scan/incremental paths and adds parent indexing for lookups. |
| internal/engine/backup_upload.go | Passes parent context into HAMT inserts during upload. |
| internal/engine/backup.go | Tracks parent index and stamps snapshots with HAMTVersion: 2. |
| internal/core/models.go | Extends LeafEntry with PathKey and Snapshot with HAMTVersion. |
| internal/engine/backup_test.go | Updates backup tests to perform affinity-aware lookups (parentID + fileID). |
| internal/engine/prune_test.go | Updates prune test HAMT inserts for new API. |
| internal/engine/diff_test.go | Updates diff test HAMT inserts for new API. |
| internal/engine/check_test.go | Updates check tests’ HAMT inserts for new API. |
| docs/affinity-benchmark.md | Adds documented benchmark results and interpretation/trade-offs. |
Comments suppressed due to low confidence (1)
internal/engine/backup_scan.go:152
detectChangenow doesLookup(oldRoot, primaryParentID(meta), meta.FileID). For repositories created before RFC 0002,oldRootis keyed by the legacy routing (hash(fileID) only), so this lookup will miss and treat previously-backed-up files as new/changed. This needs to branch on the previous snapshot’sHAMTVersion(default legacy when missing) and either use legacy routing (e.g.,parentID=fileID) or migrate the old HAMT before scanning.
func (bm *BackupManager) detectChange(oldRoot string, meta *core.FileMeta) (changed bool, oldRef string, err error) {
oldRef, err = bm.tree.Lookup(oldRoot, primaryParentID(meta), meta.FileID)
if err != nil {
return false, "", fmt.Errorf("hamt lookup: %w", err)
}
if oldRef == "" {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implement RFC 0002 by introducing affinity-aware HAMT routing keys so files sharing a parent directory share top trie levels, reducing incremental metadata churn.
What Changes
Key Idea
AffinityKey(parentID, fileID) = SHA256(parentID)[:4] + SHA256(fileID)[4:]This keeps sibling files close in the trie so incremental updates in flat directories reduce rewrites from roughly
O(N*depth)towardO(depth).Files of Interest
internal/hamt/hamt.gointernal/hamt/affinity_bench_test.gointernal/engine/backup_scan.gointernal/engine/backup.goscripts/benchmark/affinity.shdocs/affinity-benchmark.mdrfcs/0002-affinity-model.mdTracking
rfcs/0002-affinity-model.md