TExcelTAD

Why uv Is Fast

uv's headline is speed: Astral's benchmarks show installs 10–100× faster than pip, and on a warm cache creating an environment and installing a dozen packages takes milliseconds. "It's written in Rust" is part of the answer, but most of the gain comes from design decisions that any installer could make. Knowing them also tells you how to keep uv fast in CI.

Where the time goes in an install

Step pip uv
Start-up import a large Python program one native binary
Fetch index pages and metadata one at a time in parallel; reads only the METADATA of a wheel (PEP 658 metadata files, or HTTP range requests into the zip) instead of downloading it
Resolve backtracking (resolvelib) PubGrub: learns from conflicts, rarely backtracks far
Download wheels sequentially in parallel
Unpack unzip each wheel into the environment unzip once into a global cache, then link files into each environment
Compile .pyc yes, at install time no (compiled lazily on first import; --compile-bytecode for production images)

The cache, and linking instead of copying

uv keeps every wheel it has ever unpacked in its cache (~/.cache/uv on Linux). Installing the same version into a second environment needs no download and no unzip: uv just links the files. On Linux the default is hardlinks when the cache and the environment are on the same filesystem; macOS uses copy-on-write clones. (This container uses UV_LINK_MODE=copy, so the session below asks for hardlinks explicitly.)

bash · try it yourself
uv cache dir
uv venv -q a && uv venv -q b
echo "=== first environment: cold cache"
uv pip install --python a --link-mode hardlink rich==15.0.0 2>&1
echo "=== second environment: warm cache"
uv pip install --python b --link-mode hardlink rich==15.0.0 2>&1
echo "=== the same file, three names"
stat -c '%h links: %n' a/lib/python3.13/site-packages/rich/console.py b/lib/python3.13/site-packages/rich/console.py
/home/learner/.cache/uv
=== first environment: cold cache
Using Python 3.13.17 environment at: a
Resolved 4 packages in 30ms
Downloading pygments (1.2MiB)
 Downloaded pygments
Prepared 4 packages in 40ms
Installed 4 packages in 19ms
 + markdown-it-py==4.2.0
 + mdurl==0.1.2
 + pygments==2.21.0
 + rich==15.0.0
=== second environment: warm cache
Using Python 3.13.17 environment at: b
Resolved 4 packages in 19ms
Installed 4 packages in 21ms
 + markdown-it-py==4.2.0
 + mdurl==0.1.2
 + pygments==2.21.0
 + rich==15.0.0
=== the same file, three names
3 links: a/lib/python3.13/site-packages/rich/console.py
3 links: b/lib/python3.13/site-packages/rich/console.py

The second install has no Prepared step at all: nothing was downloaded or unpacked. Both environments share the same bytes on disk with the cache (three links to one file), so a hundred environments with the same dependencies cost barely more space than one.

Warning: A hardlinked file is the same file in every environment. Editing an installed package in place (a debugging hack people do) changes it everywhere. If you need to do that, reinstall with --link-mode copy first.

A runnable model: parallel downloads

The biggest win on a cold cache is overlapping network waits. This model "downloads" a set of wheels with fixed latencies one after another, then with a pool of 4 parallel slots, scheduling each file on the slot that frees up first:

python · try it yourself
import heapq

files = {"rich": 0.18, "pygments": 0.40, "markdown-it-py": 0.12, "mdurl": 0.05,
         "requests": 0.10, "urllib3": 0.14, "idna": 0.07, "certifi": 0.16,
         "charset-normalizer": 0.20, "attrs": 0.09}

sequential = sum(files.values())

def parallel(durations, slots):
    finish = [0.0] * slots                    # time each slot becomes free
    heapq.heapify(finish)
    for d in sorted(durations, reverse=True): # longest first: a simple scheduling heuristic
        start = heapq.heappop(finish)
        heapq.heappush(finish, start + d)
    return max(finish)

print(f"{len(files)} wheels, sequential: {sequential:.2f}s")
for slots in (2, 4, 8, 16):
    t = parallel(files.values(), slots)
    print(f"{slots:>2} parallel slots: {t:.2f}s  ({sequential / t:.1f}x faster)")
10 wheels, sequential: 1.51s
 2 parallel slots: 0.78s  (1.9x faster)
 4 parallel slots: 0.40s  (3.8x faster)
 8 parallel slots: 0.40s  (3.8x faster)
16 parallel slots: 0.40s  (3.8x faster)

The speed-up stops growing once there are more slots than files worth splitting: the total can never be shorter than the slowest single download (pygments here). Real installers face the same limit, plus bandwidth.

Keeping uv fast in CI

  • Cache ~/.cache/uv between runs (GitHub Actions: astral-sh/setup-uv with enable-cache: true). A warm cache turns installs into linking.
  • Prune before saving the cache: uv cache prune --ci removes wheels that are cheap to re-download and keeps the ones built from source, which are expensive to rebuild.
  • Use uv sync --locked: with a lockfile there's nothing to resolve, only to download and link.
  • In Docker, mount the cache during the build (RUN --mount=type=cache,target=/root/.cache/uv uv sync --locked) and set UV_LINK_MODE=copy (the cache mount is a different filesystem, so hardlinks are impossible anyway) and UV_COMPILE_BYTECODE=1 so the image starts fast.

Test yourself

Why was the second uv pip install of the same packages so cheap?

  1. It skipped resolution
  2. The wheels were already unpacked in uv's global cache, so uv only linked the files into the new environment
  3. pip had cached them
  4. It installed nothing

How does uv avoid downloading a whole wheel just to read its dependencies?

  1. It guesses
  2. It fetches the separately served METADATA file (PEP 658) or uses HTTP range requests to read only the metadata from the zip
  3. It asks pip
  4. It doesn't

Why set UV_COMPILE_BYTECODE=1 when building a production Docker image?

  1. It makes installs faster
  2. uv skips .pyc compilation by default; compiling at build time makes the container start faster
  3. It's required for wheels
  4. It shrinks the image