Skip to content

Conversation

@cclauss
Copy link
Contributor

@cclauss cclauss commented Dec 1, 2025

Overview: What does this pull request change?

Add ruff rules PERF for performance

Motivation and Explanation: Why and how do your changes improve the library?

Users will be happier if our code runs faster.

Links to added or changed documentation pages

https://docs.astral.sh/ruff/rules/#perflint-perf

Further Information and Comments

% ruff check --statistics

6	PERF401	manual-list-comprehension
3	PERF102	incorrect-dict-iterator
Found 9 errors.

Also upgrade pre-commit hooks and move mypy to be the last hook because it is so damn slow.

% ruff rule PERF401

manual-list-comprehension (PERF401)

Derived from the Perflint linter.

Fix is sometimes available.

What it does

Checks for for loops that can be replaced by a list comprehension.

Why is this bad?

When creating a transformed list from an existing list using a for-loop,
prefer a list comprehension. List comprehensions are more readable and
more performant.

Using the below as an example, the list comprehension is ~10% faster on
Python 3.11, and ~25% faster on Python 3.10.

Note that, as with all perflint rules, this is only intended as a
micro-optimization, and will have a negligible impact on performance in
most cases.

Example

original = list(range(10000))
filtered = []
for i in original:
    if i % 2:
        filtered.append(i)

Use instead:

original = list(range(10000))
filtered = [x for x in original if x % 2]

If you're appending to an existing list, use the extend method instead:

original = list(range(10000))
filtered.extend(x for x in original if x % 2)

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@cclauss cclauss force-pushed the ruff-rules-for-performance branch from 5773928 to 3258cda Compare December 1, 2025 10:27
@behackl behackl added the maintenance refactoring, typos, removing clutter/dead code, and other code quality improvements label Dec 1, 2025
Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

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

Fancy stuff. Thanks, LGTM -- will merge if pipeline passes.

@github-project-automation github-project-automation bot moved this from 🆕 New to 👍 To be merged in Dev Board Dec 1, 2025
@behackl behackl enabled auto-merge (squash) December 1, 2025 10:30
@behackl behackl merged commit de090c1 into ManimCommunity:main Dec 1, 2025
17 of 18 checks passed
@github-project-automation github-project-automation bot moved this from 👍 To be merged to ✅ Done in Dev Board Dec 1, 2025
@cclauss cclauss deleted the ruff-rules-for-performance branch December 1, 2025 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance refactoring, typos, removing clutter/dead code, and other code quality improvements

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants