Implement copyslot by youknowone · Pull Request #6505 · RustPython/RustPython
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
⛔ Files ignored due to path filters (1)
Lib/test/test_typing.pyis excluded by!Lib/**
📒 Files selected for processing (32)
crates/stdlib/src/sqlite.rscrates/vm/src/builtins/bool.rscrates/vm/src/builtins/classmethod.rscrates/vm/src/builtins/dict.rscrates/vm/src/builtins/iter.rscrates/vm/src/builtins/list.rscrates/vm/src/builtins/mappingproxy.rscrates/vm/src/builtins/object.rscrates/vm/src/builtins/type.rscrates/vm/src/builtins/weakproxy.rscrates/vm/src/class.rscrates/vm/src/frame.rscrates/vm/src/function/protocol.rscrates/vm/src/object/core.rscrates/vm/src/protocol/buffer.rscrates/vm/src/protocol/callable.rscrates/vm/src/protocol/iter.rscrates/vm/src/protocol/mapping.rscrates/vm/src/protocol/mod.rscrates/vm/src/protocol/number.rscrates/vm/src/protocol/object.rscrates/vm/src/protocol/sequence.rscrates/vm/src/stdlib/builtins.rscrates/vm/src/stdlib/ctypes/structure.rscrates/vm/src/stdlib/ctypes/union.rscrates/vm/src/stdlib/typing.rscrates/vm/src/types/slot.rscrates/vm/src/types/structseq.rscrates/vm/src/types/zoo.rscrates/vm/src/vm/method.rscrates/vm/src/vm/vm_object.rscrates/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.
Comment @coderabbitai help to get the list of available commands and usage tips.