Subject: bug#78773: [PATCH] Speedup url-retrieve-synchronously for low-latency connections

From: Steven Allen <[email-masked]>
Date: Fri, 13 Jun 2025 07:50:59 -0700

Robert Pluim <[email-masked]> writes:

>>>>>> On Fri, 13 Jun 2025 13:48:08 +0300, Eli Zaretskii <[email-masked]> said:
>
>     >> Iʼm wondering why weʼre setting the timeout to zero and calling
>     >> `pselect' again, which means recomputing the wait set again, if I
>     >> havenʼt gotten lost in the twisty maze of ifʼs and global variables
>     >> 😀. Weʼve got input, why canʼt we just `break'? There might be some
>     >> work we donʼt do, but that will get done the next time
>     >> `wait_reading_process_output' is called.
>
>     Eli> Try looking at the Git history of this code, maybe it will tell you
>     Eli> how this code evolved, and bring up past discussions and bug reports
>     Eli> which caused us to make the code as it is today.
>
> Yes, itʼs gnarly. I think the following would be ok, and it passes the
> test case for Bug#20978.
>
> But I canʼt detect any difference in Stevenʼs test case, so maybe we
> let sleeping pselectʼs lie.

I think this patch will cause us to the final drain loop when waiting on
a single connection. It's probably fine, but I assume this drain loop
existed for a reason?

  https://cgit.git.savannah.gnu.org/cgit/emacs.git/tree/src/process.c?h=bec823b107ef7d3b51b8e430ccab82c81bd63d24#n5525

> Robert
> -- 
> diff --git a/src/process.c b/src/process.c
> index e61ec425f7e..0119330f7fe 100644
> --- a/src/process.c
> +++ b/src/process.c
> @@ -5934,6 +5934,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
>        channel_start
>  	= process_prioritize_lower_fds ? 0 : last_read_channel + 1;
>  
> +      bool break_now = false;
>        for (channel = channel_start, wrapped = false;
>  	   !wrapped || (channel < channel_start && channel <= max_desc);
>  	   channel++)
> @@ -5975,8 +5976,11 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
>  	      if (nread > 0)
>  		{
>  		  /* Vacuum up any leftovers without waiting.  */
> -		  if (wait_proc == XPROCESS (proc))
> -		    wait = MINIMUM;
> +		  if (!wait_proc || wait_proc == XPROCESS (proc))
> +		    {
> +		      wait = MINIMUM;
> +		      break_now = true;
> +		    }
>  		  /* Since read_process_output can run a filter,
>  		     which can call accept-process-output,
>  		     don't try to read from any other processes
> @@ -6119,6 +6123,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
>  		}
>  	    }
>  	}			/* End for each file descriptor.  */
> +      if (break_now)		/* Don't rerun select.  */
> +	break;
>      }				/* End while exit conditions not met.  */
>  
>    unbind_to (count, Qnil);