Skip to content

perf(clean): replace file-based mdfind cache with in-memory lookups#986

Merged
tw93 merged 1 commit into
tw93:mainfrom
sebastianbreguel:perf/mdfind-inmemory-cache
May 28, 2026
Merged

perf(clean): replace file-based mdfind cache with in-memory lookups#986
tw93 merged 1 commit into
tw93:mainfrom
sebastianbreguel:perf/mdfind-inmemory-cache

Conversation

@sebastianbreguel
Copy link
Copy Markdown
Contributor

Summary

  • Replace grep -Fxq subprocess forks on a temp file with in-memory newline-delimited string variables checked via case glob matching (bash 3.2 compatible, zero forks per lookup)
  • Unify three separate mdfind caches (is_bundle_orphaned, is_claude_vm_bundle_orphaned, _system_service_app_exists) into one shared store, so a bundle resolved in one phase is not re-queried in another
  • Add mdfind caching to _container_stub_app_exists which previously had none

Benchmark

Isolated cache lookup (median of 5 runs, bash 3.2, Apple Silicon):

Bundle IDs OLD (grep) NEW (case) Speedup
50 139 ms 15 ms 9.3x
100 276 ms 18 ms 15.3x
200 539 ms 33 ms 16.3x
400 1,244 ms 86 ms 14.5x

End-to-end mo clean --dry-run: no perceptible difference (~41s both). Cache lookups are ~1% of total runtime; the win is fewer fork/exec calls and shared cache across scan phases.

Test plan

  • MOLE_TEST_NO_AUTH=1 bats tests/clean_apps.bats (25/25)
  • MOLE_TEST_NO_AUTH=1 bats tests/uninstall_safety.bats (13/13)
  • MOLE_TEST_NO_AUTH=1 bats tests/bundle_resolver.bats (11/11)
  • bash -n on all lib/clean/*.sh

The orphan scan forked grep twice per candidate to check the mdfind
cache file. On machines with 200+ items in ~/Library/Caches this added
400+ subprocess forks purely for cache hits.

Replace the temp-file cache with newline-delimited bash variables
checked via case glob matching (zero forks, bash 3.2 compatible).
Unify three separate caches into one shared store so bundle IDs
resolved during orphan scanning are not re-queried during system
service or container stub scanning. Add caching to
_container_stub_app_exists which previously had none.

Isolated benchmark: ~15x faster cache lookups (539ms → 33ms for 200
bundle IDs). End-to-end mo clean impact is marginal (~1% of total
runtime) since mdfind queries and filesystem traversal dominate.
@sebastianbreguel sebastianbreguel requested a review from tw93 as a code owner May 27, 2026 00:44
@tw93 tw93 merged commit 48df357 into tw93:main May 28, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants