Skip to content

Stream deque inputs and release evicted items outside locks - #8860

Merged
youknowone merged 1 commit into
RustPython:mainfrom
1ndahous3:deque_streaming
Sep 27, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
1ndahous3:deque_streaming

Conversation

@1ndahous3

@1ndahous3 1ndahous3 commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Consume inputs incrementally in deque construction, extend, extendleft, and +=. Bounded deques no longer materialize generated inputs, and items already consumed remain in the deque when iteration raises.

Release evicted items outside the deque lock so finalizers can reenter safely. Empty or immediately failing extend/extendleft calls preserve existing iterators. Prepending no longer rebuilds the entire deque for each batch.

Follow-up to #8567 and #8847.

Performance

Consuming 10,000 generated objects reduces peak live objects from 10,000 to 9 with maxlen=8, and from 10,000 to 1 with maxlen=0.

Release build, medians of three runs:

Operation Before After Speedup
New deque(maxlen=64), then extend(range(4096)) 161.58 µs 105.77 µs 1.53×
Prepend 8 items to a 10,000-item deque, then pop 8 2.55 µs 1.12 µs 2.28×

AI assistance

Written with Codex (GPT-6), reviewed by a human before submission.

Summary by CodeRabbit

  • Bug Fixes
    • Improved deque extension behavior when iterables raise errors, including preserving items added before the error.
    • Improved handling of bounded deques, self-extension, and iterables that change during eviction.
    • Preserved iterator behavior when extensions make no changes.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: RustPython/RustPython/.coderabbit.yml

Review profile: CHILL

Plan: Advanced

Run ID: b791994c-b60a-4f1a-94a2-0c433bfd5bf7

📥 Commits

Reviewing files that changed from the base of the PR and between f871707 and 91c14d1.

📒 Files selected for processing (2)
  • crates/vm/src/stdlib/_collections.rs
  • extra_tests/snippets/stdlib_collections_deque.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Deque append and extension operations now share helpers for insertion, mutation tracking, and maxlen trimming. Extension handles self-extension, bounded containers, and other iterables through distinct paths. New tests cover partial updates on errors, iterator behavior, and object lifetime.

Changes

Deque mutation and extension

Layer / File(s) Summary
Shared append and trimming helpers
crates/vm/src/stdlib/_collections.rs
Shared helpers handle insertion, mutation tracking, maxlen trimming, and releasing evicted items after unlocking. append and appendleft now use the append helper.
Extension paths and behavior tests
crates/vm/src/stdlib/_collections.rs, extra_tests/snippets/stdlib_collections_deque.py
extend, extendleft, initialization, and in-place concatenation use the shared extension path. Tests cover partial updates after errors, bounded-memory behavior, subclass iterators, no-op extensions, and object finalizers.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to 91c14

No confirmed issue prevents merging after normal checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 91c14

Streaming inputs and releasing evicted objects outside the lock improve bounded-memory behavior and reentrancy. A concurrent iterator may now miss a mutation in an additional scheduling window. No privilege expansion or sensitive-data path was established.

Retained concerns

  • Low · reliability · inferred: Moving mutation-generation updates inside the deque write lock creates an additional schedule in which a concurrent iterator can return post-mutation data without its expected mutation error. This weakens iterator failure containment where genuinely concurrent access occurs; no security-sensitive consumer was identified.
Security review details

Security Blast Radius

  • inferred — The directly established exposure is callers of the VM deque API and objects they supply. The available evidence does not establish tenant, service, data-store, or deployment-level exposure.

Trust Boundaries and Controls

  • inferred — Moving object release outside the lock changes when existing caller-controlled finalizers can reenter the deque; it does not, on the inspected path, grant those objects a new privilege.

Resilience and Maintainability Implications

  • inferred — The concurrent generation-check window can weaken fail-fast iteration, but its use in an authorization or other security-sensitive decision was not established.

Hardening Proposals

  • proposed — If concurrent deque access is supported, validate mutation generation together with the locked deque read and exercise the writer-held-lock interleaving in a concurrency test.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 24 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies both primary changes: streaming deque inputs and releasing evicted items outside locks.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codspeed

codspeed Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing 1ndahous3:deque_streaming (91c14d1) with main (f871707)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@youknowone
youknowone merged commit f24b0d8 into RustPython:main Sep 27, 2026
22 checks passed
@1ndahous3
1ndahous3 deleted the deque_streaming branch September 27, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants