perf: little improvements here and there

This commit is contained in:
imSp4rky
2026-07-16 11:02:34 -06:00
parent 686d996346
commit 094c598ef5
8 changed files with 131 additions and 52 deletions
+17 -3
View File
@@ -586,6 +586,9 @@ def requests(
total_bytes = 0
start_time = time.time()
last_speed_report = start_time
last_hedge_check = 0.0
hedge_median = 0.0 # cached; recomputed only when seg_durations grows
hedge_median_len = 0
pool = ThreadPoolExecutor(max_workers=max_workers)
event_queue: Queue[dict[str, Any]] = Queue()
@@ -672,9 +675,20 @@ def requests(
yield dict(downloaded=f"{filesize.decimal(download_speed)}/s")
last_speed_report = now
# hedge stuck segments once spare workers exist (pending < max_workers)
if len(pending) < max_workers and seg_durations and not DOWNLOAD_CANCELLED.is_set():
threshold = max(HEDGE_FACTOR * statistics.median(seg_durations), HEDGE_MIN_WAIT)
# hedge stuck segments once spare workers exist (pending < max_workers);
# throttled to ~0.5s and median recomputed only when seg_durations grows
if (
len(pending) < max_workers
and seg_durations
and now - last_hedge_check > 0.5
and not DOWNLOAD_CANCELLED.is_set()
):
last_hedge_check = now
cur_len = len(seg_durations) # atomic read; appended under seg_lock by workers
if cur_len != hedge_median_len:
hedge_median = statistics.median(seg_durations)
hedge_median_len = cur_len
threshold = max(HEDGE_FACTOR * hedge_median, HEDGE_MIN_WAIT)
with seg_lock:
stuck = [
i