unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Pogonyshev <pogonyshev@gmail.com>
To: Emacs developers <emacs-devel@gnu.org>
Subject: Make computational threads leave user interface usable
Date: Wed, 1 Nov 2017 16:06:23 +0100	[thread overview]
Message-ID: <CAG7Bpar+=Zc9yn6ss2vKnuMCPvJ4bEZ8bJYKb_r1YrKUwgN0Vg@mail.gmail.com> (raw)

Since several versions Emacs has Lisp threads, but they are not used
much, because 1) only one thread executes at any given time; 2)
threads yield control to each other only with explicit (thread-yield)
call or IO blocks. Which means that it is pointless to start a new
thread for heavy computation: it will lock UI until finished anyway.

Attached patch tries to solve point 2 only by making threads
automatically yield control to each other from time to time. The patch
is mainly for discussion.

To see its effect, evaluate this expression:

    (make-thread (lambda ()
                   (dotimes (n 10000000)
                     (when (= (% n 1000000) 0)
                       (message "%s" n)))
                   (message "done")))

In normal Emacs, UI is frozen until the thread completes. You see
messages in the echo area, but that's rather a special case: you
cannot e.g. navigate or type in the current buffer.

With the patch, however, computation thread periodically (and
automatically: no alteration of the expression is needed) yields to UI
thread, leaving Emacs responsive while computation is going on.

There are some problems, though.

* Computation is 3-4 times slower than without auto-yielding. You can
compare to unpatched Emacs or bind `thread-inhibit-auto-yield' to t in
the thread function. This is probably due to the fact it auto-yields
~50 times per second. But on the other hand, does it really have to be
that slow? I don't know much about Emacs internals, maybe someone with
more knowledge can say if it is unavoidable, or yielding is just not
optimized because it is just not done that frequently currently.

* Message buffer contents seems screwed. But this is probably
"normal", as non-main threads shouldn't touch UI as I understand. This
expression is just an example.

* Variable `thread-auto-yield-after' is accessible from Lisp, but
rebinding doesn't take effect immediately. Which is especially bad if
you rebind from nil to a non-nil value.

In general, what are the thoughts about the patch? Does it look
interesting, or is auto-yielding simply out of question?

Paul

P.S. Please CC me on replies, I'm not subscribed to the list.



             reply	other threads:[~2017-11-01 15:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-01 15:06 Paul Pogonyshev [this message]
2017-11-01 15:10 ` Make computational threads leave user interface usable Paul Pogonyshev
2017-11-01 18:12 ` John Wiegley
2017-11-01 20:03   ` Eli Zaretskii
2017-11-01 21:19   ` Paul Pogonyshev
2017-11-01 21:47     ` John Wiegley
2017-11-03 15:50     ` Stefan Monnier
2017-11-01 19:10 ` Noam Postavsky
2017-11-01 20:16   ` Eli Zaretskii
2017-11-01 20:26     ` Noam Postavsky

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAG7Bpar+=Zc9yn6ss2vKnuMCPvJ4bEZ8bJYKb_r1YrKUwgN0Vg@mail.gmail.com' \
    --to=pogonyshev@gmail.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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).