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

Improve responsiveness when invoked via Python #9315

Merged
merged 2 commits into from
Dec 31, 2023

Conversation

ofek
Copy link
Contributor

@ofek ofek commented Dec 29, 2023

This saves a handful of milliseconds on Windows and even more on other platforms when running python -m ruff. On non-Windows systems the process is replaced directly (impossible on Windows unfortunately).

❯ docker run --rm python:3.11 bash -c "for i in {1..15}; do python -m timeit -n 1 -r 1 'from pathlib import Path'; done"
1 loop, best of 1: 25.7 msec per loop
1 loop, best of 1: 3.07 msec per loop
1 loop, best of 1: 3.16 msec per loop
1 loop, best of 1: 3.06 msec per loop
1 loop, best of 1: 3.32 msec per loop
1 loop, best of 1: 3.93 msec per loop
1 loop, best of 1: 3.26 msec per loop
1 loop, best of 1: 3.73 msec per loop
1 loop, best of 1: 3.1 msec per loop
1 loop, best of 1: 3.29 msec per loop
1 loop, best of 1: 3.12 msec per loop
1 loop, best of 1: 3.05 msec per loop
1 loop, best of 1: 3.04 msec per loop
1 loop, best of 1: 3.19 msec per loop
1 loop, best of 1: 3.04 msec per loop

❯ docker run --rm python:3.11 bash -c "for i in {1..15}; do python -m timeit -n 1 -r 1 'import subprocess'; done"
1 loop, best of 1: 31.2 msec per loop
1 loop, best of 1: 3.75 msec per loop
1 loop, best of 1: 4.71 msec per loop
1 loop, best of 1: 3.88 msec per loop
1 loop, best of 1: 4.08 msec per loop
1 loop, best of 1: 4.35 msec per loop
1 loop, best of 1: 3.94 msec per loop
1 loop, best of 1: 4.06 msec per loop
1 loop, best of 1: 3.88 msec per loop
1 loop, best of 1: 3.85 msec per loop
1 loop, best of 1: 3.84 msec per loop
1 loop, best of 1: 4.01 msec per loop
1 loop, best of 1: 4.21 msec per loop
1 loop, best of 1: 4.07 msec per loop
1 loop, best of 1: 4.11 msec per loop

❯ python -m timeit -n 1 -r 1 "from pathlib import Path"
1 loop, best of 1: 5.25 msec per loop

❯ python -m timeit -n 1 -r 1 "import subprocess"
1 loop, best of 1: 7.61 msec per loop

@zanieb
Copy link
Member

zanieb commented Dec 29, 2023

Hey :)

Can you show the performance changes for invoking Ruff itself rather than microbenchmarks of the import? It's hard to tell if these changes are worthwhile in our use-case. I generally prefer the readability of undeferred imports and use of Path when it's not critical.

@ofek
Copy link
Contributor Author

ofek commented Dec 29, 2023

❯ hyperfine -m 10 --warmup 1 "python -m ruff --help"
Benchmark 1: python -m ruff --help
  Time (mean ± σ):      78.3 ms ±   1.8 ms    [User: 0.0 ms, System: 0.0 ms]
  Range (min … max):    75.7 ms …  83.3 ms    34 runs

❯ hyperfine -m 10 --warmup 1 "python -m ruff --help"
Benchmark 1: python -m ruff --help
  Time (mean ± σ):      73.4 ms ±   1.0 ms    [User: 0.0 ms, System: 1.2 ms]
  Range (min … max):    71.7 ms …  75.7 ms    36 runs

I can't easily test right now in Docker, these are on my Windows machine.

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable to me.

@charliermarsh charliermarsh added the cli Related to the command-line interface label Dec 29, 2023
@charliermarsh charliermarsh merged commit 158367b into astral-sh:main Dec 31, 2023
17 checks passed
@ofek ofek deleted the patch-1 branch December 31, 2023 16:36
@zanieb zanieb mentioned this pull request Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command-line interface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants