all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Spencer Baugh <sbaugh@catern.com>
To: emacs-devel@gnu.org
Cc: Spencer Baugh <sbaugh@catern.com>
Subject: [PATCH 3/3] Start opportunistic GC timer at startup
Date: Tue, 17 Nov 2020 19:20:50 -0500	[thread overview]
Message-ID: <20201118002050.16426-4-sbaugh@catern.com> (raw)
In-Reply-To: <20201118002050.16426-1-sbaugh@catern.com>

---
 lisp/startup.el | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/lisp/startup.el b/lisp/startup.el
index 9f67dfde12..412ed0ca0e 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -1008,6 +1008,32 @@ the `--debug-init' option to view a complete error backtrace."
     (when debug-on-error-should-be-set
       (setq debug-on-error debug-on-error-from-init-file))))
 
+(defcustom gc-opportunistic-eager-factor 2
+  "How eager we should be when GCing while Emacs is idle.
+Emacs will call garbage-collect-maybe with this argument to
+eagerly GC after gc-estimated-time idle seconds have passed.
+If this is nil, we won't perform GC while Emacs is idle.
+"
+  :type 'integer)
+
+(defvar gc-next-opportunistic-timer nil
+  "The timer for the next opportunistic GC
+This is set by `gc-start-opportunistic'.")
+
+(defun gc-start-opportunistic ()
+  "Start a timer to GC after `gc-estimated-time' idle seconds.
+The GC is performed with `garbage-collect-maybe', and is passed
+`gc-opportunistic-eager-factor' as its single argument.
+If `gc-opportunistic-eager-factor' is nil, we won't do anything.
+This function is itself run by an idle timer at Emacs startup."
+  (when gc-opportunistic-eager-factor
+    (when gc-next-opportunistic-timer
+      (cancel-timer gc-next-opportunistic-timer))
+    (setq gc-next-opportunistic-timer
+          (run-with-idle-timer
+           gc-estimated-time nil
+           #'garbage-collect-maybe gc-opportunistic-eager-factor))))
+
 (defun command-line ()
   "A subroutine of `normal-top-level'.
 Amongst another things, it parses the command-line arguments."
@@ -1420,6 +1446,15 @@ please check its value")
 		 (eq face-ignored-fonts old-face-ignored-fonts))
       (clear-face-cache)))
 
+  ;; Start opportunistic GC (after loading the init file, so we obey
+  ;; its settings).  This is desirable for two reason:
+  ;; - It reduces the number of times we have to GC in the middle of
+  ;;   an operation.
+  ;; - It means we GC when the C stack is short, reducing the risk of false
+  ;;   positives from the conservative stack scanning.
+  (when gc-opportunistic-eager-factor
+    (run-with-idle-timer 1 t #'gc-start-opportunistic))
+
   (setq after-init-time (current-time))
   ;; Display any accumulated warnings after all functions in
   ;; `after-init-hook' like `desktop-read' have finalized possible
-- 
2.28.0




  parent reply	other threads:[~2020-11-18  0:20 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Message-ID: <jwv1rgtjhhy.fsf-monnier+emacs@gnu.org>
2020-11-18  0:20 ` Eager garbage collection Spencer Baugh
2020-11-18  0:20   ` [PATCH 1/3] Add gc-estimated-time variable Spencer Baugh
2020-11-18 17:45     ` Eli Zaretskii
2020-11-18  0:20   ` [PATCH 2/3] Add garbage-collect-maybe function Spencer Baugh
2020-11-18 17:49     ` Eli Zaretskii
2020-11-18  0:20   ` Spencer Baugh [this message]
2020-11-18 17:54     ` [PATCH 3/3] Start opportunistic GC timer at startup Eli Zaretskii
2020-11-18  3:45   ` Eager garbage collection Stefan Monnier
2020-11-18  4:05     ` Spencer Baugh
2020-11-18  4:48       ` Stefan Monnier
2020-11-18  8:02   ` Andrea Corallo via Emacs development discussions.
2020-11-18 15:06     ` Eli Zaretskii
2020-11-18 15:30       ` Andrea Corallo via Emacs development discussions.
2020-11-18 17:19         ` Eli Zaretskii
2020-11-18 17:26           ` Andrea Corallo via Emacs development discussions.
2020-11-18 18:07             ` Eli Zaretskii
2020-11-18 18:19               ` Andrea Corallo via Emacs development discussions.
2020-11-18 15:19     ` Spencer Baugh
2020-11-18 15:47       ` Andrea Corallo via Emacs development discussions.
2020-11-18 16:52         ` Stefan Monnier
2020-11-18 17:12           ` Andrea Corallo via Emacs development discussions.
2020-11-18 17:34           ` Eli Zaretskii
2020-11-18 18:00   ` Eli Zaretskii
2020-11-22  5:07     ` Spencer Baugh
2020-11-22  5:08       ` [PATCH v2 1/3] Add gc-estimated-time variable Spencer Baugh
2020-11-22 18:10         ` Eli Zaretskii
2020-11-22 19:48           ` Stefan Monnier
2020-11-22  5:08       ` [PATCH v2 2/3] Add garbage-collect-maybe function Spencer Baugh
2020-11-22 18:14         ` Eli Zaretskii
2020-11-22  5:08       ` [PATCH v2 3/3] Start opportunistic GC timer at startup Spencer Baugh
2020-12-04 23:19         ` Stefan Monnier
2020-12-05  7:52           ` Eli Zaretskii
2020-12-05 13:55             ` Stefan Monnier
2020-12-05 14:24               ` Eli Zaretskii
2020-12-05 14:41                 ` Stefan Monnier
2020-11-22 15:13       ` Eager garbage collection Eli Zaretskii
2020-11-16  4:11 sbaugh
2020-11-16 15:07 ` Stefan Monnier
2020-11-16 16:34   ` Eli Zaretskii
2020-11-16 18:37     ` Stefan Monnier
2020-11-18 18:00     ` yyoncho
2020-11-18 18:16       ` Eli Zaretskii
2020-11-18 21:37         ` Stefan Monnier
2020-11-18 21:59           ` Stefan Monnier

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201118002050.16426-4-sbaugh@catern.com \
    --to=sbaugh@catern.com \
    --cc=emacs-devel@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 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.