From f647f346f23c219d2b1c6ee7ed0f8eb241e0ccca Mon Sep 17 00:00:00 2001 From: Adrian Wielgosik Date: Wed, 13 Mar 2019 20:07:57 +0100 Subject: [PATCH 1/2] Use Into instead of ToBigInt to avoid copies --- vm/src/obj/objint.rs | 7 ++----- vm/src/pyobject.rs | 3 +-- vm/src/vm.rs | 4 ++-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index dbabe79907..582dd2ddb1 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -24,11 +24,8 @@ pub struct PyInt { pub type PyIntRef = PyRef; impl PyInt { - pub fn new(i: T) -> Self { - PyInt { - // TODO: this .clone()s a BigInt, which is not what we want. - value: i.to_bigint().unwrap(), - } + pub fn new>(i: T) -> Self { + PyInt { value: i.into() } } } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 0535779487..303d0cc695 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -10,7 +10,6 @@ use std::ptr; use std::rc::Rc; use num_bigint::BigInt; -use num_bigint::ToBigInt; use num_complex::Complex64; use num_traits::{One, Zero}; @@ -508,7 +507,7 @@ impl PyContext { self.new_instance(self.object(), None) } - pub fn new_int(&self, i: T) -> PyObjectRef { + pub fn new_int>(&self, i: T) -> PyObjectRef { PyObject::new(PyInt::new(i), self.int_type()) } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index d57d0e4388..26e6e57bae 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -32,7 +32,7 @@ use crate::pyobject::{ }; use crate::stdlib; use crate::sysmodule; -use num_bigint::ToBigInt; +use num_bigint::BigInt; // use objects::objects; @@ -109,7 +109,7 @@ impl VirtualMachine { } /// Create a new python int object. - pub fn new_int(&self, i: T) -> PyObjectRef { + pub fn new_int>(&self, i: T) -> PyObjectRef { self.ctx.new_int(i) } From 6b1598dbb46f21c6095729c461bbd26e11916106 Mon Sep 17 00:00:00 2001 From: Adrian Wielgosik Date: Wed, 13 Mar 2019 20:21:07 +0100 Subject: [PATCH 2/2] Change some methods to return BigInt directly --- vm/src/obj/objint.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 582dd2ddb1..107ec7e309 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -29,6 +29,12 @@ impl PyInt { } } +impl IntoPyObject for BigInt { + fn into_pyobject(self, ctx: &PyContext) -> PyResult { + Ok(ctx.new_int(self)) + } +} + impl PyValue for PyInt { fn required_type(ctx: &PyContext) -> PyObjectRef { ctx.int_type() @@ -312,8 +318,8 @@ impl PyIntRef { } } - fn neg(self, vm: &mut VirtualMachine) -> PyObjectRef { - vm.ctx.new_int(-(&self.value)) + fn neg(self, _vm: &mut VirtualMachine) -> BigInt { + -(&self.value) } fn hash(self, _vm: &mut VirtualMachine) -> u64 { @@ -322,8 +328,8 @@ impl PyIntRef { hasher.finish() } - fn abs(self, vm: &mut VirtualMachine) -> PyObjectRef { - vm.ctx.new_int(self.value.abs()) + fn abs(self, _vm: &mut VirtualMachine) -> BigInt { + self.value.abs() } fn round(self, _precision: OptionalArg, _vm: &mut VirtualMachine) -> Self { @@ -334,8 +340,8 @@ impl PyIntRef { self.value.to_f64().unwrap() } - fn invert(self, vm: &mut VirtualMachine) -> PyObjectRef { - vm.ctx.new_int(!(&self.value)) + fn invert(self, _vm: &mut VirtualMachine) -> BigInt { + !(&self.value) } fn repr(self, _vm: &mut VirtualMachine) -> String {