[Django] #35893: Using list comprehension (instead of a generator) to any/all where the items are side-effect free is a performance error

5 views
Skip to first unread message

Django

unread,
Nov 6, 2024, 1:13:56 PM11/6/24
to django-...@googlegroups.com
#35893: Using list comprehension (instead of a generator) to any/all where the
items are side-effect free is a performance error
-------------------------------------+-------------------------------------
Reporter: john- | Owner: john-parton
parton |
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: dev
contrib.admin |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
When passing a generator to `any()` or `all()`, Python can perform short-
circuit evaluation to avoid needless computation.

Directly passing a list using list-comprehension is generally not
recommended, unless the list-comprehension has a side-effect (for example
calling a method that sets attributes on an object)

There's at least one place in the code-base where it's safe to replace
list comprehension with a generator and get the short-circuit optimization
for "free."
--
Ticket URL: <https://code.djangoproject.com/ticket/35893>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 6, 2024, 1:14:41 PM11/6/24
to django-...@googlegroups.com
#35893: Using list comprehension (instead of a generator) to any/all where the
items are side-effect free is a performance error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: john-
Type: | parton
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by john-parton):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35893#comment:1>

Django

unread,
Nov 6, 2024, 10:08:01 PM11/6/24
to django-...@googlegroups.com
#35893: Using list comprehension (instead of a generator) to any/all where the
items are side-effect free is a performance error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: john-
Type: | parton
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by john-parton):

Patch here https://github.com/django/django/pull/18770
--
Ticket URL: <https://code.djangoproject.com/ticket/35893#comment:2>

Django

unread,
Nov 7, 2024, 6:30:47 AM11/7/24
to django-...@googlegroups.com
#35893: Using list comprehension (instead of a generator) to any/all where the
items are side-effect free is a performance error
-------------------------------------+-------------------------------------
Reporter: john-parton | Owner: john-
Type: | parton
Cleanup/optimization | Status: closed
Component: contrib.admin | Version: dev
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => duplicate
* status: assigned => closed

Comment:

I will track this with #23968 and update the commit to Refs
--
Ticket URL: <https://code.djangoproject.com/ticket/35893#comment:3>
Reply all
Reply to author
Forward
0 new messages