]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
curl: improve non-blocking STDIN performance
authorDoI <5291556+denandz@users.noreply.github.com>
Tue, 10 Jun 2025 08:59:09 +0000 (20:59 +1200)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 25 Jun 2025 20:58:53 +0000 (22:58 +0200)
Using a select() call on supported platforms to check for data with a
given timeout

Closes #17566

src/tool_cb_rea.c

index 18b8d7b47aeea7360fbd5e4224b5c8320940f98d..2ceca242b65167b3136fdc724d5dd3c0a17da859 100644 (file)
@@ -144,6 +144,7 @@ int tool_readbusy_cb(void *clientp,
 {
   struct per_transfer *per = clientp;
   struct OperationConfig *config = per->config;
+  static curl_off_t ulprev;
 
   (void)dltotal;  /* unused */
   (void)dlnow;  /* unused */
@@ -151,33 +152,35 @@ int tool_readbusy_cb(void *clientp,
   (void)ulnow;  /* unused */
 
   if(config->readbusy) {
-    /* lame code to keep the rate down because the input might not deliver
-       anything, get paused again and come back here immediately */
-    static timediff_t rate = 500;
-    static struct curltime prev;
-    static curl_off_t ulprev;
-
     if(ulprev == ulnow) {
-      /* it did not upload anything since last call */
-      struct curltime now = curlx_now();
-      if(prev.tv_sec)
-        /* get a rolling average rate */
-        rate -= rate/4 - curlx_timediff(now, prev)/4;
-      prev = now;
-    }
-    else {
-      rate = 50;
-      ulprev = ulnow;
-    }
-    if(rate >= 50) {
-      /* keeps the looping down to 20 times per second in the crazy case */
-      config->readbusy = FALSE;
-      curl_easy_pause(per->curl, CURLPAUSE_CONT);
+#ifndef _WIN32
+      fd_set bits;
+      struct timeval timeout;
+      /* wait this long at the most */
+      timeout.tv_sec = 0;
+      timeout.tv_usec = 1000;
+
+      FD_ZERO(&bits);
+#if defined(__DJGPP__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Warith-conversion"
+#endif
+      FD_SET(per->infd, &bits);
+#if defined(__DJGPP__)
+#pragma GCC diagnostic pop
+#endif
+      select(per->infd + 1, &bits, NULL, NULL, &timeout);
+#else
+      /* sleep */
+      curlx_wait_ms(1);
+#endif
     }
-    else
-      /* sleep half a period */
-      curlx_wait_ms(25);
+
+    config->readbusy = FALSE;
+    curl_easy_pause(per->curl, CURLPAUSE_CONT);
   }
 
+  ulprev = ulnow;
+
   return per->noprogress ? 0 : CURL_PROGRESSFUNC_CONTINUE;
 }