From 560cd6ca6cbfc58e9571545518bb5f92dff55584 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 20 Apr 2023 21:35:22 +0900 Subject: [PATCH] Simpler new object / small int range const --- vm/src/vm/context.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/vm/src/vm/context.rs b/vm/src/vm/context.rs index 596bd3a932..dcf7c65202 100644 --- a/vm/src/vm/context.rs +++ b/vm/src/vm/context.rs @@ -234,8 +234,8 @@ declare_const_name! { // Basic objects: impl Context { - pub const INT_CACHE_POOL_MIN: i32 = -5; - pub const INT_CACHE_POOL_MAX: i32 = 256; + pub const INT_CACHE_POOL_RANGE: std::ops::RangeInclusive = (-5)..=256; + const INT_CACHE_POOL_MIN: i32 = *Self::INT_CACHE_POOL_RANGE.start(); pub fn genesis() -> &'static PyRc { rustpython_common::static_cell! { @@ -261,7 +261,7 @@ impl Context { let ellipsis = create_object(PyEllipsis, PyEllipsis::static_type()); let not_implemented = create_object(PyNotImplemented, PyNotImplemented::static_type()); - let int_cache_pool = (Self::INT_CACHE_POOL_MIN..=Self::INT_CACHE_POOL_MAX) + let int_cache_pool = Self::INT_CACHE_POOL_RANGE .map(|v| { PyRef::new_ref( PyInt::from(BigInt::from(v)), @@ -358,37 +358,33 @@ impl Context { #[inline] pub fn new_int + ToPrimitive>(&self, i: T) -> PyIntRef { if let Some(i) = i.to_i32() { - if (Self::INT_CACHE_POOL_MIN..=Self::INT_CACHE_POOL_MAX).contains(&i) { + if Self::INT_CACHE_POOL_RANGE.contains(&i) { let inner_idx = (i - Self::INT_CACHE_POOL_MIN) as usize; return self.int_cache_pool[inner_idx].clone(); } } - PyRef::new_ref(PyInt::from(i), self.types.int_type.to_owned(), None) + PyInt::from(i).into_ref(self) } #[inline] pub fn new_bigint(&self, i: &BigInt) -> PyIntRef { if let Some(i) = i.to_i32() { - if (Self::INT_CACHE_POOL_MIN..=Self::INT_CACHE_POOL_MAX).contains(&i) { + if Self::INT_CACHE_POOL_RANGE.contains(&i) { let inner_idx = (i - Self::INT_CACHE_POOL_MIN) as usize; return self.int_cache_pool[inner_idx].clone(); } } - PyRef::new_ref(PyInt::from(i.clone()), self.types.int_type.to_owned(), None) + PyInt::from(i.clone()).into_ref(self) } #[inline] pub fn new_float(&self, value: f64) -> PyRef { - PyRef::new_ref(PyFloat::from(value), self.types.float_type.to_owned(), None) + PyFloat::from(value).into_ref(self) } #[inline] pub fn new_complex(&self, value: Complex64) -> PyRef { - PyRef::new_ref( - PyComplex::from(value), - self.types.complex_type.to_owned(), - None, - ) + PyComplex::from(value).into_ref(self) } #[inline]