unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Chris Hanson <cph@chris-hanson.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 66288@debbugs.gnu.org
Subject: bug#66288: 29.1; Performance regression using pipe for subprocess
Date: Mon, 2 Oct 2023 14:22:06 -0400	[thread overview]
Message-ID: <fab8e074-ac2c-41ff-b88c-50db6bc0e680@chris-hanson.org> (raw)
In-Reply-To: <83bkdh8u2k.fsf@gnu.org>

On 10/2/23 01:36, Eli Zaretskii wrote:
>> Cc: 66288@debbugs.gnu.org
>> Date: Mon, 02 Oct 2023 08:02:14 +0300
>> From: Eli Zaretskii <eliz@gnu.org>
>>
>>> I saw that there were no relevant differences in "xscheme.el" but I
>>> never thought that was relevant.
>>>
>>> I believe this has something to do with how piped subprocesses are being
>>> managed.  I've not looked deeply into the C code for this, but I could
>>> find no mention of anything to do with pipes in NEWS.
>>
>> Because AFAIK we didn't change anything in that department.
> 
> I've now identified 3 changes in Emacs 29 which could potentially
> affect your case.  Not sure if they do, but it might be worth your
> while to check them first.
> 
> First, Emacs 29 uses posix_spawn by default on systems where it is
> available and usable.  You will see this fragment at the beginning of
> callproc.c:
> 
>    /* In order to be able to use `posix_spawn', it needs to support some
>       variant of `chdir' as well as `setsid'.  */
>    #if defined HAVE_SPAWN_H && defined HAVE_POSIX_SPAWN        \
>      && defined HAVE_POSIX_SPAWNATTR_SETFLAGS                  \
>      && (defined HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR        \
> 	|| defined HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP) \
>      && defined HAVE_DECL_POSIX_SPAWN_SETSID                   \
>      && HAVE_DECL_POSIX_SPAWN_SETSID == 1			    \
>      /* posix_spawnattr_setflags rejects POSIX_SPAWN_SETSID on \
>         Haiku */						    \
>      && !defined HAIKU
>    # include <spawn.h>
>    # define USABLE_POSIX_SPAWN 1
>    #else
>    # define USABLE_POSIX_SPAWN 0
>    #endif
> 
> If on your system USABLE_POSIX_SPAWN gets the value 1 here, edit
> callproc.c to force it to zero, then rebuild Emacs, and see if this
> affects the behavior.
> 
> Next, we have the following two code fragments in
> wait_reading_process_output, which are new in Emacs 29:
> 
> Code fragment#1:
> 
>        if ((read_kbd
> 	   /* The following code doesn't make any sense for just the
> 	      wait_for_cell case, because detect_input_pending returns
> 	      whether or not the keyboard buffer isn't empty or there
> 	      is mouse movement.  Any keyboard input that arrives
> 	      while waiting for a cell will cause the select call to
> 	      be skipped, and gobble_input to be called even when
> 	      there is no input available from the terminal itself.
> 	      Skipping the call to select also causes the timeout to
> 	      be ignored.  (bug#46935) */
> 	   /* || !NILP (wait_for_cell) */)
> 	  && detect_input_pending ())
> 
> Code fragment#2:
> 
>    #if !defined USABLE_SIGIO && !defined WINDOWSNT
> 	    /* If we're polling for input, don't get stuck in select for
> 	       more than 25 msec. */
> 	    struct timespec short_timeout = make_timespec (0, 25000000);
> 	    if ((read_kbd || !NILP (wait_for_cell))
> 		&& timespec_cmp (short_timeout, timeout) < 0)
> 	      timeout = short_timeout;
>    #endif
> 
> (I think the second one should not affect you because your system
> should have USABLE_SIGIO defined, but maybe I'm mistaken.)  Compare
> these with Emacs 28, and try reverting to 28.2 code to see if that
> changes anything in your case.

None of the three fragments made any difference.

> Finally, if you describe in plain English how xscheme.el reads
> subprocess output at the stage where you see the slowdown, it might
> give further ideas.  I'm not familiar with xscheme.el, and figuring
> out which code gets executed when one runs "run-scheme" is not
> trivial, so a detailed enough description might help.  Specifically,
> how does xscheme.el decide how much of the subprocess's output to read
> and display?

The process filter has one complexity: it looks for encoded commands 
from the subprocess, which are of the form "ESC <char>" or "ESC <char> 
<string> ESC", depending on the <char>.  There is a small state machine 
to do that, which searches the output string for ESC using 
`string-search'. In this case there are no commands embedded, so that 
should not be relevant.

The ordinary text is inserted into the process buffer using standard 
filter-output code, except it looks for BEL and translates that to 
(beep) if found.  In this case there are no occurrences of BEL in the 
output, so that's not relevant.  So, basically the output string is 
passed to `insert', making sure that process mark and point are updated 
appropriately.

I contrived a small example test and ran it under both editors (see 
below).  It does some printing and then shows the time taken in the 
subprocess.  This should be valid since Scheme will block while waiting 
on Emacs to process the output.

The reported times are in milliseconds, with 28.2 taking 1ms and 29.1 
taking 880ms (increasing the test loop from 20 to 200, the times are 8ms 
and 9974ms respectively).  As I said before, that's pretty dramatic: 
about 3 orders of magnitude.  It feels like that in normal use too -- 
it's like being 30-40 years in the past, when that kind of performance 
was expected.

28.2:
--------------------------------
(show-time
  (lambda ()
    (for-each write-line (iota 20))))
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;process time: 0 (0 RUN + 0 GC); real time: 1
--------------------------------

29.1:
--------------------------------
(show-time
  (lambda ()
    (for-each write-line (iota 20))))
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;process time: 0 (0 RUN + 0 GC); real time: 880
--------------------------------





  reply	other threads:[~2023-10-02 18:22 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-01  0:57 bug#66288: 29.1; Performance regression using pipe for subprocess Chris Hanson
2023-10-01  8:39 ` Eli Zaretskii
2023-10-01 18:02   ` Chris Hanson
2023-10-02  5:02     ` Eli Zaretskii
2023-10-02  5:07       ` Eli Zaretskii
2023-10-02 17:14         ` Chris Hanson
2023-10-02  5:36       ` Eli Zaretskii
2023-10-02 18:22         ` Chris Hanson [this message]
2023-10-02 19:12           ` Gerd Möllmann
2023-10-02 19:27             ` Dmitry Gutov
2023-10-02 19:40               ` Gerd Möllmann
2023-10-02 20:15                 ` Dmitry Gutov
2023-10-02 23:23               ` Chris Hanson
2023-10-03  5:06                 ` Gerd Möllmann
2023-10-03  6:22                 ` Eli Zaretskii
2023-10-03  6:48                   ` Eli Zaretskii
2023-10-03 17:24                   ` Eli Zaretskii
2023-10-03 19:12                     ` Chris Hanson
2023-10-03 20:22                       ` Dmitry Gutov
2023-10-03 17:42                   ` Dmitry Gutov
2023-10-03 17:57                     ` Eli Zaretskii
2023-10-03 20:58                       ` Gregory Heytings
2023-10-03 21:26                         ` Dmitry Gutov
2023-10-04  0:33                           ` Chris Hanson
2023-10-04  6:52                             ` Eli Zaretskii
2023-10-04  9:10                               ` Gregory Heytings
2023-10-04 10:09                               ` Dmitry Gutov
2023-10-04 17:55                               ` Chris Hanson
2023-10-04 22:49                               ` Paul Eggert
2023-10-04 22:54                                 ` Dmitry Gutov
2023-10-05  5:50                                   ` Eli Zaretskii
2023-10-05 10:48                                     ` Dmitry Gutov
2023-10-06  5:34                                       ` Eli Zaretskii
2023-10-05  5:49                                 ` Eli Zaretskii
2023-10-04  4:11                         ` Gerd Möllmann
2023-10-04  6:55                         ` Eli Zaretskii
2023-10-03  7:32           ` Eli Zaretskii

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=fab8e074-ac2c-41ff-b88c-50db6bc0e680@chris-hanson.org \
    --to=cph@chris-hanson.org \
    --cc=66288@debbugs.gnu.org \
    --cc=eliz@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).