* 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
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
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 ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-10-08 10:53 ` Lars Ingebrigtsen
` (2 more replies)
2021-10-08 11:08 ` Eli Zaretskii
1 sibling, 3 replies; 10+ messages in thread
From: Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2021-10-08 10:46 UTC (permalink / raw)
To: Philip Kaludercic; +Cc: 51094
Philip Kaludercic [2021-10-08 09:36 +0000] wrote:
> 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.
IIUC, the semantics of SECS=0 (alias nil) is not the same as eager
funcall, because timer functions are intended to be run asynchronously
in a separate command loop. So often what is meant by "now" is e.g. "as
soon as I quit the current active minibuffer".
I realise this patch does not touch run-at-time, but it's documented as
being interchangeable with run-with-timer, so the eager funcall sounds
like a breaking change.
If packages indeed want to run something immediately, why create a timer
at all? Or am I misunderstanding something?
Thanks,
--
Basil
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
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:03 ` Philip Kaludercic
2021-10-08 11:13 ` Eli Zaretskii
2 siblings, 1 reply; 10+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-08 10:53 UTC (permalink / raw)
To: Philip Kaludercic; +Cc: Basil L. Contovounesios, 51094
"Basil L. Contovounesios" via "Bug reports for GNU Emacs, the Swiss army
knife of text editors" <bug-gnu-emacs@gnu.org> writes:
> IIUC, the semantics of SECS=0 (alias nil) is not the same as eager
> funcall, because timer functions are intended to be run asynchronously
> in a separate command loop. So often what is meant by "now" is e.g. "as
> soon as I quit the current active minibuffer".
Yes, the semantics of running things from a timer are different from
running them in the normal flow, so we can't transform 0-delay timers
into funcalls.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
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: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
2 siblings, 1 reply; 10+ messages in thread
From: Philip Kaludercic @ 2021-10-08 11:03 UTC (permalink / raw)
To: Basil L. Contovounesios; +Cc: 51094
"Basil L. Contovounesios" <contovob@tcd.ie> writes:
> Philip Kaludercic [2021-10-08 09:36 +0000] wrote:
>
>> 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.
>
> IIUC, the semantics of SECS=0 (alias nil) is not the same as eager
> funcall, because timer functions are intended to be run asynchronously
> in a separate command loop. So often what is meant by "now" is e.g. "as
> soon as I quit the current active minibuffer".
I see, it might be necessary to consider examples where this might go
wrong.
> I realise this patch does not touch run-at-time, but it's documented as
> being interchangeable with run-with-timer, so the eager funcall sounds
> like a breaking change.
>
> If packages indeed want to run something immediately, why create a timer
> at all? Or am I misunderstanding something?
Because you might want to delay certain things, such as highlighting or
completion. It makes sense on older devices, but modern systems can run
a lot of things immediately. Consider lazy-highlight-initial-delay or
the icomplete options. On my desktop I have no issue with setting
lazy-highlight-initial-delay to 0 but on my laptop I keep the default
value.
> Thanks,
--
Philip Kaludercic
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
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 11:08 ` Eli Zaretskii
1 sibling, 0 replies; 10+ messages in thread
From: Eli Zaretskii @ 2021-10-08 11:08 UTC (permalink / raw)
To: Philip Kaludercic; +Cc: 51094
> From: Philip Kaludercic <philipk@posteo.net>
> Date: Fri, 08 Oct 2021 09:36:22 +0000
>
> 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.
I'm not sure we should care, but in any case, what you do there
requires a comment explaining the rationale.
Thanks.
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
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:03 ` Philip Kaludercic
@ 2021-10-08 11:13 ` Eli Zaretskii
2 siblings, 0 replies; 10+ messages in thread
From: Eli Zaretskii @ 2021-10-08 11:13 UTC (permalink / raw)
To: Basil L. Contovounesios; +Cc: philipk, 51094
> Cc: 51094@debbugs.gnu.org
> Date: Fri, 08 Oct 2021 11:46:00 +0100
> From: "Basil L. Contovounesios" via "Bug reports for GNU Emacs,
> the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>
> Philip Kaludercic [2021-10-08 09:36 +0000] wrote:
>
> > 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.
>
> IIUC, the semantics of SECS=0 (alias nil) is not the same as eager
> funcall, because timer functions are intended to be run asynchronously
> in a separate command loop. So often what is meant by "now" is e.g. "as
> soon as I quit the current active minibuffer".
Right, and that's one more aspect of this change to consider. It
could very well change the behavior in incompatible ways, so I wonder
whether we really should make this change.
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
2021-10-08 10:53 ` Lars Ingebrigtsen
@ 2021-10-08 11:14 ` Lars Ingebrigtsen
2021-10-08 11:33 ` Philip Kaludercic
0 siblings, 1 reply; 10+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-08 11:14 UTC (permalink / raw)
To: Philip Kaludercic; +Cc: Basil L. Contovounesios, 51094
Lars Ingebrigtsen <larsi@gnus.org> writes:
> Yes, the semantics of running things from a timer are different from
> running them in the normal flow, so we can't transform 0-delay timers
> into funcalls.
See for instance:
(when (> (minibuffer-depth) 0)
;; We're inside a minibuffer already, so if the emacs-client is trying
;; to open a frame on a new display, we might end up with an unusable
;; frame because input from that display will be blocked (until exiting
;; the minibuffer). Better exit this minibuffer right away.
(run-with-timer 0 nil (lambda () (server-execute-continuation proc)))
So this change is a no go.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
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
0 siblings, 0 replies; 10+ messages in thread
From: Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2021-10-08 11:29 UTC (permalink / raw)
To: Philip Kaludercic; +Cc: 51094
Philip Kaludercic [2021-10-08 11:03 +0000] wrote:
> "Basil L. Contovounesios" <contovob@tcd.ie> writes:
>> Philip Kaludercic [2021-10-08 09:36 +0000] wrote:
>>
>>> 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.
>>
>> IIUC, the semantics of SECS=0 (alias nil) is not the same as eager
>> funcall, because timer functions are intended to be run asynchronously
>> in a separate command loop. So often what is meant by "now" is e.g. "as
>> soon as I quit the current active minibuffer".
>
> I see, it might be necessary to consider examples where this might go
> wrong.
Off the top of my head there's also ivy-quit-and-run in the ivy package,
but Lars pointed to effectively the same idiom.
>> I realise this patch does not touch run-at-time, but it's documented as
>> being interchangeable with run-with-timer, so the eager funcall sounds
>> like a breaking change.
>>
>> If packages indeed want to run something immediately, why create a timer
>> at all? Or am I misunderstanding something?
>
> Because you might want to delay certain things, such as highlighting or
> completion.
What I meant is, the choice whether to run-with-timer or funcall can be
left to the calling package, since it knows better what it wants to do.
Thanks,
--
Basil
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
2021-10-08 11:14 ` Lars Ingebrigtsen
@ 2021-10-08 11:33 ` Philip Kaludercic
2021-10-08 12:29 ` Lars Ingebrigtsen
0 siblings, 1 reply; 10+ messages in thread
From: Philip Kaludercic @ 2021-10-08 11:33 UTC (permalink / raw)
To: Lars Ingebrigtsen; +Cc: Basil L. Contovounesios, 51094-close
Lars Ingebrigtsen <larsi@gnus.org> writes:
> Lars Ingebrigtsen <larsi@gnus.org> writes:
>
>> Yes, the semantics of running things from a timer are different from
>> running them in the normal flow, so we can't transform 0-delay timers
>> into funcalls.
>
> See for instance:
>
> (when (> (minibuffer-depth) 0)
> ;; We're inside a minibuffer already, so if the emacs-client is trying
> ;; to open a frame on a new display, we might end up with an unusable
> ;; frame because input from that display will be blocked (until exiting
> ;; the minibuffer). Better exit this minibuffer right away.
> (run-with-timer 0 nil (lambda () (server-execute-continuation proc)))
>
> So this change is a no go.
Ok, that makes sense.
One last question, would it make sense to provide a function like
run-with-timer that would work like my patch?
--
Philip Kaludercic
^ permalink raw reply [flat|nested] 10+ messages in thread
* bug#51094: Check if run-with{-idle, }-timer needs to create a timer
2021-10-08 11:33 ` Philip Kaludercic
@ 2021-10-08 12:29 ` Lars Ingebrigtsen
0 siblings, 0 replies; 10+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-08 12:29 UTC (permalink / raw)
To: Philip Kaludercic; +Cc: Basil L. Contovounesios, 51094
Philip Kaludercic <philipk@posteo.net> writes:
> One last question, would it make sense to provide a function like
> run-with-timer that would work like my patch?
It sounds really specialised -- I don't think there'd be much point.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [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.