From: Philip Kaludercic <philipk@posteo.net>
To: 51094@debbugs.gnu.org
Subject: bug#51094: Check if run-with{-idle,}-timer needs to create a timer
Date: Fri, 08 Oct 2021 09:36:22 +0000 [thread overview]
Message-ID: <87a6jj97u1.fsf@posteo.net> (raw)
[-- 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
next reply other threads:[~2021-10-08 9:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-08 9:36 Philip Kaludercic [this message]
2021-10-08 10:46 ` bug#51094: Check if run-with{-idle, }-timer needs to create a 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
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=87a6jj97u1.fsf@posteo.net \
--to=philipk@posteo.net \
--cc=51094@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).