Skip to content

Performance Issue: Cache Cleanup Blocks Main Thread in Large Vaults #396

@OneChirpZ

Description

@OneChirpZ

Problem

In large vaults (thousands of images), the automatic cache cleanup causes noticeable UI freezing/stuttering. Cleanup takes 5+ seconds and blocks the main thread, making Obsidian unresponsive during the operation.

Steps to Reproduce

  1. Use a vault with 1000+ images across multiple notes
  2. Enable the image alignment feature (creates cache entries)
  3. Set Image alignment cache cleanup interval to any value > 0 (e.g., 60000ms)
  4. Wait for automatic cleanup to trigger
  5. Try interacting with the Obsidian UI during cleanup

Expected

Cleanup should be fast enough to avoid input delays, or at least not block the main thread.

Actual

Console logs:

Image
Cache cleanup time: 8075ms
[Violation] Handling of 'wheel' input event was delayed for 5143 ms due to main thread being busy.

During cleanup, the UI is fully unresponsive for 5–8 seconds (typing/scrolling/clicking all blocked).

Root Cause

cleanCache() in src/ImageAlignmentManager.ts:636-691 uses an O(n²) nested-loop approach, repeatedly scanning vault files:

for (const notePath in this.cache) {
  for (const imageHash in this.cache[notePath]) {
    const allFiles = this.app.vault.getFiles(); // called repeatedly
    for (const file of allFiles) {              // iterates all vault files
      const currentImageHash = this.getImageHash(notePath, file.path);
      // ... hash comparison
    }
  }
}

Performance Notes

Example vault:

  • 100 notes with cached alignments
  • 10 cached images per note
  • 1000 image files

Total work ≈ 100 × 10 × 1000 = 1,000,000 hash calculations, run synchronously on the main thread, which causes the freezes.

Feature Request: Manual Cache Cleanup Button

Please add a manual cache cleanup button in the plugin settings tab. This allows users to:

  1. Disable automatic cleanup (set interval to 0 / Never)
  2. Manually trigger cleanup when needed (e.g., after deleting many images)
  3. Avoid periodic UI freezes in large vaults

Suggested Implementation

Add a button in src/ImageConverterSettings.ts:

Enhancement: "Never" Option for Cleanup Interval

Add a "Never (manual only)" option:

Current Workaround

Users can set imageAlignmentCacheCleanupInterval to 0, but there is no manual cleanup. To clean the cache now, users must:

  1. Change the interval to a non-zero value
  2. Wait for cleanup to trigger
  3. Change it back to 0 (and reload the plugin)

This is cumbersome and discourages cache cleanup entirely.

Environment

  • Obsidian version: Latest
  • Plugin version: 1.4.1
  • OS: macOS / Windows / Linux
  • Vault size: 3000+ images
  • Impact: Severe — Obsidian becomes unusable for 5–8 seconds during cleanup

Related Code

  • src/ImageAlignmentManager.ts:636-691cleanCache()
  • src/ImageAlignmentManager.ts:815-828scheduleCacheCleanup()
  • src/ImageAlignmentManager.ts:72 — initialization

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions