Skip to content

Commit 22f6763

Browse files
committed
Extract PyInt::to_str_radix_10() to deduplicate i64 fast path logic
1 parent 872a6e0 commit 22f6763

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

crates/vm/src/builtins/int.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ impl PyInt {
267267
&self.value
268268
}
269269

270+
/// Fast decimal string conversion, using i64 path when possible.
271+
#[inline]
272+
pub fn to_str_radix_10(&self) -> String {
273+
match self.value.to_i64() {
274+
Some(i) => i.to_string(),
275+
None => self.value.to_string(),
276+
}
277+
}
278+
270279
// _PyLong_AsUnsignedLongMask
271280
pub fn as_u32_mask(&self) -> u32 {
272281
let v = self.as_bigint();
@@ -603,10 +612,7 @@ impl Comparable for PyInt {
603612
impl Representable for PyInt {
604613
#[inline]
605614
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
606-
Ok(match zelf.value.to_i64() {
607-
Some(i) => i.to_string(),
608-
None => zelf.value.to_string(),
609-
})
615+
Ok(zelf.to_str_radix_10())
610616
}
611617
}
612618

crates/vm/src/protocol/object.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{
1515
protocol::PyIter,
1616
types::{Constructor, PyComparisonOp},
1717
};
18-
use num_traits::ToPrimitive;
1918

2019
// RustPython doesn't need these items
2120
// PyObject *Py_NotImplemented
@@ -379,11 +378,7 @@ impl PyObject {
379378
// Fast path for exact int: skip __str__ method resolution
380379
let obj = match obj.downcast_exact::<PyInt>(vm) {
381380
Ok(int) => {
382-
let s = match int.as_bigint().to_i64() {
383-
Some(i) => i.to_string(),
384-
None => int.as_bigint().to_string(),
385-
};
386-
return Ok(vm.ctx.new_str(s));
381+
return Ok(vm.ctx.new_str(int.to_str_radix_10()));
387382
}
388383
Err(obj) => obj,
389384
};

0 commit comments

Comments
 (0)