all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tomas Hlavaty <tom@logand.com>
To: emacs-devel@gnu.org
Subject: Re: thunk.el: Document that thunk-force == funcall?
Date: Thu, 19 Nov 2020 19:14:45 +0100	[thread overview]
Message-ID: <87lfexrz6y.fsf@logand.com> (raw)
In-Reply-To: <8E031B6C-8BF3-48DC-B183-8FAF2D366408@acm.org>

On Thu 19 Nov 2020 at 12:50, Mattias Engdegård <mattiase@acm.org> wrote:
> 19 nov. 2020 kl. 00.25 skrev Tomas Hlavaty <tom@logand.com>:
>
>> interesting drawback of thunk.el implementation is that thunk-delay does
>> not allow that and whatever is captured stays captured even after
>> thunk-force.
>
> Not after byte-compilation. The interpreter is very conservative when
> creating closures but the compiler makes more of an effort to capture
> only variables actually used.

this has nothing to do with an effort to capture only variables actually
used

the problem is that current implementation of thunk-delay has memory
leak built-in by design and does not release captured values.

those are captured in the lambda due to ,@body and references to these
captured values are never released.  it is not even possible to
explicitly release those values because it is not known what exactly is
captured.

(let ((huge-data (make-list (expt 2 60))))
  (setq delayed (thunk-delay (count huge-data)))
  ;; huge-data captured by delayed, needed to compute the delayed value
  (thunk-force delayed))
(garbage-collect)
;; huge-data still captured by delayed even though it is not needed anymore

this is bug in thunk-delay

having garbage collection does not eliminate memory leaks

fix would separate thunk and memoization and release the thunk when not
needed anymore:

(let ((void (list nil)))
  (defun memoize (thunk)
    (let ((z void))
      (lambda ()
        (when (eq z void)
          (setq z (funcall thunk)
                thunk nil))
        z))))

(defmacro thunk-delay (&rest body)
  `(memoize (lambda () ,@body)))

at that point thunk-delay is pretty much redundant and it is better not
to pretend that thunk is thunk+memoization and just teach people what
thunk really is (it is just a lambda with zero arguments)

minor note: the example in thunk.el would then also work in buffers
without lexical-binding enabled



  reply	other threads:[~2020-11-19 18:14 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-17 15:17 thunk.el: Document that thunk-force == funcall? Michael Heerdegen
2020-11-17 17:08 ` Tomas Hlavaty
2020-11-17 17:38   ` Michael Heerdegen
2020-11-17 18:09     ` Tomas Hlavaty
2020-11-17 21:07       ` Michael Heerdegen
2020-11-17 22:42         ` Tomas Hlavaty
2020-11-17 23:52           ` Stefan Monnier
2020-11-18  8:01             ` Tomas Hlavaty
2020-11-18 14:04               ` Stefan Monnier
2020-11-18 22:19                 ` Tomas Hlavaty
2020-11-18 22:49                   ` Stefan Monnier
2020-11-18 23:13                     ` Tomas Hlavaty
2020-11-18 23:40                       ` Stephen Leake
2020-11-18  9:04   ` Alfred M. Szmidt
2020-11-18 22:21     ` Tomas Hlavaty
2020-11-17 17:32 ` Drew Adams
2020-11-18 23:05   ` Tomas Hlavaty
2020-11-18 23:25     ` Tomas Hlavaty
2020-11-19 11:50       ` Mattias Engdegård
2020-11-19 18:14         ` Tomas Hlavaty [this message]
2020-11-19 17:18     ` Tomas Hlavaty
2020-11-19  9:49   ` Nicolas Petton
2020-11-17 21:51 ` Stefan Monnier
2020-11-25 14:16   ` Michael Heerdegen
2020-11-27 17:22     ` Michael Heerdegen
2020-12-17  4:37       ` Michael Heerdegen
2020-12-18  2:58         ` Adam Porter

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=87lfexrz6y.fsf@logand.com \
    --to=tom@logand.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.