From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Psionic K Newsgroups: gmane.emacs.help Subject: Identifying sources of allocations in a toy Mandelbrot package Date: Wed, 17 Jan 2024 21:39:04 +0900 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34640"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 17 13:39:59 2024 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rQ5D4-0008iY-KT for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 17 Jan 2024 13:39:58 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rQ5CT-0002Ez-TT; Wed, 17 Jan 2024 07:39:21 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rQ5CR-0002Dq-FM for help-gnu-emacs@gnu.org; Wed, 17 Jan 2024 07:39:19 -0500 Original-Received: from mail-yb1-xb2d.google.com ([2607:f8b0:4864:20::b2d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rQ5CP-0001nT-Dh for help-gnu-emacs@gnu.org; Wed, 17 Jan 2024 07:39:19 -0500 Original-Received: by mail-yb1-xb2d.google.com with SMTP id 3f1490d57ef6-dbf2740355fso5783512276.0 for ; Wed, 17 Jan 2024 04:39:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=positron.solutions; s=google; t=1705495155; x=1706099955; darn=gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=nUi7KRiIoHhfOama4ovtDD3lRebhrikqBT33Gw2msdo=; b=WFjrPrD/Q7e8sZrC/mg+Kec7erv0y49sALEUqzcj4s34b+cMIiKck3vEhnzMFZ4RpQ O8KVpBc4Z40a2ZPQx/0LG6ilwUq2LCq1hy/e42vHSFTeWciYfQRSkCjWCDaYvfcsDOat kxewW2JSEzhlAadNPvA9N1Tt9eFfoAdQ/8x2QwHuHA7oStm04JoIXQG8Y0A5ZGnZin4j aq880t88jxNLtkzux0X5p0xf0Kss4JFdXBXP2/shivxhEeymukHkr+yW45niK1+dkAh7 2B6bhyKKPvN03X9yZtTQxnlOp0a5ODUcRuW1jZisVK2suowsschPerPeP75gAjWKH5F3 Dy1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705495155; x=1706099955; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=nUi7KRiIoHhfOama4ovtDD3lRebhrikqBT33Gw2msdo=; b=QrSd0JH2ut05Vn23thwZr3LyAogCBoRa2xWDsHAlzmpd4taqqiSr2nqrv6Num+DVQ4 u905KqZHRnTZviFpzOSyN/0vWMNN048GO1CYJ9X2RSt1L4aZRhzY/aIdxP7YGrFMgrjA SvC7hm0XffQkMTvnET/BRwphSEMHBK2yvcmdCjD1jL/L6CxGl3SH5+2iKg4m5EAOy7YZ BNWtA/Gs0eduMOw5B03hrqBzGBa+xU/sp9ZR4AISoXz12K1M/+gvkyEtsU4Y4L6N0TWI +uVaw8qdPunBzo5GyHtLtoqQ5OnagcyBqoj5CPQZFYuoQGgdPTkuhYEkSCoQYy5TSumC H4tA== X-Gm-Message-State: AOJu0YwykjsbRMyR4UAe0PX/Axa3Ai6K4ipajTcyKg1dUDIFZeWHF5Do FPfSOslYFln/S8Do9OR0CIRZM0ZDCiTX917UGjlyPoM2dQX2B7dwLkgccxxyELM= X-Google-Smtp-Source: AGHT+IF8NkJrMGynZqRkM+inAI3nZL843aav9k9qTFGfQFQTgGa/dvO5XyD4bBOUAPpib7oSCx21fsky2a13t3KadG8= X-Received: by 2002:a25:ad23:0:b0:dbd:9928:e30b with SMTP id y35-20020a25ad23000000b00dbd9928e30bmr381243ybi.11.1705495155153; Wed, 17 Jan 2024 04:39:15 -0800 (PST) Received-SPF: pass client-ip=2607:f8b0:4864:20::b2d; envelope-from=exec@positron.solutions; helo=mail-yb1-xb2d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:145748 Archived-At: 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.