Skip to content

Commit 89cd8df

Browse files
committed
Make the delete-file advice cheap, and purge the right project
The advice runs for every file Emacs deletes. It looked for the project of the current buffer, so a file deleted from a buffer in another project (or in none) stayed in its project's file list, and a file deleted from inside a project was looked up in that project's list even when it lived somewhere else entirely. The projects are now found from the file name, by comparing it against the roots that have a file list in memory, which also makes a deletion outside every cached project cost next to nothing. Each purge also wrote the whole persistent cache out synchronously, so deleting a few hundred files serialized the list a few hundred times. The advice now leaves the write to the idle flush that `projectile-cache-current-file' already uses, and pending flushes are written when Emacs exits so they are not lost. A flush no longer writes nil over the cache file of a project whose list has left memory.
1 parent fe8568a commit 89cd8df

3 files changed

Lines changed: 230 additions & 27 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
### Changes
2525

26+
- [#2206](https://github.com/bbatsov/projectile/pull/2206): Deleting files inside a project with a persistent cache no longer writes the cache file once per deleted file. The write waits until Emacs is idle, and pending cache writes are flushed when Emacs exits.
2627
- [#2206](https://github.com/bbatsov/projectile/pull/2206): `projectile-find-test-file` is much faster on large projects: the project type and its test conventions are looked up once, not once per file.
2728
- [#2206](https://github.com/bbatsov/projectile/pull/2206): Listing the known projects is faster with `projectile-ignored-projects` set: its entries are symlink-resolved once per listing, not once per project.
2829
- [#2206](https://github.com/bbatsov/projectile/pull/2206): The search and replace reviewers scan a large file with many matches much faster: line numbers are counted from the previous match instead of from the top of the file.
@@ -41,6 +42,7 @@
4142

4243
### Bugs fixed
4344

45+
- [#2206](https://github.com/bbatsov/projectile/pull/2206): Deleting a file removes it from the file cache of the project it belongs to, not from the cache of the project the current buffer happens to be in.
4446
- [#2206](https://github.com/bbatsov/projectile/pull/2206): The search reviewer now shows what `rg` reported when it fails (a pattern it rejects, an unreadable directory) instead of an empty "No matches" buffer.
4547
- [#2204](https://github.com/bbatsov/projectile/pull/2204): On an Emacs built without native JSON, `projectile-search-review` and `projectile-todos` fall back to the Emacs Lisp scanner instead of running `rg` and reporting no matches.
4648
- [#2204](https://github.com/bbatsov/projectile/pull/2204): `projectile-session-switch-to-buffer`, `projectile-find-file-in-projects` and `projectile-find-file-in-directory` advertise the completion category their candidates really are (`buffer` and `file`), so Marginalia and Embark stop treating them as files relative to the project root.

‎projectile.el‎

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,11 +1185,7 @@ into a single delayed disk write per project.")
11851185
;; recreating the cache file we just deleted with nil contents.
11861186
(projectile--register-project-cache-cleanup
11871187
'projectile--pending-cache-flush-timers
1188-
(lambda (project-root)
1189-
(when-let* ((timer (gethash project-root
1190-
projectile--pending-cache-flush-timers)))
1191-
(cancel-timer timer)
1192-
(remhash project-root projectile--pending-cache-flush-timers))))
1188+
#'projectile--cancel-cache-flush)
11931189

11941190
;; Deliberately a plain defvar rather than a `projectile-define-project-cache':
11951191
;; the descriptors in this table are live OS resources, so the test sandbox
@@ -2087,20 +2083,38 @@ The cache is created both in memory and on the hard drive."
20872083
"Remove file from cache: "
20882084
(projectile-current-project-files)
20892085
:caller 'projectile-read-file)))
2090-
(let* ((project-root (projectile-project-root))
2091-
(project-cache (gethash project-root projectile-projects-cache)))
2092-
(if (projectile-file-cached-p file project-root)
2093-
(let ((new-cache (remove file project-cache)))
2094-
(puthash project-root new-cache projectile-projects-cache)
2086+
(let ((project-root (projectile-project-root)))
2087+
(if (projectile--purge-file-from-cache file project-root)
2088+
(progn
2089+
;; The command writes the cache out there and then; whatever
2090+
;; `projectile--purge-file-from-cache' scheduled is redundant.
20952091
(when (projectile-persistent-cache-p)
2096-
(projectile-serialize new-cache (projectile-project-cache-file project-root)))
2097-
;; Re-derive watches from the shrunken cache, otherwise the purged
2098-
;; file's directory keeps being watched and a later create-event
2099-
;; there re-adds the entries we just removed.
2100-
(projectile--maybe-watch-project project-root new-cache)
2092+
(projectile--cancel-cache-flush project-root)
2093+
(projectile-serialize (gethash project-root projectile-projects-cache)
2094+
(projectile-project-cache-file project-root)))
21012095
(projectile--message "%s removed from cache" file))
21022096
(user-error "%s is not in the cache" file))))
21032097

2098+
(defun projectile--purge-file-from-cache (file project-root)
2099+
"Remove FILE from PROJECT-ROOT's cached file list.
2100+
FILE is relative to PROJECT-ROOT. Returns non-nil when it was listed.
2101+
2102+
A persistent cache is written out once Emacs is idle rather than here:
2103+
this runs for every file Emacs deletes inside a cached project, and a
2104+
command deleting a few hundred of them would otherwise serialize the
2105+
whole list a few hundred times."
2106+
(let ((project-cache (gethash project-root projectile-projects-cache)))
2107+
(when (member file project-cache)
2108+
(let ((new-cache (remove file project-cache)))
2109+
(puthash project-root new-cache projectile-projects-cache)
2110+
(when (projectile-persistent-cache-p)
2111+
(projectile--schedule-cache-flush project-root))
2112+
;; Re-derive watches from the shrunken cache, otherwise the purged
2113+
;; file's directory keeps being watched and a later create-event
2114+
;; there re-adds the entries we just removed.
2115+
(projectile--maybe-watch-project project-root new-cache)
2116+
t))))
2117+
21042118
;;;###autoload
21052119
(defun projectile-purge-dir-from-cache (dir)
21062120
"Purge DIR from the cache of the current project."
@@ -2124,21 +2138,44 @@ The cache is created both in memory and on the hard drive."
21242138
"Check if FILE is already in PROJECT cache."
21252139
(member file (gethash project projectile-projects-cache)))
21262140

2141+
(defun projectile--cancel-cache-flush (project)
2142+
"Cancel the pending idle-time write of PROJECT's cache, if there is one."
2143+
(when-let* ((timer (gethash project projectile--pending-cache-flush-timers)))
2144+
(cancel-timer timer)
2145+
(remhash project projectile--pending-cache-flush-timers)))
2146+
2147+
(defun projectile--flush-cache (project)
2148+
"Write PROJECT's in-memory file list to its cache file.
2149+
Does nothing when there is no list in memory any more (the entry
2150+
expired, say): writing nil would empty a cache file that is still good."
2151+
(let ((files (gethash project projectile-projects-cache 'missing)))
2152+
(unless (eq files 'missing)
2153+
(projectile-serialize files (projectile-project-cache-file project)))))
2154+
2155+
(defun projectile--flush-pending-caches ()
2156+
"Write out every cache that is waiting for Emacs to be idle.
2157+
On `kill-emacs-hook', so quitting before the idle timer fires does not
2158+
leave the cache file without the latest additions and removals."
2159+
(dolist (project (hash-table-keys projectile--pending-cache-flush-timers))
2160+
(projectile--cancel-cache-flush project)
2161+
;; Nothing here may get in the way of Emacs exiting: not an error, and
2162+
;; not a remote host that has to be reconnected to first.
2163+
(when (or (not (file-remote-p project))
2164+
(file-remote-p project nil t))
2165+
(ignore-errors (projectile--flush-cache project)))))
2166+
21272167
(defun projectile--schedule-cache-flush (project)
21282168
"Arrange for PROJECT's in-memory cache to be serialized after Emacs is idle.
21292169
A pending flush for the same PROJECT is cancelled and rescheduled, so that
21302170
adding several files in quick succession only results in a single disk write,
21312171
and the write always uses the latest in-memory contents."
2132-
(when-let* ((existing (gethash project projectile--pending-cache-flush-timers)))
2133-
(cancel-timer existing))
2172+
(projectile--cancel-cache-flush project)
21342173
(puthash project
21352174
(run-with-idle-timer
21362175
30 nil
21372176
(lambda ()
21382177
(remhash project projectile--pending-cache-flush-timers)
2139-
(projectile-serialize
2140-
(gethash project projectile-projects-cache)
2141-
(projectile-project-cache-file project))))
2178+
(projectile--flush-cache project)))
21422179
projectile--pending-cache-flush-timers))
21432180

21442181

@@ -2922,14 +2959,39 @@ When called interactively, always re-scans; the automatic scan (see
29222959

29232960

29242961
(defun delete-file-projectile-remove-from-cache (filename &optional _trash)
2925-
(if (and projectile-enable-caching projectile-auto-update-cache (projectile-project-p))
2926-
(let* ((project-root (projectile-project-root))
2927-
(true-filename (file-truename filename))
2928-
(relative-filename (file-relative-name true-filename project-root)))
2929-
(if (projectile-file-cached-p relative-filename project-root)
2930-
(projectile-purge-file-from-cache relative-filename)))))
2962+
"Drop FILENAME from the file cache of every project that lists it.
2963+
Advice for `delete-file', so it runs for every file Emacs deletes,
2964+
temporary files and auto-saves included; anything not under a project
2965+
with a file list in memory costs a few string comparisons and nothing
2966+
else.
2967+
2968+
The projects are found from FILENAME, not from the current buffer: the
2969+
buffer deleting a file is often not in that file's project at all."
2970+
(when (and projectile-enable-caching
2971+
projectile-auto-update-cache
2972+
(not (hash-table-empty-p projectile-projects-cache)))
2973+
(let* ((file (expand-file-name filename))
2974+
;; Roots are symlink-resolved, so the directory has to be as well
2975+
;; (though never over TRAMP). The last component is left alone: a
2976+
;; symlink is listed under its own name, not its target's.
2977+
;; This is advice on every deletion in Emacs, so a directory that
2978+
;; cannot be resolved must not get in the way of one.
2979+
(file (or (and (not (file-remote-p file))
2980+
(ignore-errors
2981+
(expand-file-name
2982+
(file-name-nondirectory file)
2983+
(file-truename (file-name-directory file)))))
2984+
file)))
2985+
(dolist (root (hash-table-keys projectile-projects-cache))
2986+
(when (or (string-prefix-p root file)
2987+
;; the same directory in another case, where that is so
2988+
(and (string-prefix-p root file t)
2989+
(not (file-remote-p root))
2990+
(file-name-case-insensitive-p root)))
2991+
(projectile--purge-file-from-cache
2992+
(projectile--project-relative-name file root) root))))))
2993+
29312994

2932-
29332995
;;; Project root related utilities
29342996
(defun projectile-parent (path)
29352997
"Return the parent directory of PATH.
@@ -18078,6 +18140,7 @@ Otherwise behave as if called interactively.
1807818140
(add-hook 'dired-before-readin-hook #'projectile-track-known-projects-find-file-hook t)
1807918141
(add-hook 'dired-before-readin-hook #'projectile--maybe-run-project-changed-functions t)
1808018142
(add-hook 'kill-emacs-hook #'projectile--frecency-save)
18143+
(add-hook 'kill-emacs-hook #'projectile--flush-pending-caches)
1808118144
(when projectile-dynamic-mode-line
1808218145
(add-hook 'window-configuration-change-hook #'projectile-update-mode-line-on-window-change))
1808318146
(add-hook 'kill-emacs-hook #'projectile--teardown-all-watches)
@@ -18094,6 +18157,7 @@ Otherwise behave as if called interactively.
1809418157
(remove-hook 'dired-before-readin-hook #'projectile--maybe-run-project-changed-functions)
1809518158
(projectile--frecency-save)
1809618159
(remove-hook 'kill-emacs-hook #'projectile--frecency-save)
18160+
(remove-hook 'kill-emacs-hook #'projectile--flush-pending-caches)
1809718161
(remove-hook 'window-configuration-change-hook #'projectile-update-mode-line-on-window-change)
1809818162
(remove-hook 'kill-emacs-hook #'projectile--teardown-all-watches)
1809918163
(projectile--teardown-all-watches)

‎test/projectile-core-test.el‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,143 @@
187187
(expect 'projectile--maybe-watch-project
188188
:to-have-been-called-with "/project/" '("foo.el")))))
189189

190+
(describe "the delete-file advice"
191+
:var (projectile-projects-cache projectile--pending-cache-flush-timers)
192+
(before-each
193+
(setq projectile-projects-cache (make-hash-table :test 'equal)
194+
projectile--pending-cache-flush-timers (make-hash-table :test 'equal))
195+
(spy-on 'file-truename :and-call-fake #'identity)
196+
(spy-on 'projectile--maybe-watch-project)
197+
(spy-on 'projectile-serialize)
198+
(spy-on 'projectile--schedule-cache-flush))
199+
200+
(it "purges the file from its own project, whatever buffer is current"
201+
(let ((projectile-enable-caching t)
202+
(projectile-auto-update-cache t)
203+
(default-directory "/elsewhere/"))
204+
(puthash "/project/" '("foo.el" "src/bar.el") projectile-projects-cache)
205+
(puthash "/other/" '("src/bar.el") projectile-projects-cache)
206+
(spy-on 'projectile-project-root)
207+
(delete-file-projectile-remove-from-cache "/project/src/bar.el")
208+
(expect (gethash "/project/" projectile-projects-cache) :to-equal '("foo.el"))
209+
(expect (gethash "/other/" projectile-projects-cache) :to-equal '("src/bar.el"))
210+
(expect 'projectile-project-root :not :to-have-been-called)))
211+
212+
(it "purges the file from an enclosing project that lists it too"
213+
(let ((projectile-enable-caching t)
214+
(projectile-auto-update-cache t))
215+
(puthash "/mono/" '("app/a.el" "README") projectile-projects-cache)
216+
(puthash "/mono/app/" '("a.el") projectile-projects-cache)
217+
(delete-file-projectile-remove-from-cache "/mono/app/a.el")
218+
(expect (gethash "/mono/" projectile-projects-cache) :to-equal '("README"))
219+
(expect (gethash "/mono/app/" projectile-projects-cache) :to-be nil)))
220+
221+
(it "leaves the caches alone for a file outside every cached project"
222+
(let ((projectile-enable-caching t)
223+
(projectile-auto-update-cache t))
224+
(puthash "/project/" '("foo.el") projectile-projects-cache)
225+
(delete-file-projectile-remove-from-cache "/tmp/scratch123")
226+
(expect (gethash "/project/" projectile-projects-cache) :to-equal '("foo.el"))
227+
(expect 'projectile--maybe-watch-project :not :to-have-been-called)))
228+
229+
(it "does not resolve symlinks when nothing is cached"
230+
(let ((projectile-enable-caching t)
231+
(projectile-auto-update-cache t))
232+
(delete-file-projectile-remove-from-cache "/tmp/scratch123")
233+
(expect 'file-truename :not :to-have-been-called)))
234+
235+
(it "does nothing when the cache is not kept up to date"
236+
(let ((projectile-enable-caching t)
237+
(projectile-auto-update-cache nil))
238+
(puthash "/project/" '("foo.el") projectile-projects-cache)
239+
(delete-file-projectile-remove-from-cache "/project/foo.el")
240+
(expect (gethash "/project/" projectile-projects-cache) :to-equal '("foo.el"))))
241+
242+
(it "leaves the write of a persistent cache for when Emacs is idle"
243+
(let ((projectile-enable-caching 'persistent)
244+
(projectile-auto-update-cache t))
245+
(puthash "/project/" '("foo.el" "bar.el") projectile-projects-cache)
246+
(delete-file-projectile-remove-from-cache "/project/foo.el")
247+
(expect 'projectile-serialize :not :to-have-been-called)
248+
(expect 'projectile--schedule-cache-flush
249+
:to-have-been-called-with "/project/"))))
250+
251+
(describe "the delete-file advice and awkward file names"
252+
:var (projectile-projects-cache)
253+
(before-each
254+
(setq projectile-projects-cache (make-hash-table :test 'equal))
255+
(spy-on 'projectile--maybe-watch-project)
256+
(puthash "/Project/" '("foo.el") projectile-projects-cache))
257+
258+
(it "does not signal when the directory cannot be resolved"
259+
(let ((projectile-enable-caching t)
260+
(projectile-auto-update-cache t))
261+
(spy-on 'file-truename :and-throw-error 'file-error)
262+
(expect (delete-file-projectile-remove-from-cache "/Project/foo.el")
263+
:not :to-throw)
264+
(expect (gethash "/Project/" projectile-projects-cache) :to-be nil)))
265+
266+
(it "matches a root spelled in another case on a case-insensitive filesystem"
267+
(let ((projectile-enable-caching t)
268+
(projectile-auto-update-cache t))
269+
(spy-on 'file-truename :and-call-fake #'identity)
270+
(spy-on 'file-name-case-insensitive-p :and-return-value t)
271+
(delete-file-projectile-remove-from-cache "/project/foo.el")
272+
(expect (gethash "/Project/" projectile-projects-cache) :to-be nil)))
273+
274+
(it "does not on a case-sensitive one"
275+
(let ((projectile-enable-caching t)
276+
(projectile-auto-update-cache t))
277+
(spy-on 'file-truename :and-call-fake #'identity)
278+
(spy-on 'file-name-case-insensitive-p :and-return-value nil)
279+
(delete-file-projectile-remove-from-cache "/project/foo.el")
280+
(expect (gethash "/Project/" projectile-projects-cache)
281+
:to-equal '("foo.el")))))
282+
283+
(describe "projectile--flush-cache"
284+
(it "does not write nil over the cache file of a project no longer in memory"
285+
(let ((projectile-projects-cache (make-hash-table :test 'equal)))
286+
(spy-on 'projectile-serialize)
287+
(projectile--flush-cache "/gone/")
288+
(expect 'projectile-serialize :not :to-have-been-called)))
289+
290+
(it "writes an empty list for a project whose last file was removed"
291+
(let ((projectile-projects-cache (make-hash-table :test 'equal)))
292+
(puthash "/project/" nil projectile-projects-cache)
293+
(spy-on 'projectile-serialize)
294+
(spy-on 'projectile-project-cache-file :and-return-value "/tmp/cache.eld")
295+
(projectile--flush-cache "/project/")
296+
(expect 'projectile-serialize
297+
:to-have-been-called-with nil "/tmp/cache.eld"))))
298+
299+
(describe "projectile--flush-pending-caches"
300+
(it "writes out every cache with a pending idle write and cancels the timers"
301+
(let ((projectile-projects-cache (make-hash-table :test 'equal))
302+
(projectile--pending-cache-flush-timers (make-hash-table :test 'equal))
303+
(timer (run-with-idle-timer 3600 nil #'ignore)))
304+
(puthash "/project/" '("foo.el") projectile-projects-cache)
305+
(puthash "/project/" timer projectile--pending-cache-flush-timers)
306+
(spy-on 'projectile-serialize)
307+
(spy-on 'projectile-project-cache-file :and-return-value "/tmp/cache.eld")
308+
(projectile--flush-pending-caches)
309+
(expect 'projectile-serialize
310+
:to-have-been-called-with '("foo.el") "/tmp/cache.eld")
311+
(expect (memq timer timer-idle-list) :to-be nil)
312+
(expect (hash-table-count projectile--pending-cache-flush-timers)
313+
:to-equal 0)))
314+
315+
(it "skips a remote project it would have to reconnect to"
316+
(let ((projectile-projects-cache (make-hash-table :test 'equal))
317+
(projectile--pending-cache-flush-timers (make-hash-table :test 'equal)))
318+
(puthash "/ssh:host:/project/" '("foo.el") projectile-projects-cache)
319+
(puthash "/ssh:host:/project/" (run-with-idle-timer 3600 nil #'ignore)
320+
projectile--pending-cache-flush-timers)
321+
(spy-on 'file-remote-p :and-call-fake
322+
(lambda (_file &optional _id connected) (not connected)))
323+
(spy-on 'projectile-serialize)
324+
(projectile--flush-pending-caches)
325+
(expect 'projectile-serialize :not :to-have-been-called))))
326+
190327
(describe "projectile-purge-dir-from-cache"
191328
(it "removes files under the directory from the in-memory cache"
192329
(let ((projectile-projects-cache (make-hash-table :test 'equal))

0 commit comments

Comments
 (0)