Subject: 26.3; Post-command hooks slow with M-x commands

From: Simon Katz <[email-masked]>
Date: Fri, 13 Aug 2021 12:52:32 +0100

Post-command hooks are slow to run for commands invoked using `M-x ...`.

I'm using Emacs 26.3 (which produced this bug report), but I've checked
with 27.2.2 and I get the same behaviour.

Here's a startup file that can be used to show this (also at
https://github.com/simon-katz/demo-post-command-hook-slow-with-m-x-commands.el/blob/8e7b96fc322f6372fb1531f04ab6008d59ab9b9e/demo-post-command-hook-slow-with-m-x-commands.el#L1
).

;;; demo-post-command-hook-slow-with-m-x-commands.el --- Demo that
post-sommand hooks are slow with M-x commands      -*- lexical-binding: t;
-*-

;; This demonstrates an issue with `(add-hook 'post-command-hook ...)`.

;; To reproduce the problem:
;; - Run `emacs -q -l demo-post-command-hook-slow-with-m-x-commands.el`
(that's
;;   this file).
;; - Enter `M-x next-line`.
;; - Observe that the message displayed by the `say-something` function
;;   takes a while to appear.
;; - Contrast with hitting the down-arrow key, when the message appears
;;   immediately.

;;; Code:

(require 'package)

(setq debug-on-error t
      no-byte-compile t
      package-archives '(("melpa" . "https://melpa.org/packages/")
                         ("gnu" . "https://elpa.gnu.org/packages/"))
      package-user-dir (expand-file-name (make-temp-name
"flycheck-tmp-elpa")
                                         user-emacs-directory)
      custom-file (expand-file-name "custom.el" package-user-dir))

(defun say-something ()
  (let ((inhibit-message t))
    (message
     "With `M-x next-line` (for example), this message is slow to appear")))

(add-hook 'post-command-hook 'say-something)

(display-buffer "*Messages*")

(provide 'demo-post-command-hook-slow-with-m-x-commands)
;;; demo-post-command-hook-slow-with-m-x-commands.el ends here

In GNU Emacs 26.3 (build 1, x86_64-apple-darwin18.2.0, NS appkit-1671.20
Version 10.14.3 (Build 18D109))
 of 2019-09-02 built on builder10-14.porkrind.org
Windowing system distributor 'Apple', version 10.3.1894
Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
With ‘M-x next-line‘ (for example), this message is slow to appear [13
times]
You can run the command ‘next-line’ with C-n
With ‘M-x next-line‘ (for example), this message is slow to appear [10
times]
Quit
With ‘M-x next-line‘ (for example), this message is slow to appear [7 times]
Making completion list...
With ‘M-x next-line‘ (for example), this message is slow to appear [6 times]

Configured using:
 'configure --with-ns '--enable-locallisppath=/Library/Application
 Support/Emacs/${version}/site-lisp:/Library/Application
 Support/Emacs/site-lisp' --with-modules'

Configured features:
NOTIFY ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS MODULES THREADS

Important settings:
  value of $LANG: en_GB.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Fundamental

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort map mail-extr emacsbug message rmc puny dired
dired-loaddefs format-spec rfc822 mml mml-sec epa derived epg gnus-util
rmail rmail-loaddefs mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils
demo-post-command-hook-slow-with-m-x-commands package easymenu
epg-config url-handlers url-parse auth-source cl-seq eieio eieio-core
cl-macs eieio-loaddefs password-cache url-vars seq byte-opt gv bytecomp
byte-compile cconv cl-loaddefs cl-lib elec-pair time-date tooltip eldoc
electric uniquify ediff-hook vc-hooks lisp-float-type mwheel term/ns-win
ns-win ucs-normalize mule-util term/common-win tool-bar dnd fontset
image regexp-opt fringe tabulated-list replace newcomment text-mode
elisp-mode lisp-mode prog-mode register page menu-bar rfn-eshadow
isearch timer select scroll-bar mouse jit-lock font-lock syntax facemenu
font-core term/tty-colors frame cl-generic cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite charscript charprop case-table epa-hook
jka-cmpr-hook help simple abbrev obarray minibuffer cl-preloaded nadvice
loaddefs button faces cus-face macroexp files text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote threads kqueue cocoa ns multi-tty
make-network-process emacs)

Memory information:
((conses 16 213714 7973)
 (symbols 48 21049 1)
 (miscs 40 83 191)
 (strings 32 31839 1672)
 (string-bytes 1 863206)
 (vectors 16 36747)
 (vector-slots 8 745240 8812)
 (floats 8 57 65)
 (intervals 56 214 0)
 (buffers 992 13))