Skip to content

Commit e61a864

Browse files
committed
PERF: download in chunks of 256 kB + limit progress bar to 5 refresh/s
1 parent 6b54c19 commit e61a864

File tree

5 files changed

+17
-12
lines changed

5 files changed

+17
-12
lines changed

news/12810.feature.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Improve download performance. Download packages and update the
2+
progress bar in larger chunks of 256 kB, up from 10 kB.
3+
Limit the progress bar to 5 refresh per second.
4+
Improve hash performance. Read package files in larger chunks of 1 MB,
5+
up from 8192 bytes.

src/pip/_internal/cli/progress_bars.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _rich_progress_bar(
4949
TimeRemainingColumn(),
5050
)
5151

52-
progress = Progress(*columns, refresh_per_second=30)
52+
progress = Progress(*columns, refresh_per_second=5)
5353
task_id = progress.add_task(" " * (get_indentation() + 2), total=total)
5454
with progress:
5555
for chunk in iterable:

src/pip/_internal/network/download.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
from typing import Iterable, Optional, Tuple
99

10-
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response
10+
from pip._vendor.requests.models import Response
1111

1212
from pip._internal.cli.progress_bars import get_download_progress_renderer
1313
from pip._internal.exceptions import NetworkConnectionError
@@ -61,7 +61,7 @@ def _prepare_download(
6161
else:
6262
show_progress = False
6363

64-
chunks = response_chunks(resp, CONTENT_CHUNK_SIZE)
64+
chunks = response_chunks(resp)
6565

6666
if not show_progress:
6767
return chunks

src/pip/_internal/network/utils.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict, Generator
22

3-
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response
3+
from pip._vendor.requests.models import Response
44

55
from pip._internal.exceptions import NetworkConnectionError
66

@@ -25,6 +25,8 @@
2525
# possible to make this work.
2626
HEADERS: Dict[str, str] = {"Accept-Encoding": "identity"}
2727

28+
DOWNLOAD_CHUNK_SIZE = 256 * 1024
29+
2830

2931
def raise_for_status(resp: Response) -> None:
3032
http_error_msg = ""
@@ -55,7 +57,7 @@ def raise_for_status(resp: Response) -> None:
5557

5658

5759
def response_chunks(
58-
response: Response, chunk_size: int = CONTENT_CHUNK_SIZE
60+
response: Response, chunk_size: int = DOWNLOAD_CHUNK_SIZE
5961
) -> Generator[bytes, None, None]:
6062
"""Given a requests Response, provide the data chunks."""
6163
try:

src/pip/_internal/utils/misc.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import errno
22
import getpass
33
import hashlib
4-
import io
54
import logging
65
import os
76
import posixpath
@@ -70,6 +69,8 @@
7069
OnExc = Callable[[FunctionType, Path, BaseException], Any]
7170
OnErr = Callable[[FunctionType, Path, ExcInfo], Any]
7271

72+
FILE_CHUNK_SIZE = 1024 * 1024
73+
7374

7475
def get_pip_version() -> str:
7576
pip_pkg_dir = os.path.join(os.path.dirname(__file__), "..", "..")
@@ -122,9 +123,7 @@ def get_prog() -> str:
122123
# Retry every half second for up to 3 seconds
123124
@retry(stop_after_delay=3, wait=0.5)
124125
def rmtree(
125-
dir: str,
126-
ignore_errors: bool = False,
127-
onexc: Optional[OnExc] = None,
126+
dir: str, ignore_errors: bool = False, onexc: Optional[OnExc] = None
128127
) -> None:
129128
if ignore_errors:
130129
onexc = _onerror_ignore
@@ -313,7 +312,7 @@ def is_installable_dir(path: str) -> bool:
313312

314313

315314
def read_chunks(
316-
file: BinaryIO, size: int = io.DEFAULT_BUFFER_SIZE
315+
file: BinaryIO, size: int = FILE_CHUNK_SIZE
317316
) -> Generator[bytes, None, None]:
318317
"""Yield pieces of data from a file-like object until EOF."""
319318
while True:
@@ -643,8 +642,7 @@ def pairwise(iterable: Iterable[Any]) -> Iterator[Tuple[Any, Any]]:
643642

644643

645644
def partition(
646-
pred: Callable[[T], bool],
647-
iterable: Iterable[T],
645+
pred: Callable[[T], bool], iterable: Iterable[T]
648646
) -> Tuple[Iterable[T], Iterable[T]]:
649647
"""
650648
Use a predicate to partition entries into false entries and true entries,

0 commit comments

Comments
 (0)