This is great! Indeed, it works perfectly now.
I think using the margins is just much more elegant.

Although, I might have read about this behavior somewhere,
obviously, it was not clear to me that `erase-buffer` does not
remove the overlays. Maybe a warning/reminder about this could
 be added in the 'erase-buffer' docstring? What do you think?
I would be happy to create a patch for that (if desired).

Anyway, thanks a lot again Eli, for having a look at it.
Obviously, I really appreciate it.


On Sat, 25 Feb 2023 at 11:51, Eli Zaretskii <eliz@gnu.org> wrote:
> From: dalanicolai <dalanicolai@gmail.com>
> Date: Fri, 24 Feb 2023 21:53:35 +0100
> Cc: emacs-devel@gnu.org
>
> B.t.w if someone want to have a look, I'll attach a smaller file here where
> I have just removed about 200 pages of the book text data, so that it is
> the file is much smaller, but there is still enough date to clearly show the
> 'undesired' behavior.
>
> So now, Emacs will not 'hang', but there will still be a clear difference in time it
> takes to update the buffer (between when window margins are displayed,
> and when they are not).

Your code overwhelms redisplay with an inconceivably huge number of
overlays that are left from the previous iteration.  This one-line
change makes the code work reasonably fast:

(defun baleen-update2 ()
  ;; (let ((query (minibuffer-contents)))
  ;;   (with-current-buffer (get-buffer-create "*baleen*")
  ;;     (erase-buffer)
  ;;     (baleen-render (baleen-filter test2 query)))))
  (let* ((query (minibuffer-contents))
         (query-length (length query)))
    (if (> (length previous-query) query-length)
        (with-current-buffer (get-buffer-create "*baleen*")
          (erase-buffer)
          (baleen-render (cdr (alist-get query baleen-results nil nil #'string=))))
      (let* ((parent-results (unless (< query-length 2)
                               (alist-get (substring query 0 -1) baleen-results nil nil #'string=)))
             (current-results (if parent-results
                                  (baleen-filter parent-results query)
                                (unless (string-empty-p query) (baleen-filter test2 query)))))
        (when current-results
          (with-current-buffer (get-buffer-create "*baleen*")
            (remove-overlays) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            (erase-buffer)
            (baleen-render current-results))
          (cl-pushnew (cons query current-results) baleen-results :test #'string= :key #'car))

        (setq previous-query query)))))