Skip to content

Commit 38ee021

Browse files
committed
Drop shortcuts that only forward payload methods
Tuple length and iteration go through as_slice. Dict and list expose len. Set add stays on the payload. Assisted-by: Grok CLI:grok-4.7
1 parent d82a950 commit 38ee021

11 files changed

Lines changed: 31 additions & 172 deletions

File tree

‎crates/vm/src/builtins/bytearray.rs‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,6 @@ impl PyByteArray {
569569

570570
#[pyclass]
571571
impl Py<PyByteArray> {
572-
#[inline]
573-
pub fn borrow_buf(&self) -> PyMappedDetachingRwLockReadGuard<'_, [u8]> {
574-
self.payload().borrow_buf()
575-
}
576-
577572
fn __setitem__(
578573
&self,
579574
needle: &PyObject,

‎crates/vm/src/builtins/dict.rs‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -751,25 +751,10 @@ impl Representable for PyDict {
751751

752752
impl Py<PyDict> {
753753
#[inline]
754-
pub fn is_empty(&self) -> bool {
755-
self.payload().is_empty()
756-
}
757-
758-
#[inline]
759-
pub fn size(&self) -> dict_inner::DictSize {
760-
self.payload().size()
761-
}
762-
763-
#[inline]
764-
pub fn __len__(&self) -> usize {
754+
pub fn len(&self) -> usize {
765755
self.payload().__len__()
766756
}
767757

768-
#[inline]
769-
pub fn contains_key<K: DictKey + ?Sized>(&self, key: &K, vm: &VirtualMachine) -> bool {
770-
self.payload().contains_key(key, vm)
771-
}
772-
773758
#[inline]
774759
fn exact_dict(&self, vm: &VirtualMachine) -> bool {
775760
self.class().is(vm.ctx.types.dict_type)

‎crates/vm/src/builtins/float.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ impl PyFloat {
3535
}
3636
}
3737

38-
impl Py<PyFloat> {
39-
#[inline]
40-
pub fn to_f64(&self) -> f64 {
41-
self.payload().to_f64()
42-
}
43-
}
44-
4538
thread_local! {
4639
static FLOAT_FREELIST: Cell<crate::object::FreeList<PyFloat>> = const { Cell::new(crate::object::FreeList::new()) };
4740
}

‎crates/vm/src/builtins/int.rs‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -747,21 +747,6 @@ impl AsNumber for PyInt {
747747
}
748748
}
749749

750-
impl Py<PyInt> {
751-
#[inline]
752-
pub fn as_bigint(&self) -> &BigInt {
753-
self.payload().as_bigint()
754-
}
755-
756-
#[inline]
757-
pub fn try_to_primitive<'a, I>(&'a self, vm: &VirtualMachine) -> PyResult<I>
758-
where
759-
I: PrimInt + TryFrom<&'a BigInt>,
760-
{
761-
self.payload().try_to_primitive(vm)
762-
}
763-
}
764-
765750
impl PyInt {
766751
pub(super) const AS_NUMBER: PyNumberMethods = PyNumberMethods {
767752
add: Some(|a, b, vm| Self::number_op(a, b, |a, b, _vm| a + b, vm)),

‎crates/vm/src/builtins/list.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Py<PyList> {
140140
}
141141

142142
#[inline]
143-
pub fn __len__(&self) -> usize {
143+
pub fn len(&self) -> usize {
144144
self.payload().__len__()
145145
}
146146
}

‎crates/vm/src/builtins/set.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,13 +1069,6 @@ impl Representable for PySet {
10691069
}
10701070
}
10711071

1072-
impl Py<PySet> {
1073-
#[inline]
1074-
pub fn add(&self, object: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1075-
self.payload().add(object, vm)
1076-
}
1077-
}
1078-
10791072
impl Constructor for PyFrozenSet {
10801073
type Args = OptionalArg<PyObjectRef>;
10811074

‎crates/vm/src/builtins/str.rs‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -639,26 +639,6 @@ impl Py<PyStr> {
639639
self.payload().as_bytes()
640640
}
641641

642-
#[inline]
643-
pub fn to_str(&self) -> Option<&str> {
644-
self.payload().to_str()
645-
}
646-
647-
#[inline]
648-
pub fn byte_len(&self) -> usize {
649-
self.payload().byte_len()
650-
}
651-
652-
#[inline]
653-
pub fn char_len(&self) -> usize {
654-
self.payload().char_len()
655-
}
656-
657-
#[inline]
658-
pub fn to_string_lossy(&self) -> Cow<'_, str> {
659-
self.payload().to_string_lossy()
660-
}
661-
662642
pub fn as_utf8(&self) -> Option<&Py<PyUtf8Str>> {
663643
if self.is_utf8() {
664644
// SAFETY: is_utf8() guarantees the PyUtf8Str invariant.

‎crates/vm/src/builtins/tuple.rs‎

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl<'a, R> core::iter::IntoIterator for &'a PyTuple<R> {
295295
type IntoIter = core::slice::Iter<'a, R>;
296296

297297
fn into_iter(self) -> Self::IntoIter {
298-
self.iter()
298+
self.as_slice().iter()
299299
}
300300
}
301301

@@ -304,7 +304,7 @@ impl<'a, R> core::iter::IntoIterator for &'a Py<PyTuple<R>> {
304304
type IntoIter = core::slice::Iter<'a, R>;
305305

306306
fn into_iter(self) -> Self::IntoIter {
307-
self.iter()
307+
PyTuple::as_slice(self).iter()
308308
}
309309
}
310310

@@ -313,23 +313,6 @@ impl<R> PyTuple<R> {
313313
pub fn as_slice(&self) -> &[R] {
314314
&self.elements
315315
}
316-
317-
#[inline]
318-
#[must_use]
319-
pub fn len(&self) -> usize {
320-
self.as_slice().len()
321-
}
322-
323-
#[inline]
324-
#[must_use]
325-
pub fn is_empty(&self) -> bool {
326-
self.as_slice().is_empty()
327-
}
328-
329-
#[inline]
330-
pub fn iter(&self) -> core::slice::Iter<'_, R> {
331-
self.as_slice().iter()
332-
}
333316
}
334317

335318
impl PyTuple<PyObjectRef> {
@@ -367,7 +350,7 @@ impl PyTuple<PyObjectRef> {
367350
}
368351

369352
fn repeat(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
370-
Ok(if zelf.is_empty() || value == 0 {
353+
Ok(if zelf.as_slice().is_empty() || value == 0 {
371354
vm.ctx.empty_tuple.clone()
372355
} else if value == 1 && zelf.class().is(vm.ctx.types.tuple_type) {
373356
// Special case: when some `tuple` is multiplied by `1`,
@@ -392,21 +375,6 @@ impl Py<PyTuple> {
392375
self.payload().as_slice()
393376
}
394377

395-
#[inline]
396-
pub fn len(&self) -> usize {
397-
self.as_slice().len()
398-
}
399-
400-
#[inline]
401-
pub fn is_empty(&self) -> bool {
402-
self.as_slice().is_empty()
403-
}
404-
405-
#[inline]
406-
pub fn iter(&self) -> core::slice::Iter<'_, PyObjectRef> {
407-
self.as_slice().iter()
408-
}
409-
410378
pub fn extract_tuple<'a, T: FromPyTuple<'a>>(&'a self, vm: &VirtualMachine) -> PyResult<T> {
411379
T::from_pytuple(self, vm)
412380
}
@@ -453,12 +421,13 @@ impl PyTuple {
453421
vm: &VirtualMachine,
454422
) -> PyArithmeticValue<PyRef<Self>> {
455423
let added = other.downcast::<Self>().map(|other| {
456-
if other.is_empty() && zelf.class().is(vm.ctx.types.tuple_type) {
424+
if other.as_slice().is_empty() && zelf.class().is(vm.ctx.types.tuple_type) {
457425
zelf
458-
} else if zelf.is_empty() && other.class().is(vm.ctx.types.tuple_type) {
426+
} else if zelf.as_slice().is_empty() && other.class().is(vm.ctx.types.tuple_type) {
459427
other
460428
} else {
461429
let elements = zelf
430+
.as_slice()
462431
.iter()
463432
.chain(other.as_slice())
464433
.cloned()
@@ -486,7 +455,7 @@ impl PyTuple {
486455
#[inline]
487456
#[must_use]
488457
pub fn __len__(&self) -> usize {
489-
self.len()
458+
self.as_slice().len()
490459
}
491460

492461
fn __mul__(zelf: PyRef<Self>, value: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
@@ -520,8 +489,8 @@ impl PyTuple {
520489
range: OptionalRangeArgs,
521490
vm: &VirtualMachine,
522491
) -> PyResult<usize> {
523-
let (start, stop) = range.saturate(self.len(), vm)?;
524-
for (index, element) in self.iter().enumerate().take(stop).skip(start) {
492+
let (start, stop) = range.saturate(self.as_slice().len(), vm)?;
493+
for (index, element) in self.as_slice().iter().enumerate().take(stop).skip(start) {
525494
if vm.identical_or_equal(element, &needle)? {
526495
return Ok(index);
527496
}
@@ -568,7 +537,9 @@ impl PyTuple {
568537
impl AsMapping for PyTuple {
569538
fn as_mapping() -> &'static PyMappingMethods {
570539
static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods {
571-
length: atomic_func!(|mapping, _vm| Ok(PyTuple::mapping_downcast(mapping).len())),
540+
length: atomic_func!(|mapping, _vm| {
541+
Ok(PyTuple::mapping_downcast(mapping).as_slice().len())
542+
}),
572543
subscript: atomic_func!(
573544
|mapping, needle, vm| PyTuple::mapping_downcast(mapping)._getitem(needle, vm)
574545
),
@@ -615,7 +586,7 @@ impl AsNumber for PyTuple {
615586
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
616587
boolean: Some(|number, _vm| {
617588
let zelf = number.obj.downcast_ref::<PyTuple>().unwrap();
618-
Ok(!zelf.is_empty())
589+
Ok(!zelf.as_slice().is_empty())
619590
}),
620591
..PyNumberMethods::NOT_IMPLEMENTED
621592
};
@@ -641,9 +612,10 @@ impl Comparable for PyTuple {
641612
return Ok(res.into());
642613
}
643614
let other = class_or_notimplemented!(Self, other);
644-
zelf.iter()
615+
zelf.as_slice()
616+
.iter()
645617
.map(|o| &**o)
646-
.richcompare(other.iter().map(|o| &**o), op, vm)
618+
.richcompare(other.as_slice().iter().map(|o| &**o), op, vm)
647619
.map(PyComparisonValue::Implemented)
648620
}
649621
}
@@ -660,13 +632,20 @@ impl Iterable for PyTuple {
660632
impl Representable for PyTuple {
661633
#[inline]
662634
fn repr(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
663-
let s = if zelf.is_empty() {
635+
let s = if zelf.as_slice().is_empty() {
664636
vm.ctx.intern_str("()").to_owned()
665637
} else if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
666-
let s = if zelf.len() == 1 {
667-
wtf8_concat!("(", zelf.elements[0].repr(vm)?.as_wtf8(), ",)")
638+
let s = if zelf.as_slice().len() == 1 {
639+
wtf8_concat!("(", zelf.as_slice()[0].repr(vm)?.as_wtf8(), ",)")
668640
} else {
669-
collection_repr(None, "(", ")", "()", zelf.iter().map(|o| &**o), vm)?
641+
collection_repr(
642+
None,
643+
"(",
644+
")",
645+
"()",
646+
zelf.as_slice().iter().map(|o| &**o),
647+
vm,
648+
)?
670649
};
671650
vm.ctx.new_str(s)
672651
} else {
@@ -733,14 +712,14 @@ impl PyPayload for PyTupleIterator {
733712
impl PyTupleIterator {
734713
#[pymethod]
735714
fn __length_hint__(&self) -> usize {
736-
self.internal.lock().length_hint(|obj| obj.len())
715+
self.internal.lock().length_hint(|obj| obj.as_slice().len())
737716
}
738717

739718
#[pymethod]
740719
fn __setstate__(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
741720
self.internal
742721
.lock()
743-
.set_state(&state, |obj, pos| pos.min(obj.len()), vm)
722+
.set_state(&state, |obj, pos| pos.min(obj.as_slice().len()), vm)
744723
}
745724

746725
#[pymethod]

‎crates/vm/src/builtins/type.rs‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,36 +1651,6 @@ impl PyType {
16511651
}
16521652

16531653
impl Py<PyType> {
1654-
#[inline]
1655-
pub fn name(&self) -> BorrowedValue<'_, str> {
1656-
self.payload().name()
1657-
}
1658-
1659-
#[inline]
1660-
pub fn slot_name(&self) -> BorrowedValue<'_, str> {
1661-
self.payload().slot_name()
1662-
}
1663-
1664-
#[inline]
1665-
pub fn set_attr(&self, attr_name: &'static PyStrInterned, value: PyObjectRef) {
1666-
self.payload().set_attr(attr_name, value)
1667-
}
1668-
1669-
#[inline]
1670-
pub fn get_attr(&self, attr_name: &'static PyStrInterned) -> Option<PyObjectRef> {
1671-
self.payload().get_attr(attr_name)
1672-
}
1673-
1674-
#[inline]
1675-
pub fn get_direct_attr(&self, attr_name: &'static PyStrInterned) -> Option<PyObjectRef> {
1676-
self.payload().get_direct_attr(attr_name)
1677-
}
1678-
1679-
#[inline]
1680-
pub fn has_attr(&self, attr_name: &'static PyStrInterned) -> bool {
1681-
self.payload().has_attr(attr_name)
1682-
}
1683-
16841654
#[inline]
16851655
pub fn slots(&self) -> &PyTypeSlots {
16861656
&self.payload().slots

‎crates/vm/src/exceptions.rs‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -777,21 +777,11 @@ impl PyBaseException {
777777

778778
#[pyclass]
779779
impl Py<PyBaseException> {
780-
#[inline]
781-
pub fn args(&self) -> PyTupleRef {
782-
self.payload().args()
783-
}
784-
785780
#[inline]
786781
pub fn traceback(&self) -> Option<PyTracebackRef> {
787782
self.payload().__traceback__()
788783
}
789784

790-
#[inline]
791-
pub fn set_traceback(&self, traceback: Option<PyTracebackRef>) {
792-
self.payload().set_traceback(traceback)
793-
}
794-
795785
#[pymethod]
796786
pub(super) fn __str__(&self, vm: &VirtualMachine) -> PyStrRef {
797787
let str_args = vm.exception_args_as_string(&self.args(), true);

0 commit comments

Comments
 (0)