Skip to content

Share ctypes backing-owner snapshots between aggregate copies - #9392

Merged
youknowone merged 3 commits into
RustPython:mainfrom
1ndahous3:ctypes_shared_owners
Oct 27, 2026
Merged

youknowone merged 3 commits into
RustPython:mainfrom
1ndahous3:ctypes_shared_owners

Conversation

@1ndahous3

@1ndahous3 1ndahous3 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Addresses the quadratic private ownership metadata identified while reviewing #9251.

Summary

  • Share a flat snapshot of hidden backing owners while the source owner set is unchanged. Destination slots retain group IDs instead of copying every owner ID.
  • Keep owner counts and snapshot invalidation local to distinct groups, preserving alias lifetimes and releasing replaced values after publication.
  • Track internal snapshot tuples without running collection while their ownership state is being published. Expose their ownership edges to cyclic GC.

Performance

Repeated aggregate copies with an unchanged owner set:

Platform Elements Before After
Windows 11 x64 1,024 11.50 ms 0.99 ms
Debian 12 amd64 4,096 257.37 ms 5.52 ms

Peak process RSS in the Debian scenario: 236.2 MiB → 70.9 MiB.

AI assistance

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

Summary by CodeRabbit

  • Bug Fixes
    • Improved reference handling for ctypes objects when retained references are replaced or shared, helping ensure related objects remain available while needed.
    • Improved memory-management behavior during object creation and reference updates. Some new objects can now be tracked without triggering collection immediately; subsequent tracked allocations continue to check collection thresholds as usual. These changes help reduce unintended collection during allocation and improve the reliability of object lifetimes.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-26T17:39:32.933102Z f4df593 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 332101a and f4df593.

Files selected for processing: 2
  • crates/vm/src/gc_state.rs
  • crates/vm/src/object/core.rs

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

Walkthrough

Object tracking can now suppress collection during allocation. ctypes hidden references use owner groups and cached tuple snapshots for groups with multiple owners.

Changes: ctypes hidden-reference retention

Layer / File(s) Summary
Optional collection during allocation
crates/vm/src/gc_state.rs, crates/vm/src/object/core.rs
track_new_object_impl and a shared PyRef allocation path accept a collection flag. new_ref enables collection; new_ref_without_collection disables it on supported host builds.
Owner groups and snapshots
crates/vm/src/stdlib/_ctypes/base.rs
PrivateRefs tracks owner groups and reference counts, traverses and clears retained references, and caches a tuple snapshot for multiple owners until the owner set changes.
CData hidden-reference integration
crates/vm/src/stdlib/_ctypes/base.rs
CDataValue and retention helpers use optional HiddenRefs groups in conversions, assignments, and keep operations.

Priority: Normal

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

Change: Refactor

Possibly related PRs

Suggested reviewers: youknowone

Merge Risk: Minimal · up to f4df5

No actionable merge-blocking defect is established by the reviewed changes.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 21.05% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed 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 describes the main change: sharing ctypes backing-owner snapshots across aggregate copies.
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.
Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@1ndahous3
1ndahous3 force-pushed the ctypes_shared_owners branch from b23664e to 332101a Compare October 26, 2026 12:45

/// Allocate and track internal storage without running collection during
/// publication. The next ordinary tracked allocation still checks thresholds.
pub(crate) fn new_ref_without_collection(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the naming is confusing. new_ref_without_collection is default false call of new_ref_with_collection. those names are looking symmetric.

Comment thread crates/vm/src/gc_state.rs Outdated
/// # Safety
/// obj must be a valid pointer to an untracked PyObject with freshly zeroed `gc_bits`.
#[inline]
pub(crate) unsafe fn track_new_object_with_collection(obj: NonNull<PyObject>, collect: bool) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's give a good name here. I will think a bit more.

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@youknowone
youknowone merged commit 415e472 into RustPython:main Oct 27, 2026
28 checks passed
@1ndahous3
1ndahous3 deleted the ctypes_shared_owners branch October 28, 2026 07:34
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