Skip to content

Commit

Permalink
Add very small MRU cache for path+anchor->string
Browse files Browse the repository at this point in the history
Although commit a41f849 greatly optimized fetching/massaging Scribble
HTML, and led me to discard a cache that I'd briefly added, I think an
MRU cache is still worthwhile for eldoc purposes. People may move
point among a small set of identifiers for which we could get cache
hits, thereby avoiding redoing recent work.
  • Loading branch information
greghendershott committed Sep 23, 2024
1 parent 7ede89c commit 937e2bc
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions racket-scribble-anchor.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
;; SPDX-License-Identifier: GPL-3.0-or-later

(require 'cl-macs)
(require 'ring)
(require 'seq)
(require 'shr)
(require 'racket-back-end)
Expand All @@ -31,11 +32,29 @@
(setq buffer-read-only t)
(current-buffer))))))

(defvar racket--path+anchor-ring (make-ring 16)
"A small MRU cache of the N most recent strings.
Each ring item is (cons (cons path anchor) str).")

(defun racket--path+anchor->string (path anchor)
"A wrapper for `racket--scribble-path+anchor-insert'."
(with-temp-buffer
(racket--scribble-path+anchor-insert path anchor)
(buffer-string)))
"A wrapper for `racket--scribble-path+anchor-insert'.
Uses `racket--path+anchor-cache'."
(pcase (seq-some (lambda (item)
(and (equal (car item) (cons path anchor))
item))
(ring-elements racket--path+anchor-ring))
((and `(,_path+anchor . ,str) item)
;; Re-insert as newest.
(ring-remove+insert+extend racket--path+anchor-ring item)
str)
(_
(let* ((str (with-temp-buffer
(racket--scribble-path+anchor-insert path anchor)
(buffer-string)))
(item (cons (cons path anchor) str)))
;; Insert as newest; oldest discarded when ring full.
(ring-insert racket--path+anchor-ring item)
str))))

(defun racket--scribble-path+anchor-insert (path anchor)
(let* ((tramp-verbose 2) ;avoid excessive tramp messages
Expand Down

0 comments on commit 937e2bc

Please sign in to comment.