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 2/3] Add garbage-collect-maybe function Date: Sun, 22 Nov 2020 00:08:28 -0500 Message-ID: <20201122050829.9097-2-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="22680"; 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:09:14 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 1kghce-0005mx-Mq for ged-emacs-devel@m.gmane-mx.org; Sun, 22 Nov 2020 06:09:12 +0100 Original-Received: from localhost ([::1]:48122 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kghcd-0003PZ-Oo for ged-emacs-devel@m.gmane-mx.org; Sun, 22 Nov 2020 00:09:11 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44964) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kghc1-0002aI-R3 for emacs-devel@gnu.org; Sun, 22 Nov 2020 00:08:33 -0500 Original-Received: from venus.catern.com ([68.183.49.163]:60448) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kghc0-0001RN-8I; Sun, 22 Nov 2020 00:08:33 -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=1606021711; bh=6uwyJr2lwDcPdpClEIzGw1ZuHHY5jvrwlCJwAIFtpS8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VpUaDk1+zYWjOx3BO0c2vpLcnC5Ke/E1VkPuAJe/MSrdfN3jPrVfMs5Yx4g2+D1fY 2+xaf2wO36dUaLz1D8pQF7QpxOFZLH4lbep1IiEfb0/HBaIMzdIsr4cvaUJaI6rysp a1R4Qn36vros3BmfBmTq3Dxs8x8uHDMm8QeIVwgo= 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 561F82DDDEF; Sun, 22 Nov 2020 05:08:31 +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:259598 Archived-At: --- src/alloc.c | 37 +++++++++++++++++++++++++++++++++++++ src/lisp.h | 1 + 2 files changed, 38 insertions(+) diff --git a/src/alloc.c b/src/alloc.c index fd572c0175..cc48acb17a 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -5961,6 +5961,23 @@ maybe_garbage_collect (void) garbage_collect (); } +/* Like maybe_garbage_collect, but with the GC threshold reduced by + FACTOR, so that we'll GC sooner than we would otherwise. + Returns true if we GC'd. */ +bool +maybe_garbage_collect_eagerly (EMACS_INT factor) +{ + bump_consing_until_gc (gc_cons_threshold, Vgc_cons_percentage); + EMACS_INT since_gc = gc_threshold - consing_until_gc; + if (factor >= 1 && since_gc > gc_threshold / factor) + { + garbage_collect (); + return true; + } + else + return false; +} + /* Subroutine of Fgarbage_collect that does most of the work. */ void garbage_collect (void) @@ -6215,6 +6232,25 @@ See Info node `(elisp)Garbage Collection'. */) return CALLMANY (Flist, total); } +DEFUN ("garbage-collect-maybe", Fgarbage_collect_maybe, +Sgarbage_collect_maybe, 1, 1, "", + doc: /* Call `garbage-collect' if enough allocation happened. +FACTOR determines what "enough" means here: +If FACTOR is a positive number N, it means to run GC if more than +1/Nth of the allocations needed to triger automatic allocation took +place. +Therefore, as N gets higher, this is more likely to perform a GC. +Returns t if GC happened, and nil otherwise. */) + (Lisp_Object factor) +{ + CHECK_FIXNAT (factor); + EMACS_INT fact = XFIXNAT (factor); + if (maybe_garbage_collect_eagerly (fact)) + return Qt; + else + return Qnil; +} + /* Mark Lisp objects in glyph matrix MATRIX. Currently the only interesting objects referenced from glyphs are strings. */ @@ -7564,6 +7600,7 @@ N should be nonnegative. */); defsubr (&Smake_finalizer); defsubr (&Spurecopy); defsubr (&Sgarbage_collect); + defsubr (&Sgarbage_collect_maybe); defsubr (&Smemory_info); defsubr (&Smemory_use_counts); #ifdef GNU_LINUX diff --git a/src/lisp.h b/src/lisp.h index 76d74200ac..5ef47fa234 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -3793,6 +3793,7 @@ flush_stack_call_func (void (*func) (void *arg), void *arg) extern void garbage_collect (void); extern void maybe_garbage_collect (void); +extern bool maybe_garbage_collect_eagerly (EMACS_INT factor); extern const char *pending_malloc_warning; extern Lisp_Object zero_vector; extern EMACS_INT consing_until_gc; -- 2.28.0