-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
uv run
repeatedly compiles bytecode with UV_COMPILE_BYTECODE=1
#12202
Comments
cc @konstin |
We do recompile the entire environment, as opposed to only compiling newly-installed packages. However, IIUC, it's not quite correct to say that the behavior is "ignore the pyc cache and recompile on every invocation of uv run", since bytecode compilation only actually runs if a file's timestamp or size changes. (See, e.g., https://docs.python.org/3/library/py_compile.html#py_compile.PycInvalidationMode.) |
What's causing the timestamp or size to change here? E.g. if I expand my environment a little bit
Then it sure looks like the entire site-packages is getting recompiled on every
|
Look at the difference in time though? 522ms to 40ms. The subsequent runs short-circuit (on a per-file basis) if the timestamp hasn't changed. This is a feature that's built-in to CPython's bytecode compiler, not uv. |
40 ms of extra latency for each
So, OK fair enough that it's not ignoring the cache, but the point remains that it's a surprising and harmful behavior. (I guess a separate issue is whether the log should say "Bytecode compiled n files" if that's not what it's actually doing). |
Why not enable bytecode compilation at install time, then, rather than globally? (I'm not sure that there are other great options here. E.g., if we didn't re-check the bytecode compilation in |
We enable it globally because uv's default behavior w/r/t not compiling during the install is very harmful for containerized workflows. We are trying to help disarm this footgun for our users. And there is not always a clear "install time" distinction from the platform's perspective. Still, we will need to work on that if this is expected behavior for uv, because this outcome is probably worse. FWIW, I am not a sophisticated user of uv (just passing on issues are users are having with it), but I don't think I really understand the rationale here. My mental model is that |
Do you mean that Python itself won't invalidate the pyc cache / write new pyc files when the program executes? Or just that uv won't do "just in time" pre-compilation in this case? It's not obvious to me that pre-compilation is very meaningful in the |
That makes sense. We typically recommend setting this in (e.g.) the install commands within the Dockerfile.
The latter. This is good feedback, we'll think on it, thanks for engaging. We should consider skipping bytecode compilation in |
Yeah, everything would be easier if we could assume users would carefully read all the documentation for their tools :) In our case (providing a platform for containerized applications), users can experience significantly degraded performance if they don't realize that |
Summary
Starting with uv 0.5.5,
uv run
will compile bytecode files on every invocation whenUV_COMPILE_BYTECODE=1
is set:While not shown here, all packages in the venv get their bytecode recompiled, so this can add a lot of latency for large environments.
My understanding is that
UV_COMPILE_BYTECODE
should be telling uv to compile bytecode when installing packages:Instead, it seems to be effectively setting "ignore the pyc cache and recompile on every invocation of
uv run
". Maybe that's expected on your end, but we found it very surprising.Note that this behavior was introduced in 0.5.5. I'm guessing it's a result of this PR.
Platform
Linux 5.15.0-101.103.2.1.el9uek.x86_64 x86_64 GNU/Linux
Version
uv 0.6.6 (as mentioned, the behavior was introduced in 0.5.5)
Python version
Python 3.9.18 when making the repro, but it's not Python version dependent
The text was updated successfully, but these errors were encountered: