Skip to content

VecIntoIter.next() is remove(0) per element — consuming iteration over a Vec is O(n²) #938

Closed

Description

@ehartford

Found in the container survey prompted by #936.

lib/std/collections.w ≈ line 397:

impl[T] Iter[T] for VecIntoIter[T]:
    // remove(0) is D27's proven element transfer; the memmove-per-next
    // cost stands until a front-cursor variant earns its unsafe.
    mut fn next() -> Option[T]:
        if self.vec.len() == 0: return None
        Some(self.vec.remove(0))

Every next() memmoves the remaining elements, so for x in vec.into_iter() (D33's consuming surface) is O(n²). The comment knows; this issue makes it a tracked item rather than a note.

Fix

A front cursor: VecIntoIter { vec, idx } where next() moves element idx out (a proven element transfer via the existing D27 take/slot machinery or a raw move that blanks the slot) and the iterator's drop releases the un-yielded tail (D33: "the iterator owns the tail"). The drop audit must cover: fully consumed, partially consumed then dropped, zero-iteration.

There are 11 remove(0) sites across lib/std and src; the other notable one is BTreeMap.insert (#937's O(n²) rebuild).

Activity

  1. ehartford commented on Sep 2, 2026

    @ehartford
    ContributorAuthor

    Ruling (2026-09-01): the engine comes from the migrated corpora (c-algorithms / TommyDS / STC, migrated whole; M*LIB's B+ tree surgically) with a With facade choosing the winner — see docs/stdlib_sourcing_plan.md and the note on #940. This issue stays open as the facade deliverable.

  2. ehartford commented on Oct 22, 2026

    @ehartford
    ContributorAuthor

    Fixed on branch stc-facades (commit 0e9b82e): ListIntoIter.next() moves the head out through its own address and advances the buffer's start (D115, with_vec_take_front_es), O(1) per element. Evidence: the vec-into-iter row of test/complexity/stdlib.w is a PASS (it carried this issue as an expected failure); behav_list_sort and the existing consuming-iteration fixtures pass.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions