Bytecode oparg optimization by ShaharNaveh · Pull Request #7032 · RustPython/RustPython
📝 Walkthrough
Walkthrough
This PR refactors bytecode enum handling and casting patterns across the compiler. It replaces explicit u32 casts with standard conversion functions (u32::from()), removes the num_enum dependency, adds Debug derives to bytecode types, macro-generates oparg enum definitions and trait implementations, and removes const qualifiers from eval_ord methods. All changes maintain functional equivalence while modernizing the codebase.
Changes
| Cohort / File(s) | Summary |
|---|---|
Casting & Conversion Modernization crates/codegen/src/compile.rs, crates/vm/src/types/slot.rs |
Updated explicit u32 and u8 casts to use u32::from() and u8::from() for ResumeType and comparison operators. In compile.rs, also adjusted oparg calculation for BUILD_INTERPOLATION. Removed const qualifier from eval_ord methods in slot.rs. |
Bytecode Debug Support crates/compiler-core/src/bytecode.rs |
Added Debug derive to CodeUnit and CodeUnits structs to enable debug formatting alongside existing Copy/Clone and Clone derives respectively. |
Enum Macro Refactoring crates/compiler-core/src/bytecode/oparg.rs |
Introduced oparg_enum! and impl_oparg_enum macros to auto-generate TryFrom, From, and OpArgType trait implementations. Refactored all public enums (ConvertValueOparg, ResumeType, RaiseKind, IntrinsicFunction1/2, Intrinsic, BinaryOperator, ComparisonOperator, Invert, SpecialMethod, CommonConstant, BuildSliceArgCount) to use macro-based definitions, eliminating repetitive manual trait impls. |
Dependency Cleanup crates/compiler-core/Cargo.toml |
Removed workspace dependency on num_enum from [dependencies] section. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~22 minutes
Possibly related PRs
- Instruction::Resume #5944: Directly related through compile.rs ResumeType cast updates that align with Resume instruction introduction and ResumeType handling.
- Use
num_enumcrate for oparg types #6980: Connected via oparg enum representation changes and conversion code refactoring; both touch ResumeType and num_enum-related conversions. - Update opcode, dis from 3.14.2 #6869: Related through resume emission logic modifications in compile.rs where ResumeType-to-u32 conversions are adjusted.
Suggested reviewers
- youknowone
Poem
🐰 Hoppy hops through casting code,
Macros bloom along the road!
Enums dance in templates bright,
From-conversions feel so right! ✨
Debug fields now shine with care,
Refactored with rabbit flair!
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title 'Bytecode oparg optimization' directly and accurately reflects the main objective of the PR: optimizing bytecode operand arguments through macro-based enum generation and the removal of the num_enum dependency. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
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.