Skip to content

del list[index] never reallocates the array #158592

Description

@x42005e1f

Bug report

This works as expected:

>>> import sys
>>> seq = list(range(1000))
>>> sys.getsizeof(seq)
8056
>>> for _ in range(1000):
...     seq.pop()
>>> sys.getsizeof(seq)
56

But this does not:

>>> import sys
>>> seq = list(range(1000))
>>> sys.getsizeof(seq)
8056
>>> for _ in range(1000):
...     del seq[-1]
>>> sys.getsizeof(seq)
8056

Perhaps a regression from #115605 (note that the list_ass_slice() call has been replaced with a separate implementation).

CPython versions tested on:

3.13, 3.14, 3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Oct 2, 2026
  2. picnixz commented on Oct 2, 2026

    @picnixz
    Member

    This is not necessarily a bug though? sure it can consume a bit more memory but AFAIK, this was never guaranteed in the first place.

  3. added
    pendingThe issue will be closed if no feedback is provided
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Oct 2, 2026
  4. x42005e1f commented on Oct 2, 2026

    @x42005e1f
    ContributorAuthor

    Why not, though? The PR made lists thread-safe; it was not intended to change the behavior of deletion by index (there is also no mention of this in the changelog). All other ways of modifying a list reallocate the array. Prior to 3.13, it was also always successfully reallocated. How else can this inconsistency be described if not as a bug? And why should the inability to free memory by deleting by index be expected behavior?

    Another example:

    >>> import sys
    >>> seq = list(range(1000))
    >>> sys.getsizeof(seq)
    8056
    >>> for _ in range(1000):
    ...     del seq[-1:]  # by slice
    >>> sys.getsizeof(seq)
    56
  5. picnixz commented on Oct 2, 2026

    @picnixz
    Member

    Inconsistencies are usually not bugs as long as the actual behavior is unchanged (excluding implementation details and performance regressions that sometimes are not even fixed because deemed too fragile in general) but if other methods do reallocate the array, we can fix it on 3.14+. 3.13 is now security-only unfortunately. But if this warrants a too large refactor, we may likely keep it in 3.15+ or 3.16.

    Are you interested in making a PR so that we can evaluate the change and blast radius?

  6. picnixz commented on Oct 2, 2026

    @picnixz
    Member

    And why should the inability to free memory by deleting by index be expected behavior?

    This is an implementation detail. One could delete and reassign just after so sometimes freeing and reallocating memory may not be the best in this case (I am talking in general terms not for this case specifically).

    Implementation details are never guaranteed unless documented and can change without notice (but we try not to and I do not know why this specific issue appeared in the first place: was it an omission in the original PR?)

  7. added
    type-bugAn unexpected behavior, bug, or error
    performancePerformance or resource usage
    type-refactorCode refactoring (with no changes in behavior)
    3.16new features, bugs and security fixes
    and removed
    pendingThe issue will be closed if no feedback is provided
    on Oct 2, 2026
  8. x42005e1f commented on Oct 2, 2026

    @x42005e1f
    ContributorAuthor

    Are you interested in making a PR so that we can evaluate the change and blast radius?

    If required, then yes. But I will need some time to prepare (by reading the Developer's Guide) so I can understand what is expected of me.

  9. picnixz commented on Oct 2, 2026

    @picnixz
    Member

    What we expect from you (rough list):

    • common sense (most important thing)
    • human interaction (please do not use an LLM to reply (you do not now, but just so that you know)). You can use one for translation. In particular, be able to explain all changes by yourself (we have too many AI slops, it's burning our review time so we usually focus on high quality PRs that do not need us to fight with an agent with a human as a proxy). You do not seem to be using LLMs and I am grateful for that.
    • regression tests (if applicable)
    • do not change unrelated code
    • follow PEP-7 but use common sense: make minimal changes
    • a NEWS entry as it may be a visible change (though not really important) (use python's blurb or the blurb app).
    • never force push once a review has been given on your PR (2nd most important thing)
    • avoid the "Update branch" button, even if it is tempting. Only hit it when there is a real CI change that you need to exercise.

    OOC I would be interested in a benchmark where we del an item and reinsert at the same place or append a new item. For benchmarks use pyperf and a PGO+LTO build.

    In general: follow code style of the file or apply PEP-7 for C code and PEP-8 for Python code. Avoid magical C tricks that may not be supported everywhere (C11 only).

    Those are usually the rules I give to new contributors.

  10. added 8 commits that reference this issue on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15bugs and security fixes3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-bugAn unexpected behavior, bug, or errortype-refactorCode refactoring (with no changes in behavior)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions