Skip to content

Commit cf428e1

Browse files
authored
Merge pull request RustPython#2339 from RustPython/coolreader18/mini-opts
Replace FuncArgs.insert with prepend_arg, optimize check_signals
2 parents 5781fa4 + 8ff7a52 commit cf428e1

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

vm/src/builtins/dict.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,13 @@ impl PyDictRef {
468468
// if we lookup local names (which happens all of the time).
469469
if self.class().is(&vm.ctx.types.dict_type) {
470470
// We can take the short path here!
471-
match self.inner_getitem_option(key, vm) {
472-
Err(exc) => {
473-
if exc.isinstance(&vm.ctx.exceptions.key_error) {
474-
Ok(None)
475-
} else {
476-
Err(exc)
477-
}
471+
self.inner_getitem_option(key, vm).or_else(|exc| {
472+
if exc.isinstance(&vm.ctx.exceptions.key_error) {
473+
Ok(None)
474+
} else {
475+
Err(exc)
478476
}
479-
Ok(x) => Ok(x),
480-
}
477+
})
481478
} else {
482479
// Fall back to full get_item with KeyError checking
483480

vm/src/builtins/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ pub struct PyBoundMethod {
324324
}
325325

326326
impl Callable for PyBoundMethod {
327-
fn call(zelf: &PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
328-
let args = args.insert(zelf.object.clone());
327+
fn call(zelf: &PyRef<Self>, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
328+
args.prepend_arg(zelf.object.clone());
329329
vm.invoke(&zelf.function, args)
330330
}
331331
}

vm/src/builtins/pytype.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl PyType {
398398
)
399399
}
400400
#[pyslot]
401-
fn tp_new(metatype: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
401+
fn tp_new(metatype: PyTypeRef, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
402402
vm_trace!("type.__new__ {:?}", args);
403403

404404
let is_type_type = metatype.is(&vm.ctx.types.type_type);
@@ -439,8 +439,8 @@ impl PyType {
439439
#[allow(clippy::redundant_clone)] // false positive
440440
if let Some(ref tp_new) = winner.clone().slots.new {
441441
// Pass it to the winner
442-
443-
return tp_new(vm, args.insert(winner.into_object()));
442+
args.prepend_arg(winner.into_object());
443+
return tp_new(vm, args);
444444
}
445445
winner
446446
} else {
@@ -694,18 +694,20 @@ impl<T: DerefToPyType> DerefToPyType for &'_ T {
694694
fn call_tp_new(
695695
typ: PyTypeRef,
696696
subtype: PyTypeRef,
697-
args: FuncArgs,
697+
mut args: FuncArgs,
698698
vm: &VirtualMachine,
699699
) -> PyResult {
700700
for cls in typ.deref().iter_mro() {
701701
if let Some(new_meth) = cls.get_attr("__new__") {
702702
if !vm.ctx.is_tp_new_wrapper(&new_meth) {
703703
let new_meth = vm.call_if_get_descriptor(new_meth, typ.clone().into_object())?;
704-
return vm.invoke(&new_meth, args.insert(typ.clone().into_object()));
704+
args.prepend_arg(typ.clone().into_object());
705+
return vm.invoke(&new_meth, args);
705706
}
706707
}
707708
if let Some(tp_new) = cls.slots.new.as_ref() {
708-
return tp_new(vm, args.insert(subtype.into_object()));
709+
args.prepend_arg(subtype.into_object());
710+
return tp_new(vm, args);
709711
}
710712
}
711713
unreachable!("Should be able to find a new slot somewhere in the mro")

vm/src/function/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,9 @@ impl FuncArgs {
108108
FuncArgs { args, kwargs }
109109
}
110110

111-
pub fn insert(&self, item: PyObjectRef) -> FuncArgs {
112-
let mut args = FuncArgs {
113-
args: self.args.clone(),
114-
kwargs: self.kwargs.clone(),
115-
};
116-
args.args.insert(0, item);
117-
args
111+
pub fn prepend_arg(&mut self, item: PyObjectRef) {
112+
self.args.reserve_exact(1);
113+
self.args.insert(0, item)
118114
}
119115

120116
pub fn shift(&mut self) -> PyObjectRef {

vm/src/stdlib/signal.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,22 @@ fn _signal_alarm(time: u32) -> u32 {
9898
}
9999

100100
#[cfg_attr(feature = "flame-it", flame)]
101+
#[inline(always)]
101102
pub fn check_signals(vm: &VirtualMachine) -> PyResult<()> {
102-
let signal_handlers = match vm.signal_handlers {
103-
Some(ref h) => h.borrow(),
103+
let signal_handlers = match &vm.signal_handlers {
104+
Some(h) => h,
104105
None => return Ok(()),
105106
};
106107

107108
if !ANY_TRIGGERED.swap(false, Ordering::Relaxed) {
108109
return Ok(());
109110
}
111+
112+
trigger_signals(&signal_handlers.borrow(), vm)
113+
}
114+
#[inline(never)]
115+
#[cold]
116+
fn trigger_signals(signal_handlers: &[PyObjectRef; NSIG], vm: &VirtualMachine) -> PyResult<()> {
110117
for (signum, trigger) in TRIGGERS.iter().enumerate().skip(1) {
111118
let triggerd = trigger.swap(false, Ordering::Relaxed);
112119
if triggerd {

0 commit comments

Comments
 (0)