[SeaDrive macOS 3.0.26] File Provider enumeration takes ~80 ms per entry on large libraries (missing IdMap(repo_id, path) index) – blocks file opening for minutes
Component: SeaDrive 3.0.26 for macOS — File Provider extension (com.seafile.seadrive.fprovider)
(Filed here because issues are disabled on haiwen/seadrive-gui and haiwen/seadrive-fuse, and the File Provider source is not public.)
Summary
With a large library (~600k entries), enumeration spends ~80 ms per directory entry in SQLite, because the duplicate check in seaf_woringset_manager_add_id_to_id_map is served by the single-column idmap_repoid_index and scans every IdMap row of the repo. enumerateItemsForRepo:startingAtPage:metadata: holds the workingset DB lock and one transaction for the whole directory, so a directory with ~31k entries blocks the DB for ~40 minutes. During that time opening (materializing) any dataless file waits for minutes, and the extension sits at ~100 % CPU.
Adding one compound index fixes it: 79 ms → 0.008 ms per lookup (~10,000×).
Environment
- macOS 26.5.2, SeaDrive 3.0.26
ws.db: ~290 MB,IdMap≈ 630k rows, one library holds ~602k of them (96 %); its largest directory has 31,491 direct children
Symptoms
SeaDrive File Providerat 95–98 % CPU for 17+ minutes;fileproviderd/suggestdalso busy- Opening a dataless 0.5 MB file took 17.6 s up to several minutes before any transfer started
- Thousands of log lines
workingset-mgr.c(416): The id <UUID> already in IdMap for path ... in repo ... sampleof the extension — virtually all samples on the serial queue:
DispatchQueue: fpx-instance (serial)
FPXEnumerator enumerateItemsFromPage:suggestedPageSize:reply:
FileProviderEnumerator enumerateItemsForObserver:startingAtPage:
FileProviderEnumerator enumerateItemsForRepo:startingAtPage:metadata:
seaf_woringset_manager_add_id_to_id_map
sqlite_get_string → sqlite3_step → sqlite3VdbeExec → sqlite3BtreeTableMovetoRoot cause (from the shipped binary)
seaf_woringset_manager_add_id_to_id_map (called via saveToIDMap for every dirent) runs:
SELECT identifier FROM IdMap WHERE domain_id=%Q AND parent=%Q AND repo_id=%Q AND path=%Q
-- if not found:
INSERT INTO IdMap (domain_id, identifier, parent, repo_id, path) VALUES (...)Existing indexes: idmap_id_index(identifier), idmap_parent_index(parent), idmap_repoid_index(repo_id), idmap_repoid_policy_index(repo_id, policy). There is no ANALYZE data (sqlite_stat1 absent), and SQLite picks:
SEARCH IdMap USING INDEX idmap_repoid_index (repo_id=?)→ O(rows in repo) per lookup, O(n·m) per enumerated directory. Measured on a copy of the real DB (200 random lookups, warm cache):
| plan | per lookup | 31,491-entry dir | |
|---|---|---|---|
| 3.0.26 | idmap_repoid_index (repo_id=?) |
79.1 ms | ~41 min |
| + index below | idmap_repoid_path_index (repo_id=? AND path=?) |
0.008 ms | ~0.25 s |
get_entry_by_path / get_identifier (... WHERE repo_id=%Q AND path=%Q, ... WHERE domain_id=%Q AND repo_id=%Q and path=%Q) have the same problem and benefit from the same index.
Proposed fix
In seaf_workingset_manager_init (next to the other CREATE INDEX IF NOT EXISTS):
CREATE INDEX IF NOT EXISTS idmap_repoid_path_index ON IdMap(repo_id, path);Safe for existing DBs (non-unique, no data change, ~1 s to build for 630k rows, +~80 MB file size). I applied exactly this to my live ws.db (with the extension stopped, backup taken, integrity_check ok): extension CPU dropped from ~97 % to ~0–5 % right after restart and opening dataless files is limited by network speed again (61 MB file in 6.5 s).
Further suggestions (not verified in source)
- Don't hold the workingset DB lock/transaction for an entire huge directory; honor
NSFileProviderPage/suggestedPageSizeand return pages promptly, so materialization is never blocked by an enumeration. - Skip re-inserting unchanged entries (the "already in IdMap" path) cheaply — e.g. preload the directory's existing
(parent, path)set once per directory instead of one query per dirent. - Consider running
ANALYZE/PRAGMA optimizeoccasionally so the planner has statistics. - Log duration / item count per enumeration so this is visible without
sample.
Source: haiwen/seafile