Skip to content

Defer BigInt::clone() for new_int #1505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vm/src/obj/objenumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl PyEnumerate {
let next_obj = objiter::call_next(vm, iterator)?;
let result = vm
.ctx
.new_tuple(vec![vm.ctx.new_int(counter.borrow().clone()), next_obj]);
.new_tuple(vec![vm.ctx.new_bigint(&counter.borrow()), next_obj]);

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

Expand Down
4 changes: 2 additions & 2 deletions vm/src/obj/objfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ impl PyFloat {
}

let ratio = Ratio::from_float(value).unwrap();
let numer = vm.ctx.new_int(ratio.numer().clone());
let denom = vm.ctx.new_int(ratio.denom().clone());
let numer = vm.ctx.new_bigint(ratio.numer());
let denom = vm.ctx.new_bigint(ratio.denom());
Ok(vm.ctx.new_tuple(vec![numer, denom]))
}

Expand Down
8 changes: 5 additions & 3 deletions vm/src/obj/objint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ fn inner_pow(int1: &PyInt, int2: &PyInt, vm: &VirtualMachine) -> PyResult {
} else {
Ok(if let Some(v2) = int2.value.to_u64() {
vm.ctx.new_int(int1.value.pow(v2))
} else if int1.value.is_one() || int1.value.is_zero() {
vm.ctx.new_int(int1.value.clone())
} else if int1.value.is_one() {
vm.ctx.new_int(1)
} else if int1.value.is_zero() {
vm.ctx.new_int(0)
} else if int1.value == BigInt::from(-1) {
if int2.value.is_odd() {
vm.ctx.new_int(-1)
Expand Down Expand Up @@ -674,7 +676,7 @@ impl PyInt {
}
#[pyproperty]
fn real(&self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(self.value.clone())
vm.ctx.new_bigint(&self.value)
}

#[pyproperty]
Expand Down
13 changes: 12 additions & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,17 @@ impl PyContext {
PyObject::new(PyInt::new(i), self.int_type(), None)
}

#[inline]
pub fn new_bigint(&self, i: &BigInt) -> PyObjectRef {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coolreader18 Do you have any idea to integrate this function into new_int?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could maybe declare your own trait and implement it for both T where T: Into<BigInt> + ToPrimitive and &T where T: Into<BigInt> + ToPrimitive. I think the real issue is that ToPrimitive should also be implemented by num-traits for &T where T: ToPrimitive.

Copy link
Member Author

@youknowone youknowone Oct 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice. But for now, I think I am not interested in working for trait puzzle.
I am sorry but would you review this PR as is again please?

if let Some(i) = i.to_i32() {
if i >= INT_CACHE_POOL_MIN && i <= INT_CACHE_POOL_MAX {
let inner_idx = (i - INT_CACHE_POOL_MIN) as usize;
return self.int_cache_pool[inner_idx].clone();
}
}
PyObject::new(PyInt::new(i.clone()), self.int_type(), None)
}

pub fn new_float(&self, value: f64) -> PyObjectRef {
PyObject::new(PyFloat::from(value), self.float_type(), None)
}
Expand Down Expand Up @@ -519,7 +530,7 @@ impl PyContext {

pub fn unwrap_constant(&self, value: &bytecode::Constant) -> PyObjectRef {
match *value {
bytecode::Constant::Integer { ref value } => self.new_int(value.clone()),
bytecode::Constant::Integer { ref value } => self.new_bigint(value),
bytecode::Constant::Float { ref value } => self.new_float(*value),
bytecode::Constant::Complex { ref value } => self.new_complex(*value),
bytecode::Constant::String { ref value } => self.new_str(value.clone()),
Expand Down