Subject: Re: bug#26397: 25.1; call-process slow on macOS and slower on larger frames

From: Aaron Jensen <[email-masked]>
Date: Sun, 9 Apr 2017 08:07:26 -0700

That appears to work for me. Here's the patch I'm trying out:

https://gist.githubusercontent.com/aaronjensen/28e26a6c6c0dc767176bc35d5545d10e/raw/7fbbfa48fcf26d083e38cec9f174cef911a806ca/GNU-Emacs-25.1-OS-X-enable-vfork.patch

=======================================================

>From 4a9221e2332d85d64978719a43f884b92bf6cb7b Mon Sep 17 00:00:00 2001
From: Aaron Jensen <[email-masked]>
Date: Sat, 8 Apr 2017 08:32:57 -0700
Subject: [PATCH 1/2] Enable vfork on macOS

---
 src/conf_post.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/conf_post.h b/src/conf_post.h
index 209f60792c..04c5cd6cf6 100644
--- a/src/conf_post.h
+++ b/src/conf_post.h
@@ -87,8 +87,8 @@ typedef bool bool_bf;
    (make-comint "test0" "/nodir/nofile" nil "") when /nodir/nofile
    does not exist.  Also, setsid is not allowed in the vfork child's
    context as of Darwin 9/Mac OS X 10.5.  */
-#undef HAVE_WORKING_VFORK
-#define vfork fork
+/* #undef HAVE_WORKING_VFORK */
+/* #define vfork fork */
 #endif  /* DARWIN_OS */

 /* If HYBRID_MALLOC is defined (e.g., on Cygwin), emacs will use
-- 
2.12.2

>From 9a15fda33bf2b369207701b66cd8f93e2d3a8801 Mon Sep 17 00:00:00 2001
From: Aaron Jensen <[email-masked]>
Date: Sun, 9 Apr 2017 07:43:25 -0700
Subject: [PATCH 2/2] Only vfork if not pty

---
 src/process.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/process.c b/src/process.c
index 7ab92b0102..b49d5970c8 100644
--- a/src/process.c
+++ b/src/process.c
@@ -1811,7 +1811,14 @@ create_process (Lisp_Object process, char
**new_argv, Lisp_Object current_dir)
   int volatile forkerr_volatile = forkerr;
   struct Lisp_Process *p_volatile = p;

-  pid = vfork ();
+  #ifdef DARWIN_OS
+    if (pty_flag)
+      pid = fork ();
+    else
+      pid = vfork ();
+  #else
+    pid = vfork ();
+  #endif

   current_dir = current_dir_volatile;
   lisp_pty_name = lisp_pty_name_volatile;
-- 
2.12.2

On Sun, Apr 9, 2017 at 6:01 AM, Alan Third <[email-masked]> wrote:
> On Sat, Apr 08, 2017 at 08:19:59PM +0100, Alan Third wrote:
>> On Sat, Apr 08, 2017 at 08:47:25AM -0700, Aaron Jensen wrote:
>> > On Sat, Apr 8, 2017 at 12:37 AM, YAMAMOTO Mitsuharu
>> > <[email-masked]> wrote:
>> > >
>> > > Probably "fork" copies some GUI resources.  That would also explain
>> > > why the performance is worse on the Mac port, where each frame
>> > > allocates an extra NSWindow for overlaying.
>> > >
>> > > It becomes much faster and seemingly unaffected by the frame size if
>> > > you comment out "#undef HAVE_WORKING_VFORK" and "#define vfork fork"
>> > > in src/conf_post.h.  But I'm not sure if it is safe.
>> >
>> > Wow, that does make a big difference. The comment says that Emacs
>> > hangs when evaluating:
>> >
>> > (make-comint "test0" "/nodir/nofile" nil "")
>> >
>> > But I can not reproduce that currently with vfork.
>
> I can’t reproduce a hang with that command either.
>
>> > I do not understand the second comment: "Also, setsid is not
>> > allowed in the vfork child's context as of Darwin 9/Mac OS X
>> > 10.5."
>>
>> It looks to me like we could replace the call to setsid with
>>
>>     setpgid (0, 0);
>>
>> for Darwin builds.
>
> No, forget that. It doesn’t do the same thing at all.
>
> If you run Emacs, then
>
>     M‐x ansi-term RET RET
>
> You should get a shell prompt. Using fork/setsid, your shell will be
> able to do job control, but using vfork it can’t. You can test this by
> typing the `bg` command. Zsh (and I assume bash, etc.) responds by
> telling you either there are no jobs, or that the shell has no job
> control.
>
> We could work around this in a rather ugly manner by doing something like:
>
> #ifdef DARWIN_OS
>   if (pty_flag)
>     pid = fork ();
>   else
>     pid = vfork ();
> #else
>   pid = vfork ();
> #endif
>
> which would use fork where we’re expecting to run setsid, and vfork
> otherwise.
>
> --
> Alan Third