Annotation function being called every time a different candidate is selected? #1040
-
Hello Daniel, First, I'd like to thank your for your tremendous work and patience. I've ready through quite a few discussions here, and they have been particularly informative. I am using Consult for a personal project which requires annotations generated from a Web API call. When I use Vertico, which you might be aware of ^^, and enable the annotations, I find that switching annotated candidates back and forth is very slow, as though they were being regenerated at every movement, despite the fact that they are all already visible. I suppose this is because it is expected that the annotations can be dynamic. Here's a trivial example which associates numbers 1 to 10 to the corresponding title of the XKCD comic of the same number to illustrate this: (defun my/xkcd--get-title-from-url (comic-number)
(let* ((url-request-method "GET")
(response-buf (url-retrieve-synchronously
(format "https://xkcd.com/%s/info.0.json" comic-number)
t t 10))
(response-string (with-current-buffer response-buf
(goto-char (point-max))
(buffer-substring (pos-bol) (point)))))
(concat " " (alist-get 'safe_title (json-read-from-string response-string)))))
(defun my/consult-xkcd-from-url ()
(interactive)
(consult--read (mapcar #'number-to-string (number-sequence 1 10))
:annotate #'my/xkcd--get-title-from-url :sort nil))
(my/consult-xkcd-from-url) ;; `vertico-next' and `vertico-previous' are very slow.
(setq my/xkcd--cached-titles (mapcar (lambda (n)
(my/xkcd--get-title-from-url n))
(number-sequence 1 10)))
(defun my/xkcd--get-title-from-cache (comic-number)
(let ((number (1- (string-to-number comic-number))))
(nth number my/xkcd--cached-titles)))
(defun my/consult-xkcd-from-cache ()
(interactive)
(consult--read (mapcar #'number-to-string (number-sequence 1 10))
:annotate #'my/xkcd--get-title-from-cache :sort nil))
(my/consult-xkcd-from-cache) ;; `vertico-next' and `vertico-previous' are fast. What am I doing wrong? Do I have to cache the results myself in my own project (which is more complex) or is there a way to make sure the annotation function is called only once per candidate? Thanks a lot in advance for your help, and carry on with the great work, EV |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yes, Vertico loads annotations of all visible candidates on each redisplay. The expectation is that annotation functions are sufficiently fast. I've implemented caching in the Marginalia package, which helps there for some of the more expensive computations. In principle one could also implement annotation caching on the side of the completion UI, but this is not the route taken by the developments... |
Beta Was this translation helpful? Give feedback.
Yes, Vertico loads annotations of all visible candidates on each redisplay. The expectation is that annotation functions are sufficiently fast. I've implemented caching in the Marginalia package, which helps there for some of the more expensive computations.