unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: dalanicolai <dalanicolai@gmail.com>
To: Emacs Devel <emacs-devel@gnu.org>
Subject: Question about (excessive?) ram usage when many overlays (with large vscroll)
Date: Tue, 26 Apr 2022 14:21:07 +0200	[thread overview]
Message-ID: <CACJP=3miu=+qrqC+nQ-1RhzaDxrNWXP5e3dgd3ARr5+KphQJAw@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 2199 bytes --]

I have a question about ram usage when using overlays.

So I have created `image-roll.el` for displaying documents/books (see here
<https://lists.gnu.org/archive/html/emacs-devel/2022-04/msg00975.html>).
However, I have just noticed that it uses a large amount of RAM when
viewing (or
trying to) pages in the back of 'large' books. But even if RAM usage still
looks
perfectly fine, Emacs crashes when trying to scroll to higher page numbers.

I have looked a little into it, and have found that it is a consequence of
using
large overlays. There is no problem when creating a buffer containing many
overlays, however, when trying to scroll to some overlay at the end of the
buffer,
Emacs will use huge amounts of RAM.

So I am wondering if this is 'necessary' behavior; and then why is it
necessary?
The issue occurs even when displaying the same 'empty' svg-image on each
overlay, but it occurs also when simply displaying some 'specified space' on
each overlay.

I have created a simple test file (attached to this mail) for reproducing
the
issue.From 'emacs -Q` you can simply load the file and do `M-x test`. Then
you
can type some number and press `C-c C-c` to jump to the page/overlay(number)
(use arrow up/down to scroll). The overlay width is set to 1200 pixels (via
a
global variable, so that it can easily be changed) and the width is set to
1.4
times that size (you can also comment out the 'specified space' overlay, and
uncomment the `insert-image` form). Over here, Emacs crashes when trying to
jump
to a page number of somewhere around 1750. When increasing the overlay size,
e.g. setting the width to (window-pixel-width), then RAM usage increases
much
faster, and Emacs starts to crash from jumping to page around 800.

So my question is, if it is really necessary that Emacs uses so much RAM
for just
displaying 'empty spaces'. And another question is why the maximum
`vscroll` is
(or seems to be) limited.

I have designed the book-roll like this, so that the scroll bar might be
used
for indicating/scrolling to the/some position in a document. However,
otherwise
I probably have to design it so that the buffer only contains two overlays
and
'simulate' the image-roll behavior.

[-- Attachment #1.2: Type: text/html, Size: 2417 bytes --]

[-- Attachment #2: test.el --]
[-- Type: text/x-emacs-lisp, Size: 2276 bytes --]

(require 'svg)

(setq test-page-width 1200)

(defun test-goto-page (page)
  (interactive
   (list (if current-prefix-arg
             (prefix-numeric-value current-prefix-arg)
           (read-number "Page: "))))
  (set-window-vscroll nil
                      (print (* (print page)
                                (+ (* 1.4 test-page-width)
                                   10)))
                      t))

(defun test-scroll-forward ()
  (interactive)
  (set-window-vscroll nil (+ (window-vscroll nil t) 50) t))

(defun test-scroll-backward ()
  (interactive)
  (set-window-vscroll nil (- (window-vscroll nil t) 50) t))

(define-derived-mode test-mode special-mode "Test")

(define-key test-mode-map (kbd "<down>") 'test-scroll-forward)
(define-key test-mode-map (kbd "<up>") 'test-scroll-backward)
(define-key test-mode-map (kbd "C-c C-c") 'test-goto-page)

(when (featurep 'evil)
  (evil-define-key 'motion test-mode-map "j" 'test-scroll-forward)
  (evil-define-key 'motion test-mode-map "k" 'test-scroll-backward)
  (evil-define-key 'motion test-mode-map "G" 'test-goto-page))

(defun test ()
  (interactive)
  (with-current-buffer (get-buffer-create "*test*")
    ;; (let* ((w (window-pixel-width))
    (let* ((w test-page-width)
           (h (* 1.4 w))
           (svg (svg-create w h)))

      ;; uncomment for `colored page'
      ;; (svg-rectangle svg 0 0 w h :fill "red")

      (let ((im (svg-image svg)))

        (dotimes (i 1600)

          ;; NOTE either insert `specified space' overlays
          (let ((start (point)))
            (insert " ")
            (let ((o (make-overlay start (point))))
              (overlay-put o 'display `(space . (:width (,w) :height (,h))))))

          ;; NOTE or insert an `empty' svg image
          ;; (insert-image im " ")
          (insert "\n")

          ;; for `visual clarity', insert a page separator overlay
          (let ((start (point)))
            (insert " ")
            (let ((o (make-overlay start (point))))
              (overlay-put o 'display `(space . (:width (,w) :height (,10))))
              (overlay-put o 'face `(:background "gray"))
              (insert "\n")))))
      (test-mode)
      (setq cursor-type nil)
      (switch-to-buffer (current-buffer))
      (goto-char (point-min)))))


             reply	other threads:[~2022-04-26 12:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-26 12:21 dalanicolai [this message]
2022-04-26 12:33 ` Question about (excessive?) ram usage when many overlays (with large vscroll) Eli Zaretskii
2022-04-26 13:24   ` Eli Zaretskii
2022-04-27 14:13     ` dalanicolai
2022-04-27 15:44       ` Eli Zaretskii
2022-04-27 17:13       ` Stefan Monnier
2022-04-27 17:18         ` Eli Zaretskii
2022-04-26 12:49 ` Po Lu
2022-04-27 14:01   ` dalanicolai
2022-04-28 11:56   ` dalanicolai
2022-04-28 12:06     ` Po Lu
2022-04-28 16:28       ` dalanicolai
2022-04-28 16:48         ` dalanicolai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACJP=3miu=+qrqC+nQ-1RhzaDxrNWXP5e3dgd3ARr5+KphQJAw@mail.gmail.com' \
    --to=dalanicolai@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).