help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Identifying sources of allocations in a toy Mandelbrot package


From: Psionic K
Subject: Identifying sources of allocations in a toy Mandelbrot package
Date: Wed, 17 Jan 2024 21:39:04 +0900

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 via email to

[Prev in Thread] Current Thread [Next in Thread]