all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Psionic K <psionik@positron.solutions>
To: help-gnu-emacs@gnu.org
Subject: Identifying sources of allocations in a toy Mandelbrot package
Date: Wed, 17 Jan 2024 21:39:04 +0900	[thread overview]
Message-ID: <CADQMGAQR3yvaJK5OdzcGkcy1VAjoMoTa3pSZjwYgGSbTzP0o_A@mail.gmail.com> (raw)

I was toying around with compilation speed trying to eliminate
allocations and discovered that my package was spending about 40% of
its runtime in GC  (observed in `gc-elapsed').  I didn't suspect the
situation could be this intense until turning on GC messages.

I wrapped the command's body in some temporary GC settings and then
found values I was comfortable with in terms of tradeoffs for
resulting resident memory consumption and time spent GC'ing.

By setting absurd values that wouldn't GC until the program finished,
I was able to reproduce freezes I had experienced in the past where
Emacs was evidently GC'ing forever.

All that was fun, but what I don't understand is where this package
manages to consume up to 7GB of memory, which Emacs will not readily
give back afterwards.  How can I identify causes of allocations until
I gain familiarity with which expressions typically cause them?  To my
naive eyes, there are no allocations in the hot section of the code,
but that is very untrue given the behavior.

I want to also remark that `gc-cons-percentage' does not seem to be a
percentage but a factor.  If it is a percentage, I don't understand
it's behavior because any value larger than 2.0 seems to lead Emacs to
using all of the available memory.

Package follows.  Enjoy, but as configured, Emacs will be hovering at
around 1.3GB after running:

    ;;; mandelbrot.el --- Mandelbrot -*- lexical-binding: t; -*-

    ;;; Commentary:

    ;; Render's Mandelbrot set.

    ;;; Code:

    (eval-when-compile (require 'cl-lib))

    ;;;###autoload
    (defun mandelbrot ()
      "Render a mandelbrot."
      (declare (speed 3))
      (interactive)
      (pop-to-buffer (get-buffer-create "*mandelbrot*"))
      (let ((gc-cons-threshold (* 800000 1024))
            (gc-cons-percentage 1.0))
        (let* ((cx) (cy) (zr) (zi)
               (inhibit-modification-hooks t)
               (w 1600) (h 1200) (d 256)
               (output (make-vector (* w h 3) 0))
               (idx 0)
               (x0 -2.5) (y0 -1.5) (dx 4.0) (dy 3.0)
               (dxp (/ dx w)) (dyp (/ dy h)))
          (fundamental-mode) (erase-buffer)
          (set-buffer-multibyte nil)
          (insert (format "P6\n%d %d\n255\n" w h))
          (dotimes (y h)
            (dotimes (x w)
              (setq cx (+ x0 (* dxp x))
                    cy (+ y0 (* dyp y))
                    zr 0
                    zi 0)
              (let ((v (cl-dotimes (v d d)
                         (if (> (+ (* zr zr) (* zi zi)) 4) (cl-return v)
                           (cl-psetq
                            zr (+ (* zr zr) (- (* zi zi)) cx)
                            zi (+ (* (* zr zi) 2) cy))))))
                ;; exponential 0.8 enhances contrast a bit
                (let ((out (floor (* 256 (expt (/ (float v) d) 0.8)))))
                  (aset output idx out)
                  (aset output (+ idx 1) out)
                  (aset output (+ idx 2) out)
                  (setq idx (+ idx 3))))))
          (insert (concat output))
          (image-mode))))

    (provide 'mandelbrot)
    ;;; mandelbrot.el ends here.



             reply	other threads:[~2024-01-17 12:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-17 12:39 Psionic K [this message]
2024-01-17 12:58 ` Identifying sources of allocations in a toy Mandelbrot package Eli Zaretskii
2024-01-17 13:25 ` Emanuel Berg
  -- strict thread matches above, loose matches on Subject: below --
2024-01-19  9:19 Psionic K
2024-01-19 15:33 ` Tomas Hlavaty
2024-01-20  3:14   ` Psionic K
2024-01-20  3:37     ` Psionic K
2024-01-20  7:29     ` Eli Zaretskii
2024-01-20  9:09     ` Tomas Hlavaty
2024-01-20 10:03       ` Psionic K
2024-01-20 10:31         ` Eli Zaretskii
2024-01-26 23:36         ` Tomas Hlavaty
2024-01-27  1:07           ` Psionic K
2024-01-19 15:44 ` Eli Zaretskii
2024-01-27  9:25 Psionic K

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

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

  git send-email \
    --in-reply-to=CADQMGAQR3yvaJK5OdzcGkcy1VAjoMoTa3pSZjwYgGSbTzP0o_A@mail.gmail.com \
    --to=psionik@positron.solutions \
    --cc=help-gnu-emacs@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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.