all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Tomas Hlavaty <tom@logand.com>
Cc: rudi@constantly.at, emacs-devel@gnu.org
Subject: Re: using finalizers
Date: Sun, 02 Jan 2022 08:54:54 +0200	[thread overview]
Message-ID: <83tuemeif5.fsf@gnu.org> (raw)
In-Reply-To: <875yr3krs5.fsf@logand.com> (message from Tomas Hlavaty on Sat, 01 Jan 2022 23:36:10 +0100)

> From: Tomas Hlavaty <tom@logand.com>
> Date: Sat, 01 Jan 2022 23:36:10 +0100
> 
> Lets have a look at the function directory-files.  It is likely the
> worst possible API for accessing filesystem.  (Common Lisp has the same
> flaw.)  It opens a directory, does its work and closes the directory.
> Nice, it is done "properly": open, work, close.  The problem is, that
> the amount of work done inside open/close is not under control of the
> programmer and is not bound in space and time.  Additionally, the whole
> work needs to be done completely, or completely aborted.  It means that
> unless the amount of work is trivial, the whole thing and all the things
> that are built on top of it are useless.  That sounds a bit extreme
> claim.  Lets test it with something real: M-x find-lisp-find-dired
> /nix/store [.]service$.  While Emacs blocked, no results seen.  C-g
> after several seconds.  That was not very useful.  Lets try M-x
> find-dired /nix/store -name '*.service'.  That works nicely.  Why?
> Because the directory traversal runs in a second process (the control
> loop pushes the results out of the process step by step) and the first
> emacs process displays the results step by step as they come (with
> directory entry granularity).  How could find-lisp-find-dired be fixed?
> Could it be fixed while still using directory-files?  I do not think so.
> I guess it can only be fixed by pushing directory entries out from
> another thread (like find-dired does) or using a "stream" of directory
> entries pulled on demand.

One common paradigm for such use cases is to use a callback: the
function that traverses some collection of objects calls that callback
for every object it finds.  There are usually protocols for the
callback to stop the process and prevent the rest of objects from
being examined/reported.

> But when should close be called, when such stream is lazy?  I am
> sure a reasonable ad-hoc solution could be implemented.  But what if
> the directory is traversed recursively?  When should each close be
> called?  A case for another ad-hoc solution?  Looks like finalizers
> would be great here.

We have the unwind-protect machinery in place for that.  In fact, if
you look at the implementation of directory-files, you will see that
it already uses that machinery, and it most probably actually kicked
in when you typed C-g above.



  reply	other threads:[~2022-01-02  6:54 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-30 22:43 using finalizers Tomas Hlavaty
2021-12-31  2:46 ` LdBeth
2021-12-31  3:29   ` Stefan Monnier
2021-12-31  3:36     ` Stefan Monnier
2021-12-31  4:30     ` LdBeth
2021-12-31  4:43       ` LdBeth
2021-12-31  6:28   ` Tomas Hlavaty
2021-12-31 10:59     ` LdBeth
2021-12-31 11:18       ` Tomas Hlavaty
2021-12-31 11:41         ` LdBeth
2021-12-31 12:01           ` Tomas Hlavaty
2022-01-01 17:00     ` Stefan Monnier
2022-01-01 20:25       ` Tomas Hlavaty
2022-01-01 20:47         ` Stefan Monnier
2022-01-01 22:55           ` Tomas Hlavaty
2022-01-01 23:18             ` Stefan Monnier
2022-01-01 23:47               ` Tomas Hlavaty
2022-01-01 23:26             ` Tomas Hlavaty
2021-12-31  7:50   ` Eli Zaretskii
2021-12-31  9:31     ` Tomas Hlavaty
2021-12-31 12:27       ` Eli Zaretskii
2022-01-01 17:58         ` Tomas Hlavaty
2022-01-01 18:20           ` Eli Zaretskii
2022-01-01 20:55             ` Stefan Monnier
2022-01-01 23:05               ` Tomas Hlavaty
2022-01-01 23:21                 ` Stefan Monnier
2021-12-31 14:23       ` Rudolf Schlatte
2021-12-31 16:21         ` Stefan Monnier
2022-01-01 17:37           ` Tomas Hlavaty
2022-01-01 22:36         ` Tomas Hlavaty
2022-01-02  6:54           ` Eli Zaretskii [this message]
2022-01-02  7:53             ` Tomas Hlavaty
2022-01-02  8:33               ` Eli Zaretskii
2022-01-02 13:10                 ` Tomas Hlavaty
2022-01-02 14:42                   ` Eli Zaretskii
2022-01-02 15:16                     ` Eli Zaretskii
2022-01-02 17:18                       ` Tomas Hlavaty
2022-01-02 18:11           ` Rudolf Schlatte
2022-01-03  0:37             ` Tomas Hlavaty
2022-01-04  3:08               ` Richard Stallman
2021-12-31 14:39 ` LdBeth
2022-01-01 17:59   ` Tomas Hlavaty

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=83tuemeif5.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=rudi@constantly.at \
    --cc=tom@logand.com \
    /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.