#35723: formset all_valid use generator comprehension instead of list 
comprehension
-------------------------------------+-------------------------------------
     Reporter:  sahasrara62          |                     Type:
                                     |  Cleanup/optimization
       Status:  new                  |                Component:  Forms
      Version:  dev                  |                 Severity:  Normal
     Keywords:  formsets,            |             Triage Stage:
  validation                         |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 in function `all_valid` in `formsets.py`
 https://github.com/django/django/blob/main/django/forms/formsets.py#L576
 we are using list comprehension to check if each individual formset is
 valid or not and then adding that to list and then computing all. in this
 approach, we have to go through all the formsets even if one of them is
 not valid and then check if all of them are true.

 with this approach, we have to check all formsets first and then check if
 all of list is  valid ie True. An extra computation is happening even
 after if encounter not valid and in memory is also used .

 my purposal is to use generator comprehension here instead of list
 comprehension.
 benefits include, no need to validate all formset if one of formset is
 invalid and less in memory usages.

 so code should look like

 from
 {{{
 def all_valid(formsets):
     """Validate every formset and return True if all are valid."""
     # List comprehension ensures is_valid() is called for all formsets.
     return all([formset.is_valid() for formset in formsets])
 }}}
 to


 {{{
 def all_valid(formsets):
     """Validate every formset and return True if all are valid."""
     return all(formset.is_valid() for formset in formsets)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35723>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070191a6a8e367-ccb0094d-f275-424b-8b81-c76a1ec077a1-000000%40eu-central-1.amazonses.com.

Reply via email to