unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: mail@daniel-mendler.de
To: 46326@debbugs.gnu.org
Subject: bug#46326: 27.1.50; Excessive memory allocations with minibuffer-with-setup-hook
Date: Fri, 05 Feb 2021 13:51:41 +0100	[thread overview]
Message-ID: <62c490ed0d8d24d8b259ac1ba55ea79e@mendler.net> (raw)

Dear Emacs developers,

I have an issue on 27.1.50 with excessive memory allocations when using
minibuffer-with-setup-hook with large closures and :append. This issue 
has been
observed in my Consult package, which is similar to Counsel and which 
provides
commands based on completing-read. Naturally I have to perform some 
setup in the
minibuffer-setup-hook. Most of the memory allocations are due to 
add-hook and in
particular the sort function. The sort functions is called because of 
:append
which sets a hook priority. The reason seems to be that large closures 
are
copied, but I didn't fully investigate the reasons for the issue.

Example profile:

     140,068,687  92%       - consult-buffer
     140,068,687  92%        - consult--buffer
     140,068,687  92%         - consult--multi
     138,933,527  91%          - consult--read
     138,933,527  91%           - apply
     138,933,527  91%            - #<subr consult--read>
      99,916,347  65%             - consult--with-async-1
      99,916,347  65%              - #<compiled 0x3e02774d5b1a3f7>
      99,912,203  65%               - consult--with-preview-1
      80,735,735  53%                - #<compiled -0xbfb98ad3a8adf1c>
      80,735,735  53%                 - completing-read
      80,735,735  53%                  - selectrum-completing-read
      62,909,765  41%                   - selectrum-read
>>>  58,826,659  38%                    + add-hook
       1,077,206   0%                    + 
selectrum--minibuffer-post-command-hook
       ....
>>>  19,176,468  12%                + add-hook
>>>  39,017,180  25%             + add-hook

The issue can be mitigated by using a modified version of
minibuffer-with-setup-hook, where I am creating a symbol and fsetting 
instead of
adding a lambda directly via add-hook. This technique is also used in
set-transient-map in order to avoid problems with letrec-closures and
add-hook/remove-hook.

     (defmacro consult--minibuffer-with-setup-hook (fun &rest body)
       (declare (indent 1) (debug t))
       (let ((hook (make-symbol "hook"))
             (append))
         (when (eq (car-safe fun) :append)
           (setq append '(t) fun (cadr fun)))
         `(let ((,hook (make-symbol "consult--minibuffer-setup")))
            (fset ,hook (lambda ()
                          (remove-hook 'minibuffer-setup-hook ,hook)
                          (funcall ,fun)))
            (unwind-protect
                (progn
                  (add-hook 'minibuffer-setup-hook ,hook ,@append)
                  ,@body)
              (remove-hook 'minibuffer-setup-hook ,hook)))))

See the issue https://github.com/minad/consult/issues/193 for reference.

Maybe there exists an alternative solution to the problem and the 
problem should
be fixed on a different level than by replacing the 
minibuffer-with-setup-hook
macro?

Daniel Mendler

In GNU Emacs 27.1.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.5, 
cairo version 1.16.0) of 2020-10-24
Repository revision: c847d5998f588dbf3eca5ea1ec573a2d64a97607
Repository branch: emacs-27
Windowing system distributor 'The X.Org Foundation', version 
11.0.12004000
System Description: Debian GNU/Linux 10 (buster)





             reply	other threads:[~2021-02-05 12:51 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-05 12:51 mail [this message]
2021-02-05 14:09 ` bug#46326: 27.1.50; Excessive memory allocations with minibuffer-with-setup-hook Eli Zaretskii
2021-02-05 15:20   ` mail
2021-02-05 15:58     ` Eli Zaretskii
2021-02-05 16:10       ` mail
2021-02-08  9:25 ` jakanakaevangeli
2021-02-09  0:19   ` mail
2021-02-09 22:13     ` jakanakaevangeli
2021-04-23 18:26 ` Stefan Monnier
2021-04-23 19:28   ` Daniel Mendler
2021-04-23 20:34     ` jakanakaevangeli
2021-04-23 20:52       ` Daniel Mendler
2021-04-23 21:27     ` Stefan Monnier
2021-04-24  6:10       ` Eli Zaretskii
2021-04-24 13:06         ` Stefan Monnier
2022-06-16 13:06       ` Lars Ingebrigtsen

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=62c490ed0d8d24d8b259ac1ba55ea79e@mendler.net \
    --to=mail@daniel-mendler.de \
    --cc=46326@debbugs.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).