Skip to content

Commit

Permalink
Improve cache update for deleted lines
Browse files Browse the repository at this point in the history
If multiple adjacent lines have been deleted, drop wads and adjust wads in one
go instead of doing it for each delete line individually.

For certain buffer contents such as a block comment that spans thousands of
lines, this change reduces the cache update time from many seconds to a few
dozen (or at worst at few hundred) milliseconds.
  • Loading branch information
scymtym committed Jul 1, 2024
1 parent 8d7c96d commit 2e6e4fc
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions code/cache.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,11 @@
do (process-next-wad cache line-number))
(adjust-worklist-and-suffix cache 1))

(defun handle-deleted-line (cache line-number)
(loop until (next-wad-is-beyond-line-p cache line-number)
do (process-next-wad cache line-number))
(adjust-worklist-and-suffix cache -1))
(defun handle-deleted-lines (cache line-number line-count)
(loop :for line :from (- line-number line-count 1) :to line-number
:do (loop :until (next-wad-is-beyond-line-p cache line)
:do (process-next-wad cache line)))
(adjust-worklist-and-suffix cache (- line-count)))

;;; Take into account modifications to the buffer by destroying the
;;; parts of the cache that are no longer valid, while keeping parse
Expand All @@ -304,20 +305,25 @@
(setf cache-initialized-p t)
(ensure-update-initialized cache line-counter)))
;; Line deletion
(delete-cache-line ()
(flx:delete* lines line-counter)
(flx:delete* cluffer-lines line-counter)
(handle-deleted-line cache line-counter))
(delete-cache-lines (start-line-number end-line-number)
(let ((count (- end-line-number start-line-number)))
(loop :repeat count
:do (flx:delete* lines start-line-number)
(flx:delete* cluffer-lines start-line-number))
(handle-deleted-lines cache (1- end-line-number) count)))
(remove-deleted-lines (line)
;; Look at cache lines starting at LINE-COUNTER. Delete
;; all cache lines that do not have LINE as their
;; associated cluffer line. Those lines correspond to
;; deleted lines between the previously processed line
;; and LINE.
(loop for cluffer-line
= (flx:element* cluffer-lines line-counter)
until (eq line cluffer-line)
do (delete-cache-line)))
(loop :for end-line-number :from line-counter
:for cluffer-line
= (flx:element* cluffer-lines end-line-number)
:until (eq line cluffer-line)
:finally (when (< line-counter end-line-number)
(delete-cache-lines
line-counter end-line-number))))
;; Handlers for Cluffer's update protocol events.
(skip (count)
(incf line-counter count))
Expand All @@ -344,10 +350,11 @@
(time-stamp cache)
#'sync #'skip #'modify #'create))
;; Remove trailing cache lines after the last
;; skipped/modified/... cache line, that no longer correspond
;; to existing lines in the cluffer buffer.
(loop while (< line-counter (flx:nb-elements lines))
do (delete-cache-line))))
;; skipped/modified/... cache line, that no longer correspond to
;; existing lines in the cluffer buffer.
(let ((cache-line-count (flx:nb-elements lines)))
(when (< line-counter cache-line-count)
(delete-cache-lines line-counter cache-line-count)))))
(finish-scavenge cache))

(defmethod line-count ((cache cache))
Expand Down

0 comments on commit 2e6e4fc

Please sign in to comment.