MichaelAquilina / zsh-autoswitch-virtualenv

🐍 ZSH plugin to automatically switch python virtualenvs (including pipenv and poetry) as you move between directories
GNU General Public License v3.0
507 stars 80 forks source link

`poetry env list` is slow #158

Closed ryaminal closed 2 years ago

ryaminal commented 3 years ago

I was frustrated with why on almost every command I do in a poetry project(ls, exa, vi a file then exit) there is a large time penalty.

I put in some basic time measurement around the poetry env list command and it was, on average, taking 600ms. This was quite frustrating. I'm not certain why it's so slow but it is.

This change adds the use of the VIRTUAL_ENV environment variable if it exists else it using poetry env list. VIRTUAL_ENV isn't there until the first call to activate so we have to pay the poetry env list penalty at least once.

VIRTUAL_ENV is also checked in the _maybeworkon call, and the _activate_poetry call is only used to indicate if a message might be printed and if the _default_env should be used. Perhaps a better change would be to have _maybeworkon return a value that can be used to indicate if mkvenv should be run or something.

MichaelAquilina commented 3 years ago

Seems like a reasonable change. I'll test this out locally for a few days before merging it in as has the potential to break at lot.

Could you also add a test case for this change?

I would also suggest you reach out to the poetry devs about the slow command speed in case there is anything they can do to improve it :)

ryaminal commented 3 years ago

I've added a test and it runs on my machine but not circleci, so I'll try and figure that out.

I also wonder if this is something that should be used for pipenv as well.

Maybe something is mucked up with my machine/env or something, but pipenv takes ~98ms to run PIPENV_IGNORE_VIRTUALENVS=1 pipenv --venv and poetry take ~600ms to run poetry env list --full-path. This is an expensive/annoying delay every time I run a command in an autoswitchable directory.

ryaminal commented 3 years ago

Had to install poetry for the unit test to pass. Adds 3s to install poetry, but hopefully doesn't cause any other headache.

ryaminal commented 2 years ago

Closing because #160 is a better and less-risky solution.