Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 30, 2025

📄 77% (0.77x) speedup for all_valid in django/forms/formsets.py

⏱️ Runtime : 507 microseconds 286 microseconds (best of 194 runs)

📝 Explanation and details

The optimization removes the unnecessary list comprehension wrapper around the generator expression passed to all().

Key Change:

  • Original: all([formset.is_valid() for formset in formsets]) - creates a full list in memory
  • Optimized: all(formset.is_valid() for formset in formsets) - uses a generator expression

Why it's faster:

  1. Memory efficiency: The generator expression doesn't create an intermediate list, saving memory allocation overhead
  2. Short-circuit evaluation: When all() encounters the first False value, it can immediately return False without evaluating remaining formsets or building the complete list
  3. Reduced object creation: Eliminates the list object creation and the associated Python overhead

Performance characteristics based on tests:

  • Best speedups (100%+ faster): When early False values allow short-circuiting (e.g., test_all_valid_large_one_false_at_start shows 2581% speedup)
  • Moderate gains (50-100% faster): Mixed validity scenarios where short-circuiting happens mid-way through
  • Smaller gains or slight slowdowns (0-20%): Small collections or all-valid scenarios where the full iteration is required anyway

The optimization is particularly effective for large formset collections with early invalid entries, where the generator can terminate early rather than building a complete list of boolean results.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 47 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import pytest
from django.forms.formsets import all_valid


# Helper class to mock a formset object with is_valid() method
class MockFormset:
    def __init__(self, valid):
        self._valid = valid
    def is_valid(self):
        return self._valid

# Basic Test Cases

def test_all_valid_all_true():
    """All formsets are valid: should return True."""
    formsets = [MockFormset(True) for _ in range(5)]
    codeflash_output = all_valid(formsets) # 1.79μs -> 1.68μs (6.75% faster)

def test_all_valid_one_false():
    """One formset is invalid: should return False."""
    formsets = [MockFormset(True) for _ in range(4)] + [MockFormset(False)]
    codeflash_output = all_valid(formsets) # 1.35μs -> 1.65μs (18.1% slower)

def test_all_valid_all_false():
    """All formsets are invalid: should return False."""
    formsets = [MockFormset(False) for _ in range(5)]
    codeflash_output = all_valid(formsets) # 1.24μs -> 1.19μs (3.87% faster)

def test_all_valid_mixed():
    """Mixed valid and invalid formsets: should return False."""
    formsets = [MockFormset(True), MockFormset(False), MockFormset(True)]
    codeflash_output = all_valid(formsets) # 1.11μs -> 1.30μs (14.8% slower)

def test_all_valid_single_true():
    """Single valid formset: should return True."""
    formsets = [MockFormset(True)]
    codeflash_output = all_valid(formsets) # 911ns -> 1.03μs (11.7% slower)

def test_all_valid_single_false():
    """Single invalid formset: should return False."""
    formsets = [MockFormset(False)]
    codeflash_output = all_valid(formsets) # 880ns -> 1.09μs (19.5% slower)

# Edge Test Cases

def test_all_valid_empty_list():
    """Empty formsets list: should return True (vacuous truth)."""
    formsets = []
    codeflash_output = all_valid(formsets) # 687ns -> 710ns (3.24% slower)

def test_all_valid_non_boolean_is_valid():
    """Formset returns non-boolean value from is_valid()."""
    class NonBoolFormset:
        def __init__(self, value):
            self.value = value
        def is_valid(self):
            return self.value
    # Should behave like bool(value)
    codeflash_output = all_valid([NonBoolFormset(1), NonBoolFormset("nonempty")]) # 1.34μs -> 1.45μs (7.54% slower)
    codeflash_output = all_valid([NonBoolFormset(1), NonBoolFormset("")]) # 557ns -> 904ns (38.4% slower)
    codeflash_output = all_valid([NonBoolFormset(0), NonBoolFormset(True)]) # 399ns -> 492ns (18.9% slower)
    codeflash_output = all_valid([NonBoolFormset(None)]) # 386ns -> 458ns (15.7% slower)

def test_all_valid_is_valid_raises():
    """Formset's is_valid() raises an exception: should propagate."""
    class ErrorFormset:
        def is_valid(self):
            raise ValueError("Validation error")
    formsets = [MockFormset(True), ErrorFormset()]
    with pytest.raises(ValueError):
        all_valid(formsets) # 1.81μs -> 2.19μs (17.4% slower)

def test_all_valid_is_valid_not_callable():
    """Formset has no is_valid method: should raise AttributeError."""
    class NoValidFormset:
        pass
    formsets = [MockFormset(True), NoValidFormset()]
    with pytest.raises(AttributeError):
        all_valid(formsets) # 1.91μs -> 2.21μs (13.5% slower)

def test_all_valid_is_valid_not_method():
    """Formset's is_valid is not callable: should raise TypeError."""
    class NotCallableFormset:
        is_valid = True
    formsets = [MockFormset(True), NotCallableFormset()]
    with pytest.raises(TypeError):
        all_valid(formsets) # 2.01μs -> 2.14μs (6.03% slower)

def test_all_valid_iterable_not_list():
    """formsets is an iterable other than list (e.g., tuple, set)."""
    tuple_formsets = (MockFormset(True), MockFormset(True))
    set_formsets = {MockFormset(True), MockFormset(True)}
    # Should work for any iterable
    codeflash_output = all_valid(tuple_formsets) # 1.17μs -> 1.10μs (6.19% faster)
    codeflash_output = all_valid(set_formsets) # 755ns -> 738ns (2.30% faster)



def test_all_valid_large_all_true():
    """Large number of valid formsets: should return True."""
    formsets = [MockFormset(True) for _ in range(1000)]
    codeflash_output = all_valid(formsets) # 30.7μs -> 34.0μs (9.75% slower)

def test_all_valid_large_one_false_beginning():
    """Large number of formsets, first is invalid: should return False."""
    formsets = [MockFormset(False)] + [MockFormset(True) for _ in range(999)]
    codeflash_output = all_valid(formsets) # 27.4μs -> 1.21μs (2163% faster)

def test_all_valid_large_one_false_end():
    """Large number of formsets, last is invalid: should return False."""
    formsets = [MockFormset(True) for _ in range(999)] + [MockFormset(False)]
    codeflash_output = all_valid(formsets) # 29.3μs -> 33.3μs (12.0% slower)

def test_all_valid_large_half_false():
    """Half valid, half invalid formsets: should return False."""
    formsets = [MockFormset(True) for _ in range(500)] + [MockFormset(False) for _ in range(500)]
    codeflash_output = all_valid(formsets) # 29.1μs -> 17.4μs (67.1% faster)

def test_all_valid_large_sparse_false():
    """Large number of formsets, one invalid in the middle: should return False."""
    formsets = [MockFormset(True) for _ in range(499)] + [MockFormset(False)] + [MockFormset(True) for _ in range(500)]
    codeflash_output = all_valid(formsets) # 29.3μs -> 17.3μs (69.0% faster)

def test_all_valid_large_random_validity():
    """Large number of formsets with random validity."""
    import random
    random.seed(42)
    formsets = [MockFormset(random.choice([True, False])) for _ in range(1000)]
    expected = all(fs._valid for fs in formsets)
    codeflash_output = all_valid(formsets) # 27.9μs -> 894ns (3020% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import pytest  # used for our unit tests
from django.forms.formsets import all_valid


# Helper class to simulate a formset object
class DummyFormSet:
    def __init__(self, valid=True, raises=False):
        self.valid = valid
        self.raises = raises

    def is_valid(self):
        if self.raises:
            raise Exception("is_valid raised an exception")
        return self.valid

# ------------------ UNIT TESTS ------------------

# 1. Basic Test Cases

def test_all_valid_all_true():
    """All formsets are valid, should return True."""
    formsets = [DummyFormSet(True), DummyFormSet(True), DummyFormSet(True)]
    codeflash_output = all_valid(formsets) # 1.36μs -> 1.45μs (6.40% slower)

def test_all_valid_one_false():
    """One formset is invalid, should return False."""
    formsets = [DummyFormSet(True), DummyFormSet(False), DummyFormSet(True)]
    codeflash_output = all_valid(formsets) # 1.20μs -> 1.30μs (7.41% slower)

def test_all_valid_all_false():
    """All formsets are invalid, should return False."""
    formsets = [DummyFormSet(False), DummyFormSet(False)]
    codeflash_output = all_valid(formsets) # 1.10μs -> 1.09μs (0.827% faster)

def test_all_valid_single_true():
    """Single valid formset, should return True."""
    formsets = [DummyFormSet(True)]
    codeflash_output = all_valid(formsets) # 919ns -> 1.06μs (13.1% slower)

def test_all_valid_single_false():
    """Single invalid formset, should return False."""
    formsets = [DummyFormSet(False)]
    codeflash_output = all_valid(formsets) # 921ns -> 1.14μs (19.3% slower)

# 2. Edge Test Cases

def test_all_valid_empty_list():
    """Empty list of formsets, should return True (all([]) == True)."""
    formsets = []
    codeflash_output = all_valid(formsets) # 681ns -> 749ns (9.08% slower)

def test_all_valid_non_boolean_return():
    """Formsets return non-boolean values, should be interpreted as truthy/falsy."""
    class WeirdFormSet:
        def __init__(self, value):
            self.value = value
        def is_valid(self):
            return self.value

    # Truthy values
    formsets = [WeirdFormSet(1), WeirdFormSet("non-empty"), WeirdFormSet([1])]
    codeflash_output = all_valid(formsets) # 1.40μs -> 1.53μs (8.41% slower)

    # Falsy values
    formsets = [WeirdFormSet(1), WeirdFormSet(""), WeirdFormSet([1])]
    codeflash_output = all_valid(formsets) # 610ns -> 877ns (30.4% slower)

def test_all_valid_is_valid_raises_exception():
    """A formset's is_valid raises an exception, should propagate the exception."""
    formsets = [DummyFormSet(True), DummyFormSet(raises=True)]
    with pytest.raises(Exception, match="is_valid raised an exception"):
        all_valid(formsets) # 1.64μs -> 2.02μs (19.0% slower)

def test_all_valid_is_valid_not_callable():
    """Formset missing is_valid method, should raise AttributeError."""
    class NoIsValid:
        pass

    formsets = [DummyFormSet(True), NoIsValid()]
    with pytest.raises(AttributeError):
        all_valid(formsets) # 1.96μs -> 2.25μs (12.8% slower)

def test_all_valid_is_valid_is_property():
    """is_valid is a property, not a method, should raise TypeError."""
    class PropertyFormSet:
        @property
        def is_valid(self):
            return True

    formsets = [PropertyFormSet()]
    with pytest.raises(TypeError):
        all_valid(formsets) # 1.85μs -> 2.03μs (9.15% slower)

def test_all_valid_is_valid_returns_none():
    """is_valid returns None, which is falsy, should return False."""
    class NoneFormSet:
        def is_valid(self):
            return None

    formsets = [DummyFormSet(True), NoneFormSet()]
    codeflash_output = all_valid(formsets) # 1.34μs -> 1.59μs (15.4% slower)

def test_all_valid_is_valid_returns_zero():
    """is_valid returns 0, which is falsy, should return False."""
    class ZeroFormSet:
        def is_valid(self):
            return 0

    formsets = [DummyFormSet(True), ZeroFormSet()]
    codeflash_output = all_valid(formsets) # 1.28μs -> 1.48μs (13.8% slower)

def test_all_valid_is_valid_returns_empty_list():
    """is_valid returns empty list, which is falsy, should return False."""
    class EmptyListFormSet:
        def is_valid(self):
            return []

    formsets = [DummyFormSet(True), EmptyListFormSet()]
    codeflash_output = all_valid(formsets) # 1.31μs -> 1.59μs (17.2% slower)

def test_all_valid_is_valid_returns_object():
    """is_valid returns an object, which is truthy, should return True."""
    class ObjectFormSet:
        def is_valid(self):
            return object()

    formsets = [ObjectFormSet(), ObjectFormSet()]
    codeflash_output = all_valid(formsets) # 1.46μs -> 1.48μs (1.42% slower)

def test_all_valid_is_valid_returns_false_string():
    """is_valid returns 'False' string, which is truthy, should return True."""
    class FalseStringFormSet:
        def is_valid(self):
            return "False"

    formsets = [FalseStringFormSet()]
    codeflash_output = all_valid(formsets) # 1.17μs -> 1.32μs (11.3% slower)

# 3. Large Scale Test Cases

def test_all_valid_large_all_true():
    """Large number of valid formsets, should return True."""
    formsets = [DummyFormSet(True) for _ in range(1000)]
    codeflash_output = all_valid(formsets) # 36.2μs -> 39.1μs (7.44% slower)

def test_all_valid_large_one_false_at_start():
    """Large number of formsets, first is invalid, should return False."""
    formsets = [DummyFormSet(False)] + [DummyFormSet(True) for _ in range(999)]
    codeflash_output = all_valid(formsets) # 33.1μs -> 1.23μs (2581% faster)

def test_all_valid_large_one_false_at_end():
    """Large number of formsets, last is invalid, should return False."""
    formsets = [DummyFormSet(True) for _ in range(999)] + [DummyFormSet(False)]
    codeflash_output = all_valid(formsets) # 35.0μs -> 38.2μs (8.32% slower)

def test_all_valid_large_one_false_in_middle():
    """Large number of formsets, one in the middle is invalid, should return False."""
    formsets = [DummyFormSet(True) for _ in range(500)] + [DummyFormSet(False)] + [DummyFormSet(True) for _ in range(499)]
    codeflash_output = all_valid(formsets) # 34.4μs -> 19.8μs (73.9% faster)

def test_all_valid_large_mixed():
    """Large number of formsets, alternating valid/invalid, should return False."""
    formsets = [DummyFormSet(i % 2 == 0) for i in range(1000)]
    codeflash_output = all_valid(formsets) # 33.0μs -> 1.35μs (2349% faster)

def test_all_valid_large_all_false():
    """Large number of invalid formsets, should return False."""
    formsets = [DummyFormSet(False) for _ in range(1000)]
    codeflash_output = all_valid(formsets) # 32.7μs -> 1.14μs (2778% faster)

def test_all_valid_large_non_boolean():
    """Large number of formsets returning various truthy/falsy values."""
    class TruthyFalsyFormSet:
        def __init__(self, val):
            self.val = val
        def is_valid(self):
            return self.val

    # Mix of truthy and falsy
    vals = [True, 1, "yes", [], False, "", 0, None]
    formsets = [TruthyFalsyFormSet(val) for val in vals * 125]  # 1000 elements
    codeflash_output = all_valid(formsets) # 30.4μs -> 1.87μs (1523% faster)

def test_all_valid_large_all_truthy():
    """Large number of formsets all returning truthy non-boolean values."""
    class TruthyFormSet:
        def is_valid(self):
            return "non-empty"

    formsets = [TruthyFormSet() for _ in range(1000)]
    codeflash_output = all_valid(formsets) # 31.2μs -> 32.7μs (4.41% slower)

def test_all_valid_large_all_falsy():
    """Large number of formsets all returning falsy non-boolean values."""
    class FalsyFormSet:
        def is_valid(self):
            return ""

    formsets = [FalsyFormSet() for _ in range(1000)]
    codeflash_output = all_valid(formsets) # 26.7μs -> 1.32μs (1924% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-all_valid-mhcvw8r3 and push.

Codeflash Static Badge

The optimization removes the unnecessary list comprehension wrapper around the generator expression passed to `all()`. 

**Key Change:**
- Original: `all([formset.is_valid() for formset in formsets])` - creates a full list in memory
- Optimized: `all(formset.is_valid() for formset in formsets)` - uses a generator expression

**Why it's faster:**
1. **Memory efficiency**: The generator expression doesn't create an intermediate list, saving memory allocation overhead
2. **Short-circuit evaluation**: When `all()` encounters the first `False` value, it can immediately return `False` without evaluating remaining formsets or building the complete list
3. **Reduced object creation**: Eliminates the list object creation and the associated Python overhead

**Performance characteristics based on tests:**
- **Best speedups** (100%+ faster): When early `False` values allow short-circuiting (e.g., `test_all_valid_large_one_false_at_start` shows 2581% speedup)
- **Moderate gains** (50-100% faster): Mixed validity scenarios where short-circuiting happens mid-way through
- **Smaller gains or slight slowdowns** (0-20%): Small collections or all-valid scenarios where the full iteration is required anyway

The optimization is particularly effective for large formset collections with early invalid entries, where the generator can terminate early rather than building a complete list of boolean results.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 30, 2025 03:48
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant