slice ops and a little bit of peephole by youknowone · Pull Request #6860 · RustPython/RustPython
📝 Walkthrough
Walkthrough
The pull request introduces bytecode-level optimizations and new instruction types. Changes include a slice optimization path using a new ExprExt trait, peephole optimization for fusing consecutive load/store instructions, a CommonConstant enum for frequently-used constants, and VM execution paths for eight new or modified instructions.
Changes
| Cohort / File(s) | Summary |
|---|---|
Slice optimization logic crates/codegen/src/compile.rs |
Added ExprExt trait with is_constant, is_constant_slice, and should_use_slice_optimization methods; introduced compile_slice_two_parts for two-part slice compilation; updated compile_subscript to use new optimization path and emit BinarySlice/StoreSlice instructions when appropriate. |
Peephole optimization crates/codegen/src/ir.rs |
Added peephole_optimize method in CodeInfo.finalize_code that fuses consecutive LoadFast+LoadFast and StoreFast+StoreFast instructions into LoadFastLoadFast and StoreFastStoreFast super-instructions when both indices are < 16. |
Bytecode instruction definitions crates/compiler-core/src/bytecode/oparg.rs |
Added new public enum CommonConstant with variants (AssertionError, NotImplementedError, BuiltinTuple, BuiltinAll, BuiltinAny) and Display implementation mapping to canonical Python names. |
Instruction enum and metadata crates/compiler-core/src/bytecode/instruction.rs |
Changed LoadCommonConstant variant argument type from Arg to Arg; adjusted stack effects for LoadFastCheck (0→1) and StoreFastStoreFast (0→-2); minor formatting changes. |
Public API exports crates/compiler-core/src/bytecode.rs |
Added CommonConstant to re-exported items from oparg module. |
VM instruction execution crates/vm/src/frame.rs |
Added execution paths for eight instructions: BinarySlice, CopyFreeVars, LoadCommonConstant, LoadFastCheck, LoadFastLoadFast, MakeCell, StoreFastStoreFast, and StoreSlice with corresponding stack operations and error handling. |
Stdlib attribute exposure crates/stdlib/src/_opcode.rs |
Changed ENABLE_SPECIALIZATION_FT from #[allow(dead_code)] to #[pyattr] to expose it as a Python-accessible module attribute. |
Sequence Diagram
sequenceDiagram
participant Compiler as Compiler<br/>(compile.rs)
participant CodeGen as Code Generator<br/>(ir.rs)
participant Bytecode as Bytecode<br/>(instruction.rs)
participant Peephole as Peephole Opt.<br/>(ir.rs)
participant VM as VM Executor<br/>(frame.rs)
Compiler->>Compiler: Check is_constant_slice<br/>& should_use_slice_optimization
alt Optimize Slice Path
Compiler->>Bytecode: Emit BinarySlice/StoreSlice
else Standard Path
Compiler->>Bytecode: Emit standard slice instructions
end
Compiler->>CodeGen: Generate code with<br/>LoadFast/StoreFast pairs
CodeGen->>Peephole: Finalize code with<br/>optimization enabled
Peephole->>Peephole: Scan consecutive instructions
alt Fusible Pattern Found
Peephole->>Peephole: Fuse LoadFast+LoadFast<br/>→ LoadFastLoadFast
Peephole->>Peephole: Fuse StoreFast+StoreFast<br/>→ StoreFastStoreFast
end
Peephole->>Bytecode: Output optimized<br/>instructions
VM->>VM: Execute instruction
alt BinarySlice
VM->>VM: Pop container, start, stop
VM->>VM: Create PySlice
VM->>VM: Get item, push result
else LoadFastLoadFast
VM->>VM: Decode indices<br/>(idx1 << 4 | idx2)
VM->>VM: Load both with<br/>unbound checks
VM->>VM: Push both values
else StoreFastStoreFast
VM->>VM: Pop 2 values
VM->>VM: Store to 2 locals
end
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
- Instruction 3.14 #6805: Introduces BinarySlice, LoadCommonConstant, and fused multi-opcodes (LoadFastLoadFast/StoreFastStoreFast) alongside corresponding VM execution paths that directly align with this PR's instruction additions.
- compile_subscript #6008: Refactors compile_subscript and slice-compilation logic using is_two_element_slice detection; this PR's ExprExt::should_use_slice_optimization and compile_slice_two_parts build on that same code path.
- Sort
Instructionenum & related match arms #6322: Modifies the Instruction enum and match arms for handling new opcodes including LoadCommonConstant and other variant changes that overlap with this PR's instruction enum updates.
Suggested reviewers
- ShaharNaveh
Poem
🐰 Slices optimized, constants shine bright,
Peephole fusion makes the bytecode lean and light,
LoadFast pairs now dance as one,
The rabbit hops faster—optimizations begun! ✨
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 29.41% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title 'slice ops and a little bit of peephole' accurately describes the main changes: slice operation optimizations (BinarySlice, StoreSlice, compile_slice_two_parts) and peephole optimization (LoadFastLoadFast, StoreFastStoreFast fusion). |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
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.