progress/trspeed: use a local convenient pointer to beautify code

The function becomes easier to read and understand with less repetition.
This commit is contained in:
Daniel Stenberg 2021-05-08 13:14:42 +02:00
parent 8a75224a69
commit 1e19eceb50
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -382,76 +382,69 @@ static curl_off_t trspeed(curl_off_t size, /* number of bytes */
static bool progress_calc(struct Curl_easy *data, struct curltime now) static bool progress_calc(struct Curl_easy *data, struct curltime now)
{ {
bool timetoshow = FALSE; bool timetoshow = FALSE;
struct Progress * const p = &data->progress;
/* The time spent so far (from the start) in microseconds */ /* The time spent so far (from the start) in microseconds */
data->progress.timespent = Curl_timediff_us(now, data->progress.start); p->timespent = Curl_timediff_us(now, p->start);
data->progress.dlspeed = trspeed(data->progress.downloaded, p->dlspeed = trspeed(p->downloaded, p->timespent);
data->progress.timespent); p->ulspeed = trspeed(p->uploaded, p->timespent);
data->progress.ulspeed = trspeed(data->progress.uploaded,
data->progress.timespent);
/* Calculations done at most once a second, unless end is reached */ /* Calculations done at most once a second, unless end is reached */
if(data->progress.lastshow != now.tv_sec) { if(p->lastshow != now.tv_sec) {
int countindex; /* amount of seconds stored in the speeder array */ int countindex; /* amount of seconds stored in the speeder array */
int nowindex = data->progress.speeder_c% CURR_TIME; int nowindex = p->speeder_c% CURR_TIME;
data->progress.lastshow = now.tv_sec; p->lastshow = now.tv_sec;
timetoshow = TRUE; timetoshow = TRUE;
/* Let's do the "current speed" thing, with the dl + ul speeds /* Let's do the "current speed" thing, with the dl + ul speeds
combined. Store the speed at entry 'nowindex'. */ combined. Store the speed at entry 'nowindex'. */
data->progress.speeder[ nowindex ] = p->speeder[ nowindex ] = p->downloaded + p->uploaded;
data->progress.downloaded + data->progress.uploaded;
/* remember the exact time for this moment */ /* remember the exact time for this moment */
data->progress.speeder_time [ nowindex ] = now; p->speeder_time [ nowindex ] = now;
/* advance our speeder_c counter, which is increased every time we get /* advance our speeder_c counter, which is increased every time we get
here and we expect it to never wrap as 2^32 is a lot of seconds! */ here and we expect it to never wrap as 2^32 is a lot of seconds! */
data->progress.speeder_c++; p->speeder_c++;
/* figure out how many index entries of data we have stored in our speeder /* figure out how many index entries of data we have stored in our speeder
array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
transfer. Imagine, after one second we have filled in two entries, transfer. Imagine, after one second we have filled in two entries,
after two seconds we've filled in three entries etc. */ after two seconds we've filled in three entries etc. */
countindex = ((data->progress.speeder_c >= CURR_TIME)? countindex = ((p->speeder_c >= CURR_TIME)? CURR_TIME:p->speeder_c) - 1;
CURR_TIME:data->progress.speeder_c) - 1;
/* first of all, we don't do this if there's no counted seconds yet */ /* first of all, we don't do this if there's no counted seconds yet */
if(countindex) { if(countindex) {
int checkindex; int checkindex;
timediff_t span_ms; timediff_t span_ms;
curl_off_t amount;
/* Get the index position to compare with the 'nowindex' position. /* Get the index position to compare with the 'nowindex' position.
Get the oldest entry possible. While we have less than CURR_TIME Get the oldest entry possible. While we have less than CURR_TIME
entries, the first entry will remain the oldest. */ entries, the first entry will remain the oldest. */
checkindex = (data->progress.speeder_c >= CURR_TIME)? checkindex = (p->speeder_c >= CURR_TIME)? p->speeder_c%CURR_TIME:0;
data->progress.speeder_c%CURR_TIME:0;
/* Figure out the exact time for the time span */ /* Figure out the exact time for the time span */
span_ms = Curl_timediff(now, data->progress.speeder_time[checkindex]); span_ms = Curl_timediff(now, p->speeder_time[checkindex]);
if(0 == span_ms) if(0 == span_ms)
span_ms = 1; /* at least one millisecond MUST have passed */ span_ms = 1; /* at least one millisecond MUST have passed */
/* Calculate the average speed the last 'span_ms' milliseconds */ /* Calculate the average speed the last 'span_ms' milliseconds */
{ amount = p->speeder[nowindex]- p->speeder[checkindex];
curl_off_t amount = data->progress.speeder[nowindex]-
data->progress.speeder[checkindex];
if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */) if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
/* the 'amount' value is bigger than would fit in 32 bits if /* the 'amount' value is bigger than would fit in 32 bits if
multiplied with 1000, so we use the double math for this */ multiplied with 1000, so we use the double math for this */
data->progress.current_speed = (curl_off_t) p->current_speed = (curl_off_t)
((double)amount/((double)span_ms/1000.0)); ((double)amount/((double)span_ms/1000.0));
else else
/* the 'amount' value is small enough to fit within 32 bits even /* the 'amount' value is small enough to fit within 32 bits even
when multiplied with 1000 */ when multiplied with 1000 */
data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms; p->current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
}
} }
else else
/* the first second we use the average */ /* the first second we use the average */
data->progress.current_speed = p->current_speed = p->ulspeed + p->dlspeed;
data->progress.ulspeed + data->progress.dlspeed;
} /* Calculations end */ } /* Calculations end */
return timetoshow; return timetoshow;