From: Spencer Baugh <sbaugh@catern.com>
To: emacs-devel@gnu.org
Cc: Spencer Baugh <sbaugh@catern.com>, Eli Zaretskii <eliz@gnu.org>
Subject: [PATCH v2 2/3] Add garbage-collect-maybe function
Date: Sun, 22 Nov 2020 00:08:28 -0500 [thread overview]
Message-ID: <20201122050829.9097-2-sbaugh@catern.com> (raw)
In-Reply-To: <87h7pij7xo.fsf@catern.com>
---
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
next prev parent reply other threads:[~2020-11-22 5:08 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 ` [PATCH 3/3] Start opportunistic GC timer at startup Spencer Baugh
2020-11-18 17:54 ` 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 ` Spencer Baugh [this message]
2020-11-22 18:14 ` [PATCH v2 2/3] Add garbage-collect-maybe function 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=20201122050829.9097-2-sbaugh@catern.com \
--to=sbaugh@catern.com \
--cc=eliz@gnu.org \
--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.