From c19441bc8cb2da064e4ad44d77dc04ab8bbb22ec Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Mon, 1 Jun 2026 17:46:33 +0200 Subject: [PATCH] perf(python): add --compile-bytecode to uv pip install (#9393) Python jobs under nsjail experience slow imports (~465ms for `import requests`) because dependency directories are mounted read-only. Without pre-compiled `.pyc` files, Python recompiles `.py` source to bytecode in-memory on every import in every fresh nsjail process, paying the cost repeatedly. Add `--compile-bytecode` to both `uv pip install` invocations (the Rust-driven install in python_executor.rs and the nsjail download_deps.py.sh script) so `.pyc` files are generated at install time and available at runtime even through read-only mounts. The files are included in both the local cache and S3 piptar uploads. The flag is supported by the uv version (0.9.24+) shipped in the Dockerfile. The existing `__pycache__` skip only filters top-level dirs for dedup logic, not subdirs within packages, so there is no conflict. Fixes WIN-2001 Co-authored-by: Claude Opus 4.8 (1M context) --- backend/windmill-worker/nsjail/download_deps.py.sh | 1 + backend/windmill-worker/src/python_executor.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/backend/windmill-worker/nsjail/download_deps.py.sh b/backend/windmill-worker/nsjail/download_deps.py.sh index ce056e7e76..13fc00ddf9 100755 --- a/backend/windmill-worker/nsjail/download_deps.py.sh +++ b/backend/windmill-worker/nsjail/download_deps.py.sh @@ -31,6 +31,7 @@ $PY_PATH $INDEX_URL_ARG $EXTRA_INDEX_URL_ARG $TRUSTED_HOST_ARG --system --reinstall +--compile-bytecode " echo $CMD diff --git a/backend/windmill-worker/src/python_executor.rs b/backend/windmill-worker/src/python_executor.rs index 4bec8d44bf..3895c823b7 100644 --- a/backend/windmill-worker/src/python_executor.rs +++ b/backend/windmill-worker/src/python_executor.rs @@ -2095,6 +2095,9 @@ async fn spawn_uv_install( "--no-cache", // If we invoke uv pip install, then we want to overwrite existing data "--reinstall", + // Compile .py to .pyc at install time so imports are fast even + // through read-only nsjail mounts (no in-memory compilation per job). + "--compile-bytecode", ]; if let Some(py_path) = py_path.as_ref() {