Skip to content

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Sep 10, 2025

Remove the function call overhead of repeatedly calling list.append().

% ruff check --select=PERF --output-format=concise

mavsdk/camera.py:2188:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/camera.py:2453:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/camera.py:2513:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/component_information.py:363:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/log_files.py:308:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/mission_raw.py:788:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/mission_raw.py:817:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/mission_raw.py:846:13: PERF401 Use a list comprehension to create a transformed list
mavsdk/tune.py:253:13: PERF401 Use a list comprehension to create a transformed list
Found 9 errors.

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

Copy link
Collaborator

@JonasVautherin JonasVautherin left a comment

Choose a reason for hiding this comment

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

Again those are generated files, see e.g. here.

# -*- coding: utf-8 -*-
# DO NOT EDIT! This file is auto-generated from
# https://github.com/mavlink/MAVSDK-Python/tree/main/other/templates/py

@cclauss
Copy link
Contributor Author

cclauss commented Sep 10, 2025

We can postprocess generation with ruff check like:

Or we could have done that rewriting in:

@JonasVautherin
Copy link
Collaborator

We can postprocess generation with ruff check

Sure, that's not the problem. I'm merely noting that if you want to contribute, you need to contribute to the templates, not to the generated code that says DO NOT EDIT! This file is auto-generated.

Respectfully, I can also run a linter. What takes time is to actually contribute the change and then test it. I would be happy to review changes to the templates, given that they are tested. If I need to do most of the work (changing the code and/or testing it), then I'm less enthusiastic 😇.

@cclauss
Copy link
Contributor Author

cclauss commented Sep 11, 2025

Understood. Closing.

@cclauss cclauss closed this Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants