Start list and tuple index searches at the requested offset
list.index(value, start, end) and tuple.index(...) currently advance their lending iterators from zero, cloning and dropping excluded prefix values before searching the requested window. Initialize the existing iterator position from the normalized start instead.
Keep the existing bounds-coercion order, mutation-safe list reads, comparison path, recursion guard and native timeout polling. Comments explain why excluded entries need no equality calls or reference-count traffic. Tests cover huge/negative bounds, bound coercions that mutate the list, equality callbacks that clear it, empty windows, shared children and callback-driven timeouts.
Microbenchmarks were measured on macOS 27 arm64 with Rust 1.99.0 release builds (fat LTO, one codegen unit), clean main 5bcc54cd versus 2d8790f4. Both binaries were built in the same directory and frozen before timing. Flying Circus ran 40 randomized samples per runtime/mode, serially, with 10 repeated-mode warmups and no builds or tests running alongside measurements. These repeated-worker medians include compilation, setup and execution of the full script, excluding process startup. Scripts were validated with CPython 3.14.2; every measured request checked its assertions and expected output. Benchmark scripts are kept local and are not part of this PR; input sizes and operation counts are listed below.
| Microbenchmark | Main | Candidate | Time change |
|---|---|---|---|
| 1,000 tail-only searches of a 4,096-item list | 21.4779 ms | 0.2497 ms | -98.84% (86.0x faster) |
| 1,000 tail-only searches of a 4,096-item tuple | 22.5905 ms | 0.2272 ms | -98.99% (99.4x faster) |
| 100 full-range list searches, integer items | 8.5458 ms | 8.4614 ms | -0.99% |
| 200 full-range list searches, heap-string items | 5.5771 ms | 5.6073 ms | +0.54% |
The full-range controls fall below the configured change threshold in repeated mode. The large gains come from eliminating the excluded prefix; they describe these offset-heavy workloads.
The harness used protocol 7 transport compatibility. Timing flags were --diff --samples 40 --warmups 10 --startup-samples 3 --memory-samples 0 --threshold 1 --absolute-threshold-ms 0.01 -x 1, with each script passed using --benchmark and baseline/candidate passed using -r.
Local validation passed: all 1,492 Monty/CPython fixture checks, all 142 reference-count fixture checks, 95 Rust tests across collections, container mutation, dump compatibility, resource limits and the new callback-timeout test, Clippy with warnings denied, Ruff, formatting and import checks. Rust tests and fixtures used memory-model-checks; the reference-count pass also used ref-count-return.
Summary by cubic
Starts list.index(value, start, end) and tuple.index(...) searches at the normalized start offset instead of cloning and dropping the excluded prefix entries from zero, which dominated the cost when the needle sat near the end. Also checks the native timeout budget before the search so nested short offset searches can't outrun an expired limit.
- Excluded prefix entries no longer incur equality calls or reference-count traffic; bounds coercion order, mutation-safe reads, equality callbacks, and the recursion guard behave as before.
- Adds fixtures and tests covering bounds that mutate the list, equality callbacks that clear it, empty windows, shared children, and callback-driven and nested-search timeouts.
- Benchmarks: 1,000 tail-only searches of a 4,096-item list run ~86x faster (21.48 ms → 0.25 ms), while full-range searches change by about 1%.