emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Lisp-level macro to avoid excessive GC in memory-allocating code (was: L


From: Ihor Radchenko
Subject: Lisp-level macro to avoid excessive GC in memory-allocating code (was: Larger GC thresholds for non-interactive Emacs)
Date: Fri, 01 Jul 2022 10:34:34 +0800

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> A possible useful thing Emacs could help with would be a macro dedicated
>> to let-binds like the above. Something like:
>>
>> (with-reduced-gc
>>  ;; Do staff)
>
> Sounds about right, tho I think the name of the macro should be related
> to what the code does rather than to what the author thinks the GC should
> do about it.  Something like `this-code-does-not-generate-garbage`.
>
>>  with-reduced-gc could take care about determining the inner specifics
>>  on what alternative gc-cons-threshold value should be used (possibly
>>  depending on the system memory information).
>
> Sounds good.

This part of the discussion appears to be missed from the following
replies. So, I would like to bring it up again.

To clarify a bit on the usefulness of the proposed macro idea, consider
the following code:

(defvar lst-data)
(benchmark-progn
(let (result)
  (dotimes (i 1000000)
    (push i result))
  (setq lst-data result)
  nil))

This code does not really generate any garbage. Yet GC is triggered
frequently:

Elapsed time: 0.619852s (0.426893s in 11 GCs)

If I instead do

(defvar lst-data)
(benchmark-progn
  (let ((gc-cons-threshold most-positive-fixnum)) ;; the value is just for demo 
purposes 
(let (result)
  (dotimes (i 1000000)
    (push i result))
  (setq lst-data result)
  nil)))

Elapsed time: 0.216380s (0.031766s in 1 GCs)

The difference is obvious.

More generally, any code that generates/returns large data structures is
going to trigger frequent GCs regardless whether such code generates any
garbage.

On the other hand, we cannot, in general terms, predict if real-life
code, which allocates large permanent data structures, also produces a
lot of actual valid garbage that should be collected.

Having some way to prevent excessive garbage collection that is also
smarter than simply setting gc-cons-threshold to large value would be
useful.

As one idea, a lisp program may mark some of the variables to be skipped
by GC and to not contribute to GC threshold checks (that is, allocating
memory into the marked variables will not increase the memory counter
used by GC).

WDYT?

Best,
Ihor



reply via email to

[Prev in Thread] Current Thread [Next in Thread]