diff --git a/vm/src/builtins/str.rs b/vm/src/builtins/str.rs index 90c702a14d..a1e53fcbcd 100644 --- a/vm/src/builtins/str.rs +++ b/vm/src/builtins/str.rs @@ -528,7 +528,7 @@ impl PyStr { radd?.call((zelf,), vm) } else { Err(vm.new_type_error(format!( - "can only concatenate str (not \"{}\") to str", + r#"can only concatenate str (not "{}") to str"#, other.class().name() ))) } @@ -570,6 +570,7 @@ impl PyStr { hash => hash, } } + #[cold] fn _compute_hash(&self, vm: &VirtualMachine) -> hash::PyHash { let hash_val = vm.state.hash_secret.hash_bytes(self.as_bytes()); @@ -583,6 +584,7 @@ impl PyStr { pub fn byte_len(&self) -> usize { self.data.len() } + #[inline] pub fn is_empty(&self) -> bool { self.data.is_empty() @@ -1439,6 +1441,7 @@ impl PyStr { struct CharLenStr<'a>(&'a str, usize); impl std::ops::Deref for CharLenStr<'_> { type Target = str; + fn deref(&self) -> &Self::Target { self.0 } @@ -1852,6 +1855,7 @@ impl AnyStrWrapper for PyStrRef { fn as_ref(&self) -> Option<&Wtf8> { Some(self.as_wtf8()) } + fn is_empty(&self) -> bool { self.data.is_empty() } @@ -1861,6 +1865,7 @@ impl AnyStrWrapper for PyStrRef { fn as_ref(&self) -> Option<&str> { self.data.as_str() } + fn is_empty(&self) -> bool { self.data.is_empty() } @@ -1870,6 +1875,7 @@ impl AnyStrWrapper for PyStrRef { fn as_ref(&self) -> Option<&AsciiStr> { self.data.as_ascii() } + fn is_empty(&self) -> bool { self.data.is_empty() } @@ -1893,9 +1899,11 @@ impl anystr::AnyChar for char { fn is_lowercase(self) -> bool { self.is_lowercase() } + fn is_uppercase(self) -> bool { self.is_uppercase() } + fn bytes_len(self) -> usize { self.len_utf8() } @@ -2122,9 +2130,11 @@ impl anystr::AnyChar for ascii::AsciiChar { fn is_lowercase(self) -> bool { self.is_lowercase() } + fn is_uppercase(self) -> bool { self.is_uppercase() } + fn bytes_len(self) -> usize { 1 } diff --git a/vm/src/function/argument.rs b/vm/src/function/argument.rs index 5033ee7627..cf9d035bb4 100644 --- a/vm/src/function/argument.rs +++ b/vm/src/function/argument.rs @@ -302,12 +302,14 @@ pub trait FromArgOptional { type Inner: TryFromObject; fn from_inner(x: Self::Inner) -> Self; } + impl FromArgOptional for OptionalArg { type Inner = T; fn from_inner(x: T) -> Self { Self::Present(x) } } + impl FromArgOptional for T { type Inner = Self; fn from_inner(x: Self) -> Self { @@ -342,7 +344,7 @@ where } impl KwArgs { - pub fn new(map: IndexMap) -> Self { + pub const fn new(map: IndexMap) -> Self { KwArgs(map) } @@ -354,11 +356,13 @@ impl KwArgs { self.0.is_empty() } } + impl FromIterator<(String, T)> for KwArgs { fn from_iter>(iter: I) -> Self { KwArgs(iter.into_iter().collect()) } } + impl Default for KwArgs { fn default() -> Self { KwArgs(IndexMap::new()) @@ -408,7 +412,7 @@ where } impl PosArgs { - pub fn new(args: Vec) -> Self { + pub const fn new(args: Vec) -> Self { Self(args) } diff --git a/vm/src/function/buffer.rs b/vm/src/function/buffer.rs index 40a0e04d7e..b8ef771929 100644 --- a/vm/src/function/buffer.rs +++ b/vm/src/function/buffer.rs @@ -51,11 +51,11 @@ impl ArgBytesLike { f(&self.borrow_buf()) } - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.0.desc.len } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len() == 0 } @@ -103,11 +103,11 @@ impl ArgMemoryBuffer { f(&mut self.borrow_buf_mut()) } - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.0.desc.len } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len() == 0 } } diff --git a/vm/src/object/core.rs b/vm/src/object/core.rs index 8edcb4dfd6..cc314cce69 100644 --- a/vm/src/object/core.rs +++ b/vm/src/object/core.rs @@ -491,6 +491,7 @@ pub struct PyObject(PyInner); impl Deref for PyObjectRef { type Target = PyObject; + #[inline(always)] fn deref(&self) -> &PyObject { unsafe { self.ptr.as_ref() } diff --git a/vm/src/object/ext.rs b/vm/src/object/ext.rs index b2bc6eec46..41443aa56f 100644 --- a/vm/src/object/ext.rs +++ b/vm/src/object/ext.rs @@ -47,6 +47,7 @@ where fmt::Display::fmt(&**self, f) } } + impl fmt::Display for Py where T: PyObjectPayload + fmt::Display, @@ -72,6 +73,7 @@ impl PyExact { impl Deref for PyExact { type Target = Py; + #[inline(always)] fn deref(&self) -> &Py { &self.inner @@ -108,6 +110,7 @@ impl AsRef> for PyExact { impl std::borrow::ToOwned for PyExact { type Owned = PyRefExact; + fn to_owned(&self) -> Self::Owned { let owned = self.inner.to_owned(); unsafe { PyRefExact::new_unchecked(owned) } @@ -181,6 +184,7 @@ impl TryFromObject for PyRefExact { impl Deref for PyRefExact { type Target = PyExact; + #[inline(always)] fn deref(&self) -> &PyExact { unsafe { PyExact::ref_unchecked(self.inner.deref()) } diff --git a/vm/src/object/traverse.rs b/vm/src/object/traverse.rs index 46e5daff05..3e2efefdff 100644 --- a/vm/src/object/traverse.rs +++ b/vm/src/object/traverse.rs @@ -158,6 +158,7 @@ unsafe impl Traverse for (A,) { self.0.traverse(tracer_fn); } } + trace_tuple!((A, 0), (B, 1)); trace_tuple!((A, 0), (B, 1), (C, 2)); trace_tuple!((A, 0), (B, 1), (C, 2), (D, 3)); diff --git a/vm/src/protocol/buffer.rs b/vm/src/protocol/buffer.rs index fcd44c11d3..0166920b22 100644 --- a/vm/src/protocol/buffer.rs +++ b/vm/src/protocol/buffer.rs @@ -378,12 +378,7 @@ impl BufferDescriptor { } pub fn is_zero_in_shape(&self) -> bool { - for (shape, _, _) in self.dim_desc.iter().cloned() { - if shape == 0 { - return true; - } - } - false + self.dim_desc.iter().any(|(shape, _, _)| *shape == 0) } // TODO: support fortain order diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index 254134991c..34f1b01ca8 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -107,6 +107,7 @@ where O: Borrow, { type Target = PyObject; + #[inline(always)] fn deref(&self) -> &Self::Target { self.0.borrow() @@ -242,7 +243,7 @@ impl<'a, T, O> PyIterIter<'a, T, O> where O: Borrow, { - pub fn new(vm: &'a VirtualMachine, obj: O, length_hint: Option) -> Self { + pub const fn new(vm: &'a VirtualMachine, obj: O, length_hint: Option) -> Self { Self { vm, obj, diff --git a/vm/src/protocol/number.rs b/vm/src/protocol/number.rs index 94c4f39796..0a05cbfb8a 100644 --- a/vm/src/protocol/number.rs +++ b/vm/src/protocol/number.rs @@ -18,7 +18,7 @@ pub type PyNumberTernaryFunc = fn(&PyObject, &PyObject, &PyObject, &VirtualMachi impl PyObject { #[inline] - pub fn to_number(&self) -> PyNumber<'_> { + pub const fn to_number(&self) -> PyNumber<'_> { PyNumber(self) } @@ -440,7 +440,7 @@ impl Deref for PyNumber<'_> { } impl<'a> PyNumber<'a> { - pub(crate) fn obj(self) -> &'a PyObject { + pub(crate) const fn obj(self) -> &'a PyObject { self.0 } diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index e2121973ec..7c92dbd923 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -1116,7 +1116,7 @@ impl PyComparisonOp { } } - pub fn operator_token(self) -> &'static str { + pub const fn operator_token(self) -> &'static str { match self { Self::Lt => "<", Self::Le => "<=",