8000 Implement copyslot by youknowone · Pull Request #6505 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@youknowone
Copy link
Member
@youknowone youknowone commented Dec 25, 2025
Benchmark OLD NEW Speedup
fannkuch 1.56s 1.50s +4%
nbody 0.15s 0.14s +7%
pystone 0.88s 0.85s +3%

Summary by CodeRabbit

  • Refactor

    • Optimized method slot resolution by replacing dynamic inheritance chain traversals with direct class slot access across core protocol systems.
    • Restructured sequence and mapping protocol methods from static method references to dynamic slot-based access.
    • Streamlined internal protocol method dispatch mechanisms.
  • Bug Fixes

    • Enforced stricter type validation for __str__ and __bool__ special methods to prevent returning incorrect types.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor
coderabbitai bot commented Dec 25, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This PR refactors protocol access patterns across the VM, replacing MRO-based slot lookups with direct class slot access. It introduces new unchecked accessors for sequences and mappings, restructures PyNumber/PySequence/PyMapping with slot-based designs, removes cached protocol methods from heap types, and adds slot inheritance helpers to simplify type initialization.

Changes

Cohort / File(s) Summary
Core Protocol Restructuring
crates/vm/src/protocol/number.rs, crates/vm/src/protocol/sequence.rs, crates/vm/src/protocol/mapping.rs
Introduced PySequenceSlots and PyMappingSlots structures for atomic slot storage. Renamed to_number() to number() and restructured PyNumber from tuple-newtype to named-field struct. Updated PySequence and PyMapping to use slot-based dispatch instead of static method references. Added sequence_unchecked(), try_sequence(), mapping_unchecked(), and try_mapping() accessors on PyObject.
Type System and Slot Management
crates/vm/src/types/slot.rs, crates/vm/src/builtins/type.rs, crates/vm/src/class.rs
Changed as_sequence and as_mapping fields in PyTypeSlots from PointerSlot to PySequenceSlots/PyMappingSlots. Removed sequence_methods and mapping_methods fields from HeapTypeExt. Removed mro_find_map method and added slot inheritance helpers (inherit_slots, inherit_readonly_slots, inherit_number_slots, etc.). Updated type initialization to inherit slots from base types. Added wrappers for __str__ and __bool__ enforcement.
Object Protocol Implementation
crates/vm/src/protocol/object.rs, crates/vm/src/object/core.rs
Replaced MRO-based slot lookups with direct obj.class().slots.X.load() access patterns across attribute access, item operations, comparisons, repr, hash, and length operations. Updated descriptor lookups to use direct slot access instead of MRO traversal.
VM Operations and Execution
crates/vm/src/vm/vm_ops.rs, crates/vm/src/vm/method.rs, crates/vm/src/vm/vm_object.rs, crates/vm/src/frame.rs
Replaced MRO-based lookups for number operation slots, descriptor getters, and attribute access with direct slot retrieval. Updated arithmetic and comparison operator paths to use direct slot access. Simplified method resolution in ExecutingFrame.
Protocol Implementations
crates/vm/src/protocol/callable.rs, crates/vm/src/protocol/iter.rs, crates/vm/src/protocol/buffer.rs
Changed call and iterator slot lookups from MRO-based search to direct class slot access. Updated PyCallable::new, PyIter::check, and buffer protocol to use obj.class().slots.X.load() directly.
Function Protocol
crates/vm/src/function/protocol.rs
Removed methods field from ArgMapping struct. Changed constructor from with_methods(obj, methods) to new(obj). Updated mapping() method to use self.obj.mapping_unchecked(). Simplified TryFromObject implementation for ArgMapping by removing methods validation step.
Builtin Types
crates/vm/src/builtins/bool.rs, crates/vm/src/builtins/classmethod.rs, crates/vm/src/builtins/dict.rs, crates/vm/src/builtins/iter.rs, crates/vm/src/builtins/list.rs, crates/vm/src/builtins/mappingproxy.rs, crates/vm/src/builtins/weakproxy.rs
Updated containment and sequence access patterns to use sequence_unchecked() instead of to_sequence(). Removed PySequenceMethods caching in PySequenceIterator. Updated PyClassMethod pyclass attribute to include Initializer. Changed mapping proxy construction to use ArgMapping::new() and direct slot validation.
Standard Library
crates/stdlib/src/sqlite.rs, crates/vm/src/stdlib/builtins.rs, crates/vm/src/stdlib/ctypes/structure.rs, crates/vm/src/stdlib/ctypes/union.rs, crates/vm/src/stdlib/typing.rs
Updated numeric access from to_number() to number() in SQLite collation. Changed eval globals mapping check to use mapping_unchecked().check(). Replaced MRO-based descriptor lookups in ctypes with direct slot access. Added init() function for typing module initialization.
Module Exports
crates/vm/src/protocol/mod.rs, crates/vm/src/types/structseq.rs, crates/vm/src/types/zoo.rs
Added PyMappingSlots and PySequenceSlots to public protocol exports. Updated struct sequence slot initialization to use copy_from() for direct slot population. Added typing module initialization to TypeZoo.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • iter with slot-wrapper #6488: Modifies iterator slot-wrapper machinery and class slot handling in parallel with changes to iter initialization and PySequenceIterator refactoring.
  • disallow __new__, __init__ #6446: Updates pyclass initialization with Constructor/Initializer traits, aligning with the classmethod attribute changes and slot-based initialization patterns in this PR.
  • ctypes overhaul #6450: Modifies ctypes implementation (PyCStructType, PyCUnionType) descriptor handling, which overlaps with the descriptor slot lookup refactoring in this PR.

Suggested reviewers

  • ShaharNaveh
  • arihant2math
  • coolreader18

🐰 Protocol slots now shimmer bright,
Direct access replaces MRO's flight,
Unchecked pathways swift and clean,
Type slots wired, a cleaner scene,
The rabbit hops through refactored light!

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7eb0fe4 and c83101f.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_typing.py is excluded by !Lib/**
📒 Files selected for processing (32)
  • crates/stdlib/src/sqlite.rs
  • crates/vm/src/builtins/bool.rs
  • crates/vm/src/builtins/classmethod.rs
  • crates/vm/src/builtins/dict.rs
  • crates/vm/src/builtins/iter.rs
  • crates/vm/src/builtins/list.rs
  • crates/vm/src/builtins/mappingproxy.rs
  • crates/vm/src/builtins/object.rs
  • crates/vm/src/builtins/type.rs
  • crates/vm/src/builtins/weakproxy.rs
  • crates/vm/src/class.rs
  • crates/vm/src/frame.rs
  • crates/vm/src/function/protocol.rs
  • crates/vm/src/object/core.rs
  • crates/vm/src/protocol/buffer.rs
  • crates/vm/src/protocol/callable.rs
  • crates/vm/src/protocol/iter.rs
  • crates/vm/src/protocol/mapping.rs
  • crates/vm/src/protocol/mod.rs
  • crates/vm/src/protocol/number.rs
  • crates/vm/src/protocol/object.rs
  • crates/vm/src/protocol/sequence.rs
  • crates/vm/src/stdlib/builtins.rs
  • crates/vm/src/stdlib/ctypes/structure.rs
  • crates/vm/src/stdlib/ctypes/union.rs
  • crates/vm/src/stdlib/typing.rs
  • crates/vm/src/types/slot.rs
  • crates/vm/src/types/structseq.rs
  • crates/vm/src/types/zoo.rs
  • crates/vm/src/vm/method.rs
  • crates/vm/src/vm/vm_object.rs
  • crates/vm/src/vm/vm_ops.rs

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 and usage tips.

@youknowone youknowone force-pushed the copyslot branch 6 times, most recently from 86969fb to f6602a7 Compare December 25, 2025 04:29
@github-actions
Copy link
Contributor

Code has been automatically formatted

The code in this PR has been formatted using cargo fmt --all.
Please pull the latest changes before pushing again:

git pull origin copyslot

@youknowone youknowone marked this pull request as ready for review December 25, 2025 07:56
@youknowone youknowone merged commit 151f074 into RustPython:main Dec 25, 2025
12 of 13 checks passed
@youknowone youknowone deleted the copyslot branch December 25, 2025 07:56
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.

1 participant

0