Skip to content
This repository was archived by the owner on May 11, 2023. It is now read-only.

Commit 560cd6c

Browse files
committed
Simpler new object / small int range const
1 parent 968f2ad commit 560cd6c

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

vm/src/vm/context.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ declare_const_name! {
234234

235235
// Basic objects:
236236
impl Context {
237-
pub const INT_CACHE_POOL_MIN: i32 = -5;
238-
pub const INT_CACHE_POOL_MAX: i32 = 256;
237+
pub const INT_CACHE_POOL_RANGE: std::ops::RangeInclusive<i32> = (-5)..=256;
238+
const INT_CACHE_POOL_MIN: i32 = *Self::INT_CACHE_POOL_RANGE.start();
239239

240240
pub fn genesis() -> &'static PyRc<Self> {
241241
rustpython_common::static_cell! {
@@ -261,7 +261,7 @@ impl Context {
261261
let ellipsis = create_object(PyEllipsis, PyEllipsis::static_type());
262262
let not_implemented = create_object(PyNotImplemented, PyNotImplemented::static_type());
263263

264-
let int_cache_pool = (Self::INT_CACHE_POOL_MIN..=Self::INT_CACHE_POOL_MAX)
264+
let int_cache_pool = Self::INT_CACHE_POOL_RANGE
265265
.map(|v| {
266266
PyRef::new_ref(
267267
PyInt::from(BigInt::from(v)),
@@ -358,37 +358,33 @@ impl Context {
358358
#[inline]
359359
pub fn new_int<T: Into<BigInt> + ToPrimitive>(&self, i: T) -> PyIntRef {
360360
if let Some(i) = i.to_i32() {
361-
if (Self::INT_CACHE_POOL_MIN..=Self::INT_CACHE_POOL_MAX).contains(&i) {
361+
if Self::INT_CACHE_POOL_RANGE.contains(&i) {
362362
let inner_idx = (i - Self::INT_CACHE_POOL_MIN) as usize;
363363
return self.int_cache_pool[inner_idx].clone();
364364
}
365365
}
366-
PyRef::new_ref(PyInt::from(i), self.types.int_type.to_owned(), None)
366+
PyInt::from(i).into_ref(self)
367367
}
368368

369369
#[inline]
370370
pub fn new_bigint(&self, i: &BigInt) -> PyIntRef {
371371
if let Some(i) = i.to_i32() {
372-
if (Self::INT_CACHE_POOL_MIN..=Self::INT_CACHE_POOL_MAX).contains(&i) {
372+
if Self::INT_CACHE_POOL_RANGE.contains(&i) {
373373
let inner_idx = (i - Self::INT_CACHE_POOL_MIN) as usize;
374374
return self.int_cache_pool[inner_idx].clone();
375375
}
376376
}
377-
PyRef::new_ref(PyInt::from(i.clone()), self.types.int_type.to_owned(), None)
377+
PyInt::from(i.clone()).into_ref(self)
378378
}
379379

380380
#[inline]
381381
pub fn new_float(&self, value: f64) -> PyRef<PyFloat> {
382-
PyRef::new_ref(PyFloat::from(value), self.types.float_type.to_owned(), None)
382+
PyFloat::from(value).into_ref(self)
383383
}
384384

385385
#[inline]
386386
pub fn new_complex(&self, value: Complex64) -> PyRef<PyComplex> {
387-
PyRef::new_ref(
388-
PyComplex::from(value),
389-
self.types.complex_type.to_owned(),
390-
None,
391-
)
387+
PyComplex::from(value).into_ref(self)
392388
}
393389

394390
#[inline]

0 commit comments

Comments
 (0)