[PR #1392] [MERGED] Add ruff rule PERF for performance #2001

Open
opened 2026-01-29 20:56:51 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/google/brotli/pull/1392
Author: @cclauss
Created: 11/20/2025
Status: Merged
Merged: 11/24/2025
Merged by: @copybara-service[bot]

Base: masterHead: comprehensions


📝 Commits (3)

  • 7ff6d1d Add ruff rule PERF for performance
  • 688d661 Merge branch 'master' into comprehensions
  • 5db7aca Merge branch 'master' into comprehensions

📊 Changes

4 files changed (+18 additions, -26 deletions)

View changed files

📝 .github/workflows/lint.yml (+1 -1)
📝 scripts/dictionary/step-02-rfc-to-bin.py (+3 -5)
📝 scripts/dictionary/step-04-generate-java-literals.py (+10 -11)
📝 setup.py (+4 -9)

📄 Description

% ruff check --extend-select=C4,C90,PERF,RET,SIM,W

% 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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/google/brotli/pull/1392 **Author:** [@cclauss](https://github.com/cclauss) **Created:** 11/20/2025 **Status:** ✅ Merged **Merged:** 11/24/2025 **Merged by:** [@copybara-service[bot]](https://github.com/apps/copybara-service) **Base:** `master` ← **Head:** `comprehensions` --- ### 📝 Commits (3) - [`7ff6d1d`](https://github.com/google/brotli/commit/7ff6d1d286e54eede7546ca89d0a4bedc17516f6) Add ruff rule PERF for performance - [`688d661`](https://github.com/google/brotli/commit/688d661f40d2408156bf4f4798b2141402bca254) Merge branch 'master' into comprehensions - [`5db7aca`](https://github.com/google/brotli/commit/5db7aca5719563535841dc339cb66943f0af0afc) Merge branch 'master' into comprehensions ### 📊 Changes **4 files changed** (+18 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `.github/workflows/lint.yml` (+1 -1) 📝 `scripts/dictionary/step-02-rfc-to-bin.py` (+3 -5) 📝 `scripts/dictionary/step-04-generate-java-literals.py` (+10 -11) 📝 `setup.py` (+4 -9) </details> ### 📄 Description % `ruff check --extend-select=C4,C90,PERF,RET,SIM,W` % `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 ```python original = list(range(10000)) filtered = [] for i in original: if i % 2: filtered.append(i) ``` Use instead: ```python 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: ```python original = list(range(10000)) filtered.extend(x for x in original if x % 2) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 20:56:51 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#2001