Skip to content

Commit dc09fb7

Browse files
committed
Defer BigInt::clone() for new_int
1 parent 8bce893 commit dc09fb7

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

vm/src/obj/objenumerate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl PyEnumerate {
5555
let next_obj = objiter::call_next(vm, iterator)?;
5656
let result = vm
5757
.ctx
58-
.new_tuple(vec![vm.ctx.new_int(counter.borrow().clone()), next_obj]);
58+
.new_tuple(vec![vm.ctx.new_bigint(&counter.borrow()), next_obj]);
5959

6060
AddAssign::add_assign(&mut counter.borrow_mut() as &mut BigInt, 1);
6161

vm/src/obj/objfloat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ impl PyFloat {
560560
}
561561

562562
let ratio = Ratio::from_float(value).unwrap();
563-
let numer = vm.ctx.new_int(ratio.numer().clone());
564-
let denom = vm.ctx.new_int(ratio.denom().clone());
563+
let numer = vm.ctx.new_bigint(ratio.numer());
564+
let denom = vm.ctx.new_bigint(ratio.denom());
565565
Ok(vm.ctx.new_tuple(vec![numer, denom]))
566566
}
567567

vm/src/obj/objint.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ fn inner_pow(int1: &PyInt, int2: &PyInt, vm: &VirtualMachine) -> PyResult {
126126
} else {
127127
Ok(if let Some(v2) = int2.value.to_u64() {
128128
vm.ctx.new_int(int1.value.pow(v2))
129-
} else if int1.value.is_one() || int1.value.is_zero() {
130-
vm.ctx.new_int(int1.value.clone())
129+
} else if int1.value.is_one() {
130+
vm.ctx.new_int(1)
131+
} else if int1.value.is_zero() {
132+
vm.ctx.new_int(0)
131133
} else if int1.value == BigInt::from(-1) {
132134
if int2.value.is_odd() {
133135
vm.ctx.new_int(-1)
@@ -674,7 +676,7 @@ impl PyInt {
674676
}
675677
#[pyproperty]
676678
fn real(&self, vm: &VirtualMachine) -> PyObjectRef {
677-
vm.ctx.new_int(self.value.clone())
679+
vm.ctx.new_bigint(&self.value)
678680
}
679681

680682
#[pyproperty]

vm/src/pyobject.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,17 @@ impl PyContext {
384384
PyObject::new(PyInt::new(i), self.int_type(), None)
385385
}
386386

387+
#[inline]
388+
pub fn new_bigint(&self, i: &BigInt) -> PyObjectRef {
389+
if let Some(i) = i.to_i32() {
390+
if i >= INT_CACHE_POOL_MIN && i <= INT_CACHE_POOL_MAX {
391+
let inner_idx = (i - INT_CACHE_POOL_MIN) as usize;
392+
return self.int_cache_pool[inner_idx].clone();
393+
}
394+
}
395+
PyObject::new(PyInt::new(i.clone()), self.int_type(), None)
396+
}
397+
387398
pub fn new_float(&self, value: f64) -> PyObjectRef {
388399
PyObject::new(PyFloat::from(value), self.float_type(), None)
389400
}
@@ -519,7 +530,7 @@ impl PyContext {
519530

520531
pub fn unwrap_constant(&self, value: &bytecode::Constant) -> PyObjectRef {
521532
match *value {
522-
bytecode::Constant::Integer { ref value } => self.new_int(value.clone()),
533+
bytecode::Constant::Integer { ref value } => self.new_bigint(value),
523534
bytecode::Constant::Float { ref value } => self.new_float(*value),
524535
bytecode::Constant::Complex { ref value } => self.new_complex(*value),
525536
bytecode::Constant::String { ref value } => self.new_str(value.clone()),

0 commit comments

Comments
 (0)