Skip to content

Enable Ruff PERF #13312

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ select = [
"B", # flake8-bugbear
"FA", # flake8-future-annotations
"I", # isort
"PERF", # Perflint
"PGH", # pygrep-hooks
"RUF", # Ruff-specific and unused-noqa
"UP", # pyupgrade
Expand Down Expand Up @@ -87,11 +88,15 @@ ignore = [
###
# Rules we don't want or don't agree with
###
# Slower and more verbose https://github.com/astral-sh/ruff/issues/7871
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
# Used for direct, non-subclass type comparison, for example: `type(val) is str`
# see https://github.com/astral-sh/ruff/issues/6465
"E721", # Do not compare types, use `isinstance()`
# Can easily produce false-positives. Worth checking but don't block CI.
# Anyway Python 3.11 introduced "zero cost" exception handling for the non-error path.
# Our tests & scripts run on modern Python versions.
"PERF203", # try-except within a loop incurs performance overhead
# Slower and more verbose https://github.com/astral-sh/ruff/issues/7871
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
###
# False-positives, but already checked by type-checkers
###
Expand Down
7 changes: 4 additions & 3 deletions scripts/create_baseline_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ async def get_upstream_repo_url(project: str) -> str | None:

# Order the project URLs so that we put the ones
# that are most likely to point to the source code first
urls_to_check: list[str] = []
url_names_probably_pointing_to_source = ("Source", "Repository", "Homepage")

urls_to_check: list[str] = []
for url_name in url_names_probably_pointing_to_source:
if url := project_urls.get(url_name):
urls_to_check.append(url)
urls_to_check.append(url) # noqa: PERF401 # False-positive with walrus, this code is the fastest form
urls_to_check.extend(
url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source
[url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source]
)

for url in urls_to_check:
Copy link
Collaborator Author

@Avasam Avasam Dec 26, 2024

Choose a reason for hiding this comment

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

Test:

import timeit

setup = """
from itertools import chain

project_urls = {
    "Changelog": "https://code.qt.io/cgit/pyside/pyside-setup.git/tree/doc/changelogs",
    "Documentation": "https://doc.qt.io/qtforpython",
    "Homepage": "https://pyside.org",
    "Repository": "https://code.qt.io/cgit/pyside/pyside-setup.git/",
    "Tracker": "https://bugreports.qt.io/projects/PYSIDE",
}

url_names_probably_pointing_to_source = ("Source", "Repository", "Homepage")
"""

stmt = """
urls_to_check: list[str] = []
for url_name in url_names_probably_pointing_to_source:
    if url := project_urls.get(url_name):
        urls_to_check.append(url)
urls_to_check.extend(
    url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source
)

for url in urls_to_check:
    pass
"""
print(timeit.timeit(stmt=stmt, setup=setup))

stmt = """
urls_to_check: list[str] = []
for url_name in url_names_probably_pointing_to_source:
    if url := project_urls.get(url_name):
        urls_to_check.append(url)
urls_to_check.extend(
    [url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source]
)

for url in urls_to_check:
    pass
"""
print(timeit.timeit(stmt=stmt, setup=setup))

stmt = """
project_urls_probably_pointing_to_source = (
    project_urls.get(url_name) for url_name in url_names_probably_pointing_to_source
)
urls_to_check = chain(
    (url for url in project_urls_probably_pointing_to_source if url),
    (url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source),
)

for url in urls_to_check:
    pass
"""
print(timeit.timeit(stmt=stmt, setup=setup))

stmt = """
project_urls_probably_pointing_to_source = [project_urls.get(url_name) for url_name in url_names_probably_pointing_to_source]
urls_to_check = (
    [url for url in project_urls_probably_pointing_to_source if url]
    + [url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source]
)

for url in urls_to_check:
    pass
"""
print(timeit.timeit(stmt=stmt, setup=setup))

Results (Python 3.10):

0.6867717999994056
0.610139699998399
0.9288775999993959
0.7600129999991623

Results (Python 3.13):

0.7435727000010957
0.5391235000024608
0.9949863000001642
0.5781141000006755

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nevermind, I was just missing parenthesis: astral-sh/ruff#15047 (comment)
(new run time on python 3.13 is 0.5153441999973438)

Expand Down
Loading