all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#51094: Check if run-with{-idle,}-timer needs to create a timer
@ 2021-10-08  9:36 Philip Kaludercic
  2021-10-08 10:46 ` bug#51094: Check if run-with{-idle, }-timer " Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2021-10-08 11:08 ` Eli Zaretskii
  0 siblings, 2 replies; 10+ messages in thread
From: Philip Kaludercic @ 2021-10-08  9:36 UTC (permalink / raw)
  To: 51094

[-- Attachment #1: Type: text/plain, Size: 1524 bytes --]

Tags: patch


I have seen a few packages use run-with-timer or run-with-idle-timer,
where the SECS parameter is configurable with a user option. When this
timer doesn't repeat itself and it makes sense to set SECS to 0 when you
want something to run immediately, I don't think it makes sense to
create a timer object.

The following patch would remove this overhead, and calls the function
immediately if it makes sense.

Comparing these two test cases

--8<---------------cut here---------------start------------->8---
(benchmark-run-compiled 10000
  (funcall #'message "Test %s" "case"))

(benchmark-run-compiled 10000
  (run-with-timer 0 nil #'message "Test %s" "case"))
--8<---------------cut here---------------end--------------->8---

it turns out that funcall takes longer, but run-with-timer (or
run-with-idle-timer for that matter) block Emacs for significantly
longer.  Now this is a very forced example, because run-with-timer is
usually not called in a loop, but it do think it demonstrates a general
advantage.

In GNU Emacs 28.0.60 (build 6, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw scroll bars)
 of 2021-10-05 built on icterid
Repository revision: 1cd1b2835b5e35562c677c48dcf185bb73af4275
Repository branch: emacs-28
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)

Configured using:
 'configure --with-native-compilation --with-x-toolkit=lucid
 --with-imagemagick 'CFLAGS=-O2 -march=native -pipe' LDFLAGS=-flto'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Avoid-creating-timer-if-possible.patch --]
[-- Type: text/patch, Size: 2149 bytes --]

From 1e7c19e1aba6152dbc49173faa04d614fe2961c2 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
Date: Fri, 8 Oct 2021 11:20:53 +0200
Subject: [PATCH] Avoid creating timer if possible

* timer.el (run-with-timer): Check if function can be called
  immediately
(run-with-idle-timer): Check if function can be called
  immediately
---
 lisp/emacs-lisp/timer.el | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/lisp/emacs-lisp/timer.el b/lisp/emacs-lisp/timer.el
index 1ef4931b7b..22029798fd 100644
--- a/lisp/emacs-lisp/timer.el
+++ b/lisp/emacs-lisp/timer.el
@@ -422,7 +422,9 @@ run-with-timer
 
 This function returns a timer object which you can use in `cancel-timer'."
   (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ")
-  (apply 'run-at-time secs repeat function args))
+  (if (and (numberp secs) (= secs 0) (null repeat))
+      (apply function args)
+    (apply 'run-at-time secs repeat function args)))
 
 (defun add-timeout (secs function object &optional repeat)
   "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT.
@@ -448,13 +450,15 @@ run-with-idle-timer
 This function returns a timer object which you can use in `cancel-timer'."
   (interactive
    (list (read-from-minibuffer "Run after idle (seconds): " nil nil t)
-	 (y-or-n-p "Repeat each time Emacs is idle? ")
-	 (intern (completing-read "Function: " obarray 'fboundp t))))
-  (let ((timer (timer-create)))
-    (timer-set-function timer function args)
-    (timer-set-idle-time timer secs repeat)
-    (timer-activate-when-idle timer t)
-    timer))
+         (y-or-n-p "Repeat each time Emacs is idle? ")
+         (intern (completing-read "Function: " obarray 'fboundp t))))
+  (if (and (numberp secs) (= secs 0) (null repeat))
+      (apply function args)
+    (let ((timer (timer-create)))
+      (timer-set-function timer function args)
+      (timer-set-idle-time timer secs repeat)
+      (timer-activate-when-idle timer t)
+      timer)))
 \f
 (defvar with-timeout-timers nil
   "List of all timers used by currently pending `with-timeout' calls.")
-- 
2.30.2


[-- Attachment #3: Type: text/plain, Size: 24 bytes --]


-- 
	Philip Kaludercic

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-10-08 12:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-10-08  9:36 bug#51094: Check if run-with{-idle,}-timer needs to create a timer Philip Kaludercic
2021-10-08 10:46 ` bug#51094: Check if run-with{-idle, }-timer " Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-10-08 10:53   ` Lars Ingebrigtsen
2021-10-08 11:14     ` Lars Ingebrigtsen
2021-10-08 11:33       ` Philip Kaludercic
2021-10-08 12:29         ` Lars Ingebrigtsen
2021-10-08 11:03   ` Philip Kaludercic
2021-10-08 11:29     ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-10-08 11:13   ` Eli Zaretskii
2021-10-08 11:08 ` Eli Zaretskii

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.