From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh Newsgroups: gmane.emacs.devel Subject: [PATCH v2 3/3] Start opportunistic GC timer at startup Date: Sun, 22 Nov 2020 00:08:29 -0500 Message-ID: <20201122050829.9097-3-sbaugh@catern.com> References: <87h7pij7xo.fsf@catern.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27148"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Spencer Baugh , Eli Zaretskii To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Nov 22 06:10:23 2020 Return-path: Envelope-to: ged-emacs-devel@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 1kghdn-0006y6-L0 for ged-emacs-devel@m.gmane-mx.org; Sun, 22 Nov 2020 06:10:23 +0100 Original-Received: from localhost ([::1]:51068 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kghdm-0004b2-NZ for ged-emacs-devel@m.gmane-mx.org; Sun, 22 Nov 2020 00:10:22 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44968) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kghc3-0002cD-QL for emacs-devel@gnu.org; Sun, 22 Nov 2020 00:08:35 -0500 Original-Received: from venus.catern.com ([68.183.49.163]:60450) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kghc0-0001SC-Rq; Sun, 22 Nov 2020 00:08:35 -0500 Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=98.7.229.235; helo=localhost; envelope-from=sbaugh@catern.com; receiver= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=catern.com; s=mail; t=1606021712; bh=ZzXhE9TIiwS7vKIHbWpTGkBLmP8mrmndCSZ2QOrVL8w=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=UHMDRc8BWfHimk+rZUqPtKx4zdlmBLW4N4V8T/2bR7SizTsa5uJstCxrAW3Im36RQ fM/pOqEelclIdVgWVsOBMQVM7zyLQf6TnqZC1BxIwPpgULfr0bX1hrDGoHfE3hXa80 6d5neLl3YV3B0m8TObGAv5wYAZV7lEzjQ46t5BTY= Original-Received: from localhost (cpe-98-7-229-235.nyc.res.rr.com [98.7.229.235]) by venus.catern.com (Postfix) with ESMTPSA id 09C732DDDF4; Sun, 22 Nov 2020 05:08:32 +0000 (UTC) X-Mailer: git-send-email 2.28.0 In-Reply-To: <87h7pij7xo.fsf@catern.com> Received-SPF: pass client-ip=68.183.49.163; envelope-from=sbaugh@catern.com; helo=venus.catern.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:259599 Archived-At: --- lisp/startup.el | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lisp/startup.el b/lisp/startup.el index 9f67dfde12..a9dc75f542 100644 --- a/lisp/startup.el +++ b/lisp/startup.el @@ -1008,6 +1008,53 @@ 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 + "Factor to use for `garbage-collect-maybe' with opportunistic GC. +This should be a positive integer or nil. +If non-nil, Emacs will call `garbage-collect-maybe' with this +argument as part of opportunistic GC triggered by +`gc-start-opportunistic'. +If this is nil, `gc-start-opportunistic' will do nothing." + :type 'integer + :version "28.1") + +(defvar gc-opportunistic-performed 0 + "The number of opportunistic GCs we have actually performed. +This is incremented by `gc-start-opportunistic', if and when it +actually does a GC.") + +(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 an opportunistic GC which may run at some point in the future. + +This function may perform a single GC at some point while Emacs +is idle, with the goal of improving interactive performance by +avoiding GC while the user is actively interacting with Emacs. + +The higher `gc-opportunistic-eager-factor' is, the more likely +this function is to actually perform a GC. Note that increasing +this variable can worsen performance by performing excessive GCs. +If `gc-opportunistic-eager-factor' is nil, this function will do +nothing. + +If `gc-opportunistic-eager-factor' is non-nil at Emacs startup, +this function will be run by an idle timer. Such a timer can +also be started after Emacs startup with `run-with-idle-timer'." + (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 + (lambda () + (if (garbage-collect-maybe gc-opportunistic-eager-factor) + (setq gc-opportunistic-performed + (1+ gc-opportunistic-performed)))) + )))) + (defun command-line () "A subroutine of `normal-top-level'. Amongst another things, it parses the command-line arguments." @@ -1420,6 +1467,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