Skip to content

Commit cd87f6d

Browse files
committed
Try some mini-optimizations
1 parent 7c58f4a commit cd87f6d

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

vm/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl ExecutingFrame<'_> {
331331
let instr = &self.code.instructions[idx];
332332
let result = self.execute_instruction(instr, vm);
333333
match result {
334-
Ok(None) => {}
334+
Ok(None) => continue,
335335
Ok(Some(value)) => {
336336
break Ok(value);
337337
}
@@ -349,7 +349,7 @@ impl ExecutingFrame<'_> {
349349
exception.set_traceback(Some(new_traceback.into_ref(vm)));
350350

351351
match self.unwind_blocks(vm, UnwindReason::Raising { exception }) {
352-
Ok(None) => {}
352+
Ok(None) => continue,
353353
Ok(Some(result)) => {
354354
break Ok(result);
355355
}

vm/src/pyobject.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -383,26 +383,41 @@ impl<T> TryFromObject for PyRef<T>
383383
where
384384
T: PyValue,
385385
{
386+
#[inline]
386387
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
387388
let class = T::class(vm);
388389
if obj.isinstance(class) {
389-
obj.downcast().map_err(|obj| {
390-
vm.new_runtime_error(format!(
391-
"Unexpected payload '{}' for type '{}'",
392-
class.name,
393-
obj.class().name,
394-
))
395-
})
390+
obj.downcast()
391+
.map_err(|obj| pyref_payload_error(vm, class, obj))
396392
} else {
397-
let expected_type = &class.name;
398-
let actual_type = &obj.class().name;
399-
Err(vm.new_type_error(format!(
400-
"Expected type '{}', not '{}'",
401-
expected_type, actual_type,
402-
)))
393+
Err(pyref_type_error(vm, class, obj))
403394
}
404395
}
405396
}
397+
// the impl Borrow allows to pass PyObjectRef or &PyObjectRef
398+
fn pyref_payload_error(
399+
vm: &VirtualMachine,
400+
class: &PyTypeRef,
401+
obj: impl std::borrow::Borrow<PyObjectRef>,
402+
) -> PyBaseExceptionRef {
403+
vm.new_runtime_error(format!(
404+
"Unexpected payload '{}' for type '{}'",
405+
&*class.name,
406+
&*obj.borrow().class().name,
407+
))
408+
}
409+
fn pyref_type_error(
410+
vm: &VirtualMachine,
411+
class: &PyTypeRef,
412+
obj: impl std::borrow::Borrow<PyObjectRef>,
413+
) -> PyBaseExceptionRef {
414+
let expected_type = &*class.name;
415+
let actual_type = &*obj.borrow().class().name;
416+
vm.new_type_error(format!(
417+
"Expected type '{}', not '{}'",
418+
expected_type, actual_type,
419+
))
420+
}
406421

407422
impl<'a, T: PyValue> From<&'a PyRef<T>> for &'a PyObjectRef {
408423
fn from(obj: &'a PyRef<T>) -> Self {
@@ -787,19 +802,10 @@ unsafe impl<T: PyValue> TransmuteFromObject for PyRef<T> {
787802
if obj.payload_is::<T>() {
788803
Ok(())
789804
} else {
790-
Err(vm.new_runtime_error(format!(
791-
"Unexpected payload '{}' for type '{}'",
792-
class.name,
793-
obj.class().name,
794-
)))
805+
Err(pyref_payload_error(vm, class, obj))
795806
}
796807
} else {
797-
let expected_type = &class.name;
798-
let actual_type = &obj.class().name;
799-
Err(vm.new_type_error(format!(
800-
"Expected type '{}', not '{}'",
801-
expected_type, actual_type,
802-
)))
808+
Err(pyref_type_error(vm, class, obj))
803809
}
804810
}
805811
}

0 commit comments

Comments
 (0)