Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,13 @@ impl PyDictRef {
// if we lookup local names (which happens all of the time).
if self.class().is(&vm.ctx.types.dict_type) {
// We can take the short path here!
match self.inner_getitem_option(key, vm) {
Err(exc) => {
if exc.isinstance(&vm.ctx.exceptions.key_error) {
Ok(None)
} else {
Err(exc)
}
self.inner_getitem_option(key, vm).or_else(|exc| {
if exc.isinstance(&vm.ctx.exceptions.key_error) {
Ok(None)
} else {
Err(exc)
}
Ok(x) => Ok(x),
}
})
} else {
// Fall back to full get_item with KeyError checking

Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ pub struct PyBoundMethod {
}

impl Callable for PyBoundMethod {
fn call(zelf: &PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
let args = args.insert(zelf.object.clone());
fn call(zelf: &PyRef<Self>, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
args.prepend_arg(zelf.object.clone());
vm.invoke(&zelf.function, args)
}
}
Expand Down
14 changes: 8 additions & 6 deletions vm/src/builtins/pytype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ impl PyType {
)
}
#[pyslot]
fn tp_new(metatype: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
fn tp_new(metatype: PyTypeRef, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
vm_trace!("type.__new__ {:?}", args);

let is_type_type = metatype.is(&vm.ctx.types.type_type);
Expand Down Expand Up @@ -439,8 +439,8 @@ impl PyType {
#[allow(clippy::redundant_clone)] // false positive
if let Some(ref tp_new) = winner.clone().slots.new {
// Pass it to the winner

return tp_new(vm, args.insert(winner.into_object()));
args.prepend_arg(winner.into_object());
return tp_new(vm, args);
}
winner
} else {
Expand Down Expand Up @@ -694,18 +694,20 @@ impl<T: DerefToPyType> DerefToPyType for &'_ T {
fn call_tp_new(
typ: PyTypeRef,
subtype: PyTypeRef,
args: FuncArgs,
mut args: FuncArgs,
vm: &VirtualMachine,
) -> PyResult {
for cls in typ.deref().iter_mro() {
if let Some(new_meth) = cls.get_attr("__new__") {
if !vm.ctx.is_tp_new_wrapper(&new_meth) {
let new_meth = vm.call_if_get_descriptor(new_meth, typ.clone().into_object())?;
return vm.invoke(&new_meth, args.insert(typ.clone().into_object()));
args.prepend_arg(typ.clone().into_object());
return vm.invoke(&new_meth, args);
}
}
if let Some(tp_new) = cls.slots.new.as_ref() {
return tp_new(vm, args.insert(subtype.into_object()));
args.prepend_arg(subtype.into_object());
return tp_new(vm, args);
}
}
unreachable!("Should be able to find a new slot somewhere in the mro")
Expand Down
10 changes: 3 additions & 7 deletions vm/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,9 @@ impl FuncArgs {
FuncArgs { args, kwargs }
}

pub fn insert(&self, item: PyObjectRef) -> FuncArgs {
let mut args = FuncArgs {
args: self.args.clone(),
kwargs: self.kwargs.clone(),
};
args.args.insert(0, item);
args
pub fn prepend_arg(&mut self, item: PyObjectRef) {
self.args.reserve_exact(1);
self.args.insert(0, item)
}

pub fn shift(&mut self) -> PyObjectRef {
Expand Down
11 changes: 9 additions & 2 deletions vm/src/stdlib/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,22 @@ fn _signal_alarm(time: u32) -> u32 {
}

#[cfg_attr(feature = "flame-it", flame)]
#[inline(always)]
pub fn check_signals(vm: &VirtualMachine) -> PyResult<()> {
let signal_handlers = match vm.signal_handlers {
Some(ref h) => h.borrow(),
let signal_handlers = match &vm.signal_handlers {
Some(h) => h,
None => return Ok(()),
};

if !ANY_TRIGGERED.swap(false, Ordering::Relaxed) {
return Ok(());
}

trigger_signals(&signal_handlers.borrow(), vm)
}
#[inline(never)]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about cold attribute? this is a question instead of suggestion because i haven't seen any actual usage of cold yet. https://doc.rust-lang.org/reference/attributes/codegen.html

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good, I was thinking that it really isn't that exceptional of a case, but given that it is (possibly) called for every instruction execution it would make sense to signal that it should very rarely ever happen.

#[cold]
fn trigger_signals(signal_handlers: &[PyObjectRef; NSIG], vm: &VirtualMachine) -> PyResult<()> {
for (signum, trigger) in TRIGGERS.iter().enumerate().skip(1) {
let triggerd = trigger.swap(false, Ordering::Relaxed);
if triggerd {
Expand Down