-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Minor optimizations in Python code #13478
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the commit message could probably be a bit better (nothing huge, but I'd at least include in the extended description the command you used to reproduce the changes).
The other big thing to me is my comment below about the second commit.
mesonbuild/compilers/cuda.py
Outdated
@@ -233,7 +235,7 @@ def _shield_nvcc_list_arg(cls, arg: str, listmode: bool = True) -> str: | |||
# There are single quotes. Double-quote them, and single-quote the | |||
# strings between them. | |||
l = [cls._shield_nvcc_list_arg(s) for s in arg.split(SQ)] | |||
l = sum([[s, DQSQ] for s in l][:-1], []) # Interleave l with DQSQs | |||
l = functools.reduce(operator.iadd, [[s, DQSQ] for s in l][:-1], []) # Interleave l with DQSQs | |||
return ''.join(l) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole logic feels kinda hairy honestly. I wonder if there's a better way to do it that doesn't need additional imports...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hairy indeed. The commit message lists the three linear solutions and why ruff defaults to this one. Given its hairiness, let's revert this change and deal with it in a separate PR.
test cases/common/271 env in generator.process/generate_main.py
Outdated
Show resolved
Hide resolved
5d6ddb6
to
aa6e68c
Compare
aa6e68c
to
6a2efc6
Compare
039d1ec
to
d754adf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, please no "Apply suggestions from code review" commits or other forms of fixup for a previous commit in the same PR.
6a600f1
to
3f998e4
Compare
3f998e4
to
6709c18
Compare
Can someone please stop and rerun the runaway GitHub Action? Five hours is a bit too long! |
Any particular reason for this? Just curious... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes here look good to me. Once Eli is happy with everything I'm fine to merge this.
I reverted |
They fail in git master. Those jobs are only run on a weekly schedule or whenever the job definition itself changes -- so avoiding to change the job definition, doesn't make any difference to the test other than preventing its status from showing up. It is perfectly fine to include that change... |
6709c18
to
26eab9a
Compare
%
ruff check --select=C4,PERF,PIE810 --statistics | grep "\[\*\]"
Edit as requested below:
%
ruff check --select=C4,PERF,PIE810 --fix --unsafe-fixes
%
ruff rule RUF017
quadratic-list-summation (RUF017)
Derived from the Ruff-specific rules linter.
Fix is always available.
What it does
Checks for the use of
sum()
to flatten lists of lists, which hasquadratic complexity.
Why is this bad?
The use of
sum()
to flatten lists of lists is quadratic in the number oflists, as
sum()
creates a new list for each element in the summation.Instead, consider using another method of flattening lists to avoid
quadratic complexity. The following methods are all linear in the number of
lists:
functools.reduce(operator.iadd, lists, [])
list(itertools.chain.from_iterable(lists))
[item for sublist in lists for item in sublist]
When fixing relevant violations, Ruff defaults to the
functools.reduce
form, which outperforms the other methods in microbenchmarks.
Example
Use instead:
References