Skip to content
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

poetry startup time is slow #3502

Closed
3 tasks done
yoav-orca opened this issue Dec 19, 2020 · 13 comments
Closed
3 tasks done

poetry startup time is slow #3502

yoav-orca opened this issue Dec 19, 2020 · 13 comments
Labels
kind/enhancement Not a bug or feature, but improves usability or performance
Milestone

Comments

@yoav-orca
Copy link

  • I am on the latest Poetry version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).
  • OS version and name: macOS cataline
  • Poetry version: 1.1.4
  • The contents of your pyproject.toml file:
[tool.poetry]
name = "poetry_slow_startup"
version = "0.1.0"
description = ""
authors = ["John Doe <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Issue

I'll start by saying that poetry is awesome, it made me and my team more productive since we migrated from Pipenv!

I use the poetry run/shell commands a lot in my daily routine, I also use poetry as a wrapper to other tools that I use from my IDE & git hooks. However, there's an overhead when running commands with poetry:

$ time /usr/bin/true
/usr/bin/true  0.00s user 0.00s system 69% cpu 0.003 total
$ time /usr/bin/true
/usr/bin/true  0.00s user 0.00s system 72% cpu 0.006 total
$ time /usr/bin/true
/usr/bin/true  0.00s user 0.00s system 78% cpu 0.005 total

# now with poetry

$ time poetry run /usr/bin/true
poetry run /usr/bin/true  0.54s user 0.15s system 72% cpu 0.944 total
$ time poetry run /usr/bin/true
poetry run /usr/bin/true  0.48s user 0.09s system 98% cpu 0.581 total
$ time poetry run /usr/bin/true
poetry run /usr/bin/true  0.52s user 0.14s system 78% cpu 0.844 total

It seems that running commands with poetry adds between 570-930ms latency to the command.

I tried to profile the issue, and it seems that most of the time is spent in the import statements before the main is called, the main itself is only 90ms.
I tried to profile the imports with PYTHONPROFILEIMPORTTIME as follows:

PYTHONPROFILEIMPORTTIME=1 poetry run /usr/bin/true 2>imports.txt

I visualized the results using tuna as follows:

tuna imports.txt

image

The results indicate that most of the time is used in imports that are not used by the run/shell commands.
I propose using LazyLoader to improve the startup time and responsiveness of poetry.

@yoav-orca yoav-orca added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Dec 19, 2020
@sinoroc
Copy link

sinoroc commented Dec 19, 2020

Probably could have been filed as "feature request" instead of "bug".

@yoav-orca
Copy link
Author

@sinoroc I wasn't sure, because it didn't feel like a feature request per se, because older versions of poetry were faster (poetry 1.0 is consistently under 350ms), but I can change the label

@sinoroc
Copy link

sinoroc commented Dec 19, 2020

@sinoroc I wasn't sure, because it didn't feel like a feature request per se, because older versions of poetry were faster (poetry 1.0 is consistently under 350ms), but I can change the label

No worries. I just mentioned it. Maintainers will decide.

Maybe it is worth editing the first message to clearly show that it could be a regression (i.e. it used to be faster).

@finswimmer finswimmer added kind/enhancement Not a bug or feature, but improves usability or performance and removed kind/bug Something isn't working as expected labels Dec 20, 2020
@sdispater sdispater added this to the 1.2 milestone Dec 21, 2020
@jules-ch
Copy link
Contributor

A bunch of imports for commands need to be moved to handle to delay imports.
It's done like that for some commands & imports but not all.

@yoav-orca
Copy link
Author

@jules-ch could you point me to specific candidates, I'll try to send a PR

@jules-ch
Copy link
Contributor

#3618 might help. I think we can wait & see if that improves performance.

@yoav-orca
Copy link
Author

@jules-ch I gave poetry 1.1.5 a try, it did slightly improve things, but it's still about ~500ms slower than native code run.
I re-run tuna to see where the time is spent on imports, the situation is not very different, most of the time is spent before the main logic happens.

image

abn added a commit to abn/poetry that referenced this issue Apr 13, 2021
This change speeds up poetry command start time by 50ms compared to
current master and around 250ms compared to 1.1.5.

Relates-to: python-poetry#3502
@abn
Copy link
Member

abn commented Apr 13, 2021

@yoav-orca can you try master? Should get you to around ~300ms. And #3938 will get you to ~250ms. (obviously times here are dependent on externa factors too).

Using pipx

pipx install --suffix=@3938 'poetry @ git+https://github.com/python-poetry/poetry.git@refs/pull/3938/head'
PYTHONPROFILEIMPORTTIME=1 poetry@3938 run /usr/bin/true 2>imports.txt 
pipx install --suffix=@master 'poetry @ git+https://github.com/python-poetry/poetry.git@master'
PYTHONPROFILEIMPORTTIME=1 poetry@master run /usr/bin/true 2>imports.txt 

@yoav-orca
Copy link
Author

yoav-orca commented Apr 18, 2021

Hi @abn I tried both versions

My setup:
macOS Catalina
Python 3.9.4

Empty poetry

mkdir test-proj
cd test-proj
poetry init -n

With 3938:

» time poetry@3938 run /usr/bin/true
poetry@3938 run /usr/bin/true  0.31s user 0.07s system 97% cpu 0.391 total
» time poetry@3938 run /usr/bin/true
poetry@3938 run /usr/bin/true  0.32s user 0.08s system 97% cpu 0.403 total
» time poetry@3938 run /usr/bin/true
poetry@3938 run /usr/bin/true  0.31s user 0.07s system 97% cpu 0.399 total

At master:

» time poetry@master run /usr/bin/true
poetry@master run /usr/bin/true  0.31s user 0.07s system 97% cpu 0.394 total
» time poetry@master run /usr/bin/true
poetry@master run /usr/bin/true  0.32s user 0.07s system 97% cpu 0.405 total
» time poetry@master run /usr/bin/true
poetry@master run /usr/bin/true  0.31s user 0.07s system 97% cpu 0.396 total

The diagram looks very similar to both branches, I'm adding the diagram for 3938:
image

BTW when running on a non-empty project (~50 dependencies) I see a spike of 100-150 ms on startup time.

neersighted pushed a commit to abn/poetry that referenced this issue Nov 11, 2021
This change speeds up poetry command start time by 50ms compared to
current master and around 250ms compared to 1.1.5.

Relates-to: python-poetry#3502
neersighted pushed a commit that referenced this issue Nov 11, 2021
This change speeds up poetry command start time by 50ms compared to
current master and around 250ms compared to 1.1.5.

Relates-to: #3502
1nF0rmed pushed a commit to 1nF0rmed/poetry that referenced this issue Nov 15, 2021
This change speeds up poetry command start time by 50ms compared to
current master and around 250ms compared to 1.1.5.

Relates-to: python-poetry#3502
edvardm pushed a commit to edvardm/poetry that referenced this issue Nov 24, 2021
This change speeds up poetry command start time by 50ms compared to
current master and around 250ms compared to 1.1.5.

Relates-to: python-poetry#3502
@lafrenierejm
Copy link
Contributor

lafrenierejm commented Jun 11, 2022

@abn, @yoav-orca May I ask what the target performance is for this issue to be considered resolved?

Based on yoav-orca's latest comment, it looked like the timing of master at that point in time was ~350ms and similar to 1.0's timing:

older versions of poetry were faster (poetry 1.0 is consistently under 350ms)

@systematicguy
Copy link

We really need this to get going/resolved as it is the sole serious issue in the 1.2 project besides another one.

archlinux-github pushed a commit to archlinux/infrastructure that referenced this issue Aug 14, 2022
"poetry run" is very slow[1] and adds +1 second to the startup time.
This is made even worse by the fact that aurweb-git-serve is called
twice by sshd[2].

[1] python-poetry/poetry#3502
[2] https://security.stackexchange.com/questions/123795/authorizedkeyscommand-of-sshd-config-getting-called-twice/123801#123801
@neersighted
Copy link
Member

I'm going to close this as current speeds are not a regression -- I'd suggest opening issues for specific commands/sections of code with specific targets if anyone finds any unnecessary hot loops or blocking behavior.

@mkniewallner mkniewallner removed the status/triage This issue needs to be triaged label Sep 18, 2022
Copy link

github-actions bot commented Mar 1, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/enhancement Not a bug or feature, but improves usability or performance
Projects
None yet
Development

No branches or pull requests

10 participants