Skip to content

Commit 93b40f6

Browse files
committed
common : throttle download progress output to reduce IO flush
This change limits progress updates to approximately every 0.1% of the file size to minimize stdio overhead. Also fixes compiler warnings regarding __func__ in lambdas. Signed-off-by: Adrien Gallouët <[email protected]>
1 parent ec61d09 commit 93b40f6

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

common/download.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,36 +517,43 @@ static bool common_pull_file(httplib::Client & cli,
517517
headers.emplace("Range", "bytes=" + std::to_string(existing_size) + "-");
518518
}
519519

520-
std::atomic<size_t> downloaded{existing_size};
520+
const char *func = __func__; // avoid __func__ inside a lambda
521+
size_t downloaded = existing_size;
522+
size_t progress_step = 0;
521523

522524
auto res = cli.Get(resolve_path, headers,
523525
[&](const httplib::Response &response) {
524526
if (existing_size > 0 && response.status != 206) {
525-
LOG_WRN("%s: server did not respond with 206 Partial Content for a resume request. Status: %d\n", __func__, response.status);
527+
LOG_WRN("%s: server did not respond with 206 Partial Content for a resume request. Status: %d\n", func, response.status);
526528
return false;
527529
}
528530
if (existing_size == 0 && response.status != 200) {
529-
LOG_WRN("%s: download received non-successful status code: %d\n", __func__, response.status);
531+
LOG_WRN("%s: download received non-successful status code: %d\n", func, response.status);
530532
return false;
531533
}
532534
if (total_size == 0 && response.has_header("Content-Length")) {
533535
try {
534536
size_t content_length = std::stoull(response.get_header_value("Content-Length"));
535537
total_size = existing_size + content_length;
536538
} catch (const std::exception &e) {
537-
LOG_WRN("%s: invalid Content-Length header: %s\n", __func__, e.what());
539+
LOG_WRN("%s: invalid Content-Length header: %s\n", func, e.what());
538540
}
539541
}
540542
return true;
541543
},
542544
[&](const char *data, size_t len) {
543545
ofs.write(data, len);
544546
if (!ofs) {
545-
LOG_ERR("%s: error writing to file: %s\n", __func__, path_tmp.c_str());
547+
LOG_ERR("%s: error writing to file: %s\n", func, path_tmp.c_str());
546548
return false;
547549
}
548550
downloaded += len;
549-
print_progress(downloaded, total_size);
551+
progress_step += len;
552+
553+
if (progress_step >= total_size / 1000 || downloaded == total_size) {
554+
print_progress(downloaded, total_size);
555+
progress_step = 0;
556+
}
550557
return true;
551558
},
552559
nullptr

0 commit comments

Comments
 (0)