Skip to content

Commit fd21173

Browse files
authored
Simplify cache_entries match statement (RustPython#7613)
* Simplify cache_entries method * Use `Opcode`
1 parent 3602538 commit fd21173

1 file changed

Lines changed: 24 additions & 116 deletions

File tree

crates/compiler-core/src/bytecode/instruction.rs

Lines changed: 24 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -752,124 +752,32 @@ impl Instruction {
752752
}
753753

754754
/// Number of CACHE code units that follow this instruction.
755+
///
756+
/// Instrumented and specialized opcodes have the same cache entries as their base.
757+
///
755758
/// _PyOpcode_Caches
756759
pub const fn cache_entries(self) -> usize {
757-
match self {
758-
// LOAD_ATTR: 9 cache entries
759-
Self::LoadAttr { .. }
760-
| Self::LoadAttrClass
761-
| Self::LoadAttrClassWithMetaclassCheck
762-
| Self::LoadAttrGetattributeOverridden
763-
| Self::LoadAttrInstanceValue
764-
| Self::LoadAttrMethodLazyDict
765-
| Self::LoadAttrMethodNoDict
766-
| Self::LoadAttrMethodWithValues
767-
| Self::LoadAttrModule
768-
| Self::LoadAttrNondescriptorNoDict
769-
| Self::LoadAttrNondescriptorWithValues
770-
| Self::LoadAttrProperty
771-
| Self::LoadAttrSlot
772-
| Self::LoadAttrWithHint => 9,
773-
774-
// BINARY_OP: 5 cache entries
775-
Self::BinaryOp { .. }
776-
| Self::BinaryOpAddFloat
777-
| Self::BinaryOpAddInt
778-
| Self::BinaryOpAddUnicode
779-
| Self::BinaryOpExtend
780-
| Self::BinaryOpInplaceAddUnicode
781-
| Self::BinaryOpMultiplyFloat
782-
| Self::BinaryOpMultiplyInt
783-
| Self::BinaryOpSubscrDict
784-
| Self::BinaryOpSubscrGetitem
785-
| Self::BinaryOpSubscrListInt
786-
| Self::BinaryOpSubscrListSlice
787-
| Self::BinaryOpSubscrStrInt
788-
| Self::BinaryOpSubscrTupleInt
789-
| Self::BinaryOpSubtractFloat
790-
| Self::BinaryOpSubtractInt => 5,
791-
792-
// LOAD_GLOBAL / STORE_ATTR: 4 cache entries
793-
Self::LoadGlobal { .. }
794-
| Self::LoadGlobalBuiltin
795-
| Self::LoadGlobalModule
796-
| Self::StoreAttr { .. }
797-
| Self::StoreAttrInstanceValue
798-
| Self::StoreAttrSlot
799-
| Self::StoreAttrWithHint => 4,
800-
801-
// CALL / CALL_KW / TO_BOOL: 3 cache entries
802-
Self::Call { .. }
803-
| Self::CallAllocAndEnterInit
804-
| Self::CallBoundMethodExactArgs
805-
| Self::CallBoundMethodGeneral
806-
| Self::CallBuiltinClass
807-
| Self::CallBuiltinFast
808-
| Self::CallBuiltinFastWithKeywords
809-
| Self::CallBuiltinO
810-
| Self::CallIsinstance
811-
| Self::CallLen
812-
| Self::CallListAppend
813-
| Self::CallMethodDescriptorFast
814-
| Self::CallMethodDescriptorFastWithKeywords
815-
| Self::CallMethodDescriptorNoargs
816-
| Self::CallMethodDescriptorO
817-
| Self::CallNonPyGeneral
818-
| Self::CallPyExactArgs
819-
| Self::CallPyGeneral
820-
| Self::CallStr1
821-
| Self::CallTuple1
822-
| Self::CallType1
823-
| Self::CallKw { .. }
824-
| Self::CallKwBoundMethod
825-
| Self::CallKwNonPy
826-
| Self::CallKwPy
827-
| Self::ToBool
828-
| Self::ToBoolAlwaysTrue
829-
| Self::ToBoolBool
830-
| Self::ToBoolInt
831-
| Self::ToBoolList
832-
| Self::ToBoolNone
833-
| Self::ToBoolStr => 3,
834-
835-
// 1 cache entry
836-
Self::CompareOp { .. }
837-
| Self::CompareOpFloat
838-
| Self::CompareOpInt
839-
| Self::CompareOpStr
840-
| Self::ContainsOp { .. }
841-
| Self::ContainsOpDict
842-
| Self::ContainsOpSet
843-
| Self::ForIter { .. }
844-
| Self::ForIterGen
845-
| Self::ForIterList
846-
| Self::ForIterRange
847-
| Self::ForIterTuple
848-
| Self::JumpBackward { .. }
849-
| Self::JumpBackwardJit
850-
| Self::JumpBackwardNoJit
851-
| Self::LoadSuperAttr { .. }
852-
| Self::LoadSuperAttrAttr
853-
| Self::LoadSuperAttrMethod
854-
| Self::PopJumpIfTrue { .. }
855-
| Self::PopJumpIfFalse { .. }
856-
| Self::PopJumpIfNone { .. }
857-
| Self::PopJumpIfNotNone { .. }
858-
| Self::Send { .. }
859-
| Self::SendGen
860-
| Self::StoreSubscr
861-
| Self::StoreSubscrDict
862-
| Self::StoreSubscrListInt
863-
| Self::UnpackSequence { .. }
864-
| Self::UnpackSequenceList
865-
| Self::UnpackSequenceTuple
866-
| Self::UnpackSequenceTwoTuple => 1,
867-
868-
// Instrumented opcodes have the same cache entries as their base
869-
_ => match self.to_base() {
870-
Some(base) => base.cache_entries(),
871-
None => 0,
872-
},
760+
match self.deoptimize().opcode() {
761+
Opcode::LoadAttr => 9,
762+
Opcode::BinaryOp => 5,
763+
Opcode::LoadGlobal => 4,
764+
Opcode::StoreAttr => 4,
765+
Opcode::Call => 3,
766+
Opcode::CallKw => 3,
767+
Opcode::ToBool => 3,
768+
Opcode::CompareOp => 1,
769+
Opcode::ContainsOp => 1,
770+
Opcode::ForIter => 1,
771+
Opcode::JumpBackward => 1,
772+
Opcode::LoadSuperAttr => 1,
773+
Opcode::Send => 1,
774+
Opcode::StoreSubscr => 1,
775+
Opcode::UnpackSequence => 1,
776+
Opcode::PopJumpIfTrue => 1,
777+
Opcode::PopJumpIfFalse => 1,
778+
Opcode::PopJumpIfNone => 1,
779+
Opcode::PopJumpIfNotNone => 1,
780+
_ => 0,
873781
}
874782
}
875783
}

0 commit comments

Comments
 (0)