Getǣl — Read-only macOS home directory disk usage cartography & APFS telemetry service

Normalize SQLite paths with integer `dir_id`/`parent_id` and remove redundant text indexes #4

Closed zip.mu.social
Closed opened by zip.mu.social

Problem #

Measured on a live home directory cache, path strings are heavily duplicated across tables and B-tree indexes:

  • directories indexes = 222.6 MiB:
    • idx_directories_depth ON directories(depth, path): 95.8 MiB (duplicates every full path string a second time)
    • sqlite_autoindex_directories_1 (path TEXT PRIMARY KEY): 65.3 MiB
    • idx_directories_parent ON directories(parent_path): 52.0 MiB (plus 36.4 MiB for the parent_path column itself)
    • idx_directories_name ON directories(name): 9.6 MiB (only used by find_bloat_hints for 8 known folder names)
  • top_files repeats full directory paths 4 times per row (~1.47 GiB before filtering):
    • path (287 MiB) + dir_path (244 MiB) + sqlite_autoindex_top_files_1 (496 MiB) + idx_top_files_dir (402 MiB), plus category stored as text (21.8 MiB) instead of a 1-byte integer enum tag.

Estimated Savings #

  • ~200+ MiB on directories + indexes, and ~5× smaller per-row footprint on top_files.

Proposed Fix #

  1. Add id INTEGER PRIMARY KEY (rowid alias) to directories and replace parent_path TEXT with parent_id INTEGER indexed by idx_directories_parent(parent_id) (shrinks parent_path + index from ~88 MiB to ~6 MiB).
  2. Drop idx_directories_depth(depth, path) (−95.8 MiB): fetch depth-bounded trees in get_tree using a WITH RECURSIVE CTE over the integer idx_directories_parent(parent_id) index (fast, exact, zero duplicated path strings).
  3. Replace idx_directories_name (−9.5 MiB) with a partial index covering only bloat folder names:
    CREATE INDEX idx_directories_bloat ON directories(name, subtree_physical_bytes DESC)
    WHERE name IN ('target', 'node_modules', '.venv', 'DerivedData', 'Caches', '.cache', 'ollama', 'huggingface');
    
  4. In top_files, replace path TEXT PRIMARY KEY and dir_path TEXT with id INTEGER PRIMARY KEY, dir_id INTEGER NOT NULL, name TEXT NOT NULL, and category INTEGER NOT NULL (reconstructing full path via join to directories at query time).
  5. Verify all cache unit tests, proptests, and e2e tests pass after schema migration.
Sign up or Sign in to add to the discussion.