From 937e2bcea2d94747724e13a013bed34e381aa1ba Mon Sep 17 00:00:00 2001 From: Greg Hendershott Date: Mon, 23 Sep 2024 15:11:42 -0400 Subject: [PATCH] Add very small MRU cache for path+anchor->string 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. --- racket-scribble-anchor.el | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/racket-scribble-anchor.el b/racket-scribble-anchor.el index 5e90a901..9c928c82 100644 --- a/racket-scribble-anchor.el +++ b/racket-scribble-anchor.el @@ -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) @@ -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