From ef0d83dca236c69d856da57df0732cafb2c02686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dean=20Qui=C3=B1anola?= Date: Sun, 18 Oct 2026 14:38:28 -0700 Subject: [PATCH] perf(docker): precompile Python bytecode in worker images uv pip install does not compile bytecode by default, so the published images ship with no .pyc for runpod, aiohttp, or the worker source. Every fresh container recompiles each imported module on first import, on every cold start. - Pass --compile-bytecode to the build-time uv pip install in all four Dockerfiles (flag, not UV_COMPILE_BYTECODE env, so runtime installs in dependency_installer.py stay uncompiled) - compileall the worker source in /app - Cold-container import runpod.serverless: ~1180ms -> ~600ms (arm64 CPU image, local build); image +10.8MB SLS-809 --- Dockerfile | 5 ++++- Dockerfile-cpu | 5 ++++- Dockerfile-lb | 5 ++++- Dockerfile-lb-cpu | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index ef0d4ad..07782ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -71,8 +71,11 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y --no-ins # Use --python to target the active interpreter (preserves torch in its site-packages) COPY README.md pyproject.toml uv.lock ./ COPY src/ ./ +# Precompile bytecode at build time: a fresh container otherwise recompiles +# every imported module on first import, on every cold start. RUN uv export --format requirements-txt --no-dev --no-hashes > requirements.txt \ - && uv pip install --python $(which python) --break-system-packages -r requirements.txt + && uv pip install --compile-bytecode --python $(which python) --break-system-packages -r requirements.txt \ + && python -m compileall -q . # Install numpy for the active Python version. # The runpod/pytorch image ships torch but not numpy. Flash build excludes numpy diff --git a/Dockerfile-cpu b/Dockerfile-cpu index 48c174f..7ed3d8f 100644 --- a/Dockerfile-cpu +++ b/Dockerfile-cpu @@ -32,7 +32,10 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y --no-ins # Copy app code and install dependencies COPY README.md pyproject.toml uv.lock ./ COPY src/ ./ +# Precompile bytecode at build time: a fresh container otherwise recompiles +# every imported module on first import, on every cold start. RUN uv export --format requirements-txt --no-dev --no-hashes > requirements.txt \ - && uv pip install --system --break-system-packages -r requirements.txt + && uv pip install --compile-bytecode --system --break-system-packages -r requirements.txt \ + && python -m compileall -q . CMD ["python", "handler.py"] diff --git a/Dockerfile-lb b/Dockerfile-lb index ff927c6..7d04f4e 100644 --- a/Dockerfile-lb +++ b/Dockerfile-lb @@ -66,8 +66,11 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y --no-ins # Use --python to target the active interpreter (preserves torch in its site-packages) COPY README.md pyproject.toml uv.lock ./ COPY src/ ./ +# Precompile bytecode at build time: a fresh container otherwise recompiles +# every imported module on first import, on every cold start. RUN uv export --format requirements-txt --no-dev --no-hashes > requirements.txt \ - && uv pip install --python $(which python) --break-system-packages -r requirements.txt + && uv pip install --compile-bytecode --python $(which python) --break-system-packages -r requirements.txt \ + && python -m compileall -q . # Install numpy for the active Python version. # The runpod/pytorch image ships torch but not numpy. Flash build excludes numpy diff --git a/Dockerfile-lb-cpu b/Dockerfile-lb-cpu index f99459f..1d0645f 100644 --- a/Dockerfile-lb-cpu +++ b/Dockerfile-lb-cpu @@ -32,8 +32,11 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y --no-ins # Copy app code and install dependencies COPY README.md pyproject.toml uv.lock ./ COPY src/ ./ +# Precompile bytecode at build time: a fresh container otherwise recompiles +# every imported module on first import, on every cold start. RUN uv export --format requirements-txt --no-dev --no-hashes > requirements.txt \ - && uv pip install --system --break-system-packages -r requirements.txt + && uv pip install --compile-bytecode --system --break-system-packages -r requirements.txt \ + && python -m compileall -q . EXPOSE 80