Share element access for lists and tuples

Merged
JA
12d ago
+321-364

Summary

List and tuple fast paths now read elements through SequenceItems. The view exposes length, per-item access, and
owned copies so callers can work with compact buffers that construct a Value on demand.

Move bulk insertion, swaps, and GC child traversal into List methods. Host export still copies the list's elements
before recursing because a nested __repr__ can mutate the source list.

This refactor prepares for the storage strategies explored in #1025.

Tests

  • 1,178 Rust tests with memory-model-checks and test-hooks

  • 24 heap-reader compile-fail tests

  • 1,574 Monty and CPython fixture checks with CPython 3.14.10

  • Clippy, formatting, and import checks

  • Five local release benchmarks showed no statistically significant regression against 32bbc255

  • I have read the contribution guide.

  • I understand and can explain the proposed changes, including any generated with AI assistance.


Summary by cubic

Encapsulates list, tuple, and namedtuple element access behind a SequenceItems read-only view, moving bulk insertion, swaps, list concatenation, and GC child traversal into List methods in preparation for the storage strategies in #1025.

  • Adds SequenceItems with indexed (map_item, clone_item) and iterator access producing owned Values on demand; callers restrict which heap types the view accepts, with tuple-only lookups (from_tuple) for dict item membership and named-tuple concatenation.
  • Migrates callers from direct slice access and vector mutation to SequenceItems and List methods (extend_owned, swap, for_each_child_id); list.extend, in-place concatenation, starred unpacking, Callable, Decimal, dict item membership, sorted, JSON separators, and package paths reuse owned reads.
  • sorted keeps its periodic deadline check while copying; the CSV writer now copies directive fields through the same guarded loop so a timeout releases their heap references.
  • DictView accepts namedtuples of length two as candidate key/value pairs.
  • Unpacking a named tuple into too few targets now reports the total length, e.g. ValueError: too many values to unpack (expected 1, got 2); CPython omits got 2 for named tuples, so the divergence is documented.
  • Host export still copies list elements before recursing because a nested __repr__ can mutate the source list.
  • Covered by existing Rust, heap-reader, Monty, and CPython fixture tests, plus new tests for list/tuple operands in forward references, namedtuple unpacking and dict-item membership, Decimal with DecimalTuple, csv.writer rows, and a non-sequence import path.