curl: make --progress-bar update the line less frequently
authorDaniel Stenberg <daniel@haxx.se>
Tue, 30 Jul 2013 22:17:56 +0000 (00:17 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 31 Jul 2013 11:41:00 +0000 (13:41 +0200)
Also, use memset() instead of a lame loop.

The previous logic that tried to avoid too many updates were very
ineffective for really fast transfers, as then it could easily end up
doing hundreds of updates per second that would make a significant
impact in transfer performance!

Bug: http://curl.haxx.se/mail/archive-2013-07/0031.html
Reported-by: Marc Doughty
src/tool_cb_prg.c
src/tool_cb_prg.h

index 7a701b6..9facd9e 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -49,17 +49,19 @@ int tool_progress_cb(void *clientp,
   double percent;
   int barwidth;
   int num;
-  int i;
-
+  time_t now = time(NULL);
   struct ProgressData *bar = (struct ProgressData *)clientp;
+  curl_off_t total;
+  curl_off_t point;
+
+  if(bar->prevtime == now) /* wait with update */
+    return 0;
 
   /* expected transfer size */
-  curl_off_t total = (curl_off_t)dltotal + (curl_off_t)ultotal +
-    bar->initial_size;
+  total = (curl_off_t)dltotal + (curl_off_t)ultotal + bar->initial_size;
 
   /* we've come this far */
-  curl_off_t point = (curl_off_t)dlnow + (curl_off_t)ulnow +
-    bar->initial_size;
+  point = (curl_off_t)dlnow + (curl_off_t)ulnow + bar->initial_size;
 
   if(point > total)
     /* we have got more than the expected total! */
@@ -83,14 +85,14 @@ int tool_progress_cb(void *clientp,
     num = (int) (((double)barwidth) * frac);
     if(num > MAX_BARLENGTH)
       num = MAX_BARLENGTH;
-    for(i = 0; i < num; i++)
-      line[i] = '#';
-    line[i] = '\0';
+    memset(line, '#', num);
+    line[num] = '\0';
     snprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
     fprintf(bar->out, format, line, percent);
   }
   fflush(bar->out);
   bar->prev = point;
+  bar->prevtime = now;
 
   return 0;
 }
index 81915b2..ceb8598 100644 (file)
@@ -7,7 +7,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -29,6 +29,7 @@
 struct ProgressData {
   int         calls;
   curl_off_t  prev;
+  time_t      prevtime;
   int         width;
   FILE       *out;  /* where to write everything to */
   curl_off_t  initial_size;