From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#51094: Check if run-with{-idle,}-timer needs to create a timer Date: Fri, 08 Oct 2021 09:36:22 +0000 Message-ID: <87a6jj97u1.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10969"; mail-complaints-to="usenet@ciao.gmane.io" To: 51094@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Oct 08 11:37:12 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mYmJU-0002cG-1e for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 08 Oct 2021 11:37:12 +0200 Original-Received: from localhost ([::1]:35458 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mYmJR-0000bQ-TY for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 08 Oct 2021 05:37:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48276) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mYmJL-0000bI-5G for bug-gnu-emacs@gnu.org; Fri, 08 Oct 2021 05:37:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:37608) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mYmJK-0001xQ-QX for bug-gnu-emacs@gnu.org; Fri, 08 Oct 2021 05:37:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mYmJK-0001hd-Jr for bug-gnu-emacs@gnu.org; Fri, 08 Oct 2021 05:37:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 08 Oct 2021 09:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 51094 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16336857926503 (code B ref -1); Fri, 08 Oct 2021 09:37:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 8 Oct 2021 09:36:32 +0000 Original-Received: from localhost ([127.0.0.1]:49154 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mYmIq-0001go-FE for submit@debbugs.gnu.org; Fri, 08 Oct 2021 05:36:32 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:38856) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mYmIn-0001gf-A2 for submit@debbugs.gnu.org; Fri, 08 Oct 2021 05:36:31 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48160) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mYmIm-0000aA-PX for bug-gnu-emacs@gnu.org; Fri, 08 Oct 2021 05:36:29 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:40025) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mYmIk-0001O0-7P for bug-gnu-emacs@gnu.org; Fri, 08 Oct 2021 05:36:28 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 41947240027 for ; Fri, 8 Oct 2021 11:36:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1633685783; bh=WnsuaiB7naR06paEpP9IY6CddW5Hcv8GyNj05844K+I=; h=From:To:Subject:Autocrypt:Date:From; b=WrdkXwfgyfBdfENLGHWoUmQHDMF5oJE46kNiANwqQL7HGBk50yeE4Ef3DdZIAJaNU FmWqmAcBbXcP6wOttoxx8++Wes/T8esDw2uiZcpg5OBgV55pH7+BY1Ehdq6zbfwW3i +aahUc0QfQ3VxU0Ag6Bp6QQ8uIehs41h7LDPEILfOw7xdbdg4M5Gqn0iVsiBWG6kFP Mu3Bw6tUaSMy48mhI+B7llpqel03Qu5xsekfH+VpPbLrohnHtyHA3e8KsywYzQVNsF 0mMIES7gjDKygljvruYS82ijiX1j9cefT3z0n9TFn/RLtoAw3TCtnZrs4kBTPPUMsF XmYL/1OXtirrQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4HQjkG50zKz9rxf for ; Fri, 8 Oct 2021 11:36:22 +0200 (CEST) Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB Received-SPF: pass client-ip=185.67.36.65; envelope-from=philipk@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:216698 Archived-At: --=-=-= Content-Type: text/plain 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' --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Avoid-creating-timer-if-possible.patch >From 1e7c19e1aba6152dbc49173faa04d614fe2961c2 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic 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))) (defvar with-timeout-timers nil "List of all timers used by currently pending `with-timeout' calls.") -- 2.30.2 --=-=-= Content-Type: text/plain -- Philip Kaludercic --=-=-=--