Skip to content

impl __sizeof__ for bytearray, int, list and str #1426

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 4 commits into from
Oct 16, 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
5 changes: 5 additions & 0 deletions vm/src/dictdatatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use num_bigint::ToBigInt;
/// And: http://code.activestate.com/recipes/578375/
use std::collections::{hash_map::DefaultHasher, HashMap};
use std::hash::{Hash, Hasher};
use std::mem::size_of;

/// hash value of an object returned by __hash__
type HashValue = pyhash::PyHash;
Expand Down Expand Up @@ -269,6 +270,10 @@ impl<T: Clone> Dict<T> {
None => None,
}
}

pub fn sizeof(&self) -> usize {
size_of::<Self>() + self.size * size_of::<DictEntry<T>>()
}
}

enum LookupResult {
Expand Down
6 changes: 6 additions & 0 deletions vm/src/obj/objbytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::pyobject::{
TryFromObject,
};
use crate::vm::VirtualMachine;
use std::mem::size_of;

/// "bytearray(iterable_of_ints) -> bytearray\n\
/// bytearray(string, encoding[, errors]) -> bytearray\n\
Expand Down Expand Up @@ -109,6 +110,11 @@ impl PyByteArrayRef {
self.inner.borrow().len()
}

#[pymethod(name = "__sizeof__")]
fn sizeof(self, _vm: &VirtualMachine) -> usize {
size_of::<Self>() + self.inner.borrow().len() * size_of::<u8>()
}

#[pymethod(name = "__eq__")]
fn eq(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.inner.borrow().eq(other, vm)
Expand Down
7 changes: 7 additions & 0 deletions vm/src/obj/objdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::pyobject::{
};
use crate::vm::{ReprGuard, VirtualMachine};

use std::mem::size_of;

pub type DictContentType = dictdatatype::Dict;

#[derive(Default)]
Expand Down Expand Up @@ -154,6 +156,10 @@ impl PyDictRef {
self.entries.borrow().len()
}

fn sizeof(self, _vm: &VirtualMachine) -> usize {
size_of::<Self>() + self.entries.borrow().sizeof()
}

fn repr(self, vm: &VirtualMachine) -> PyResult<String> {
let s = if let Some(_guard) = ReprGuard::enter(self.as_object()) {
let mut str_parts = vec![];
Expand Down Expand Up @@ -576,6 +582,7 @@ pub fn init(context: &PyContext) {
extend_class!(context, &context.types.dict_type, {
"__bool__" => context.new_rustfunc(PyDictRef::bool),
"__len__" => context.new_rustfunc(PyDictRef::len),
"__sizeof__" => context.new_rustfunc(PyDictRef::sizeof),
"__contains__" => context.new_rustfunc(PyDictRef::contains),
"__delitem__" => context.new_rustfunc(PyDictRef::inner_delitem),
"__eq__" => context.new_rustfunc(PyDictRef::eq),
Expand Down
6 changes: 6 additions & 0 deletions vm/src/obj/objint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use std::mem::size_of;
use std::str;

use num_bigint::{BigInt, Sign};
Expand Down Expand Up @@ -573,6 +574,11 @@ impl PyInt {
!self.value.is_zero()
}

#[pymethod(name = "__sizeof__")]
fn sizeof(&self, _vm: &VirtualMachine) -> usize {
Copy link
Member

@coolreader18 coolreader18 Sep 28, 2019

Choose a reason for hiding this comment

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

Maybe use self.value.bits() and then round up to the nearest u64 for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

something like (self.value.bits() as f64 / 8.).ceil() as usize ?

size_of::<Self>()
}

#[pymethod]
fn bit_length(&self, _vm: &VirtualMachine) -> usize {
self.value.bits()
Expand Down
6 changes: 6 additions & 0 deletions vm/src/obj/objlist.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::{Cell, RefCell};
use std::fmt;
use std::mem::size_of;
use std::ops::Range;

use num_bigint::{BigInt, ToBigInt};
Expand Down Expand Up @@ -191,6 +192,10 @@ impl PyListRef {
self.elements.borrow().len()
}

fn sizeof(self, _vm: &VirtualMachine) -> usize {
size_of::<Self>() + self.elements.borrow().capacity() * size_of::<PyObjectRef>()
}

fn reverse(self, _vm: &VirtualMachine) {
self.elements.borrow_mut().reverse();
}
Expand Down Expand Up @@ -879,6 +884,7 @@ pub fn init(context: &PyContext) {
The argument must be an iterable if specified.";

extend_class!(context, list_type, {
"__sizeof__" => context.new_rustfunc(PyListRef::sizeof),
"__add__" => context.new_rustfunc(PyListRef::add),
"__iadd__" => context.new_rustfunc(PyListRef::iadd),
"__bool__" => context.new_rustfunc(PyListRef::bool),
Expand Down
14 changes: 14 additions & 0 deletions vm/src/obj/objset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ impl PySetInner {
self.content.len()
}

fn sizeof(&self) -> usize {
self.content.sizeof()
}

fn copy(&self) -> PySetInner {
PySetInner {
content: self.content.clone(),
Expand Down Expand Up @@ -343,6 +347,11 @@ impl PySet {
self.inner.borrow().len()
}

#[pymethod(name = "__sizeof__")]
fn sizeof(&self, _vm: &VirtualMachine) -> usize {
std::mem::size_of::<Self>() + self.inner.borrow().sizeof()
}

#[pymethod]
fn copy(&self, _vm: &VirtualMachine) -> Self {
Self {
Expand Down Expand Up @@ -594,6 +603,11 @@ impl PyFrozenSet {
self.inner.len()
}

#[pymethod(name = "__sizeof__")]
fn sizeof(&self, _vm: &VirtualMachine) -> usize {
std::mem::size_of::<Self>() + self.inner.sizeof()
}

#[pymethod]
fn copy(&self, _vm: &VirtualMachine) -> Self {
Self {
Expand Down
6 changes: 6 additions & 0 deletions vm/src/obj/objstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate unicode_xid;
use std::cell::Cell;
use std::char;
use std::fmt;
use std::mem::size_of;
use std::ops::Range;
use std::str::FromStr;
use std::string::ToString;
Expand Down Expand Up @@ -285,6 +286,11 @@ impl PyString {
self.value.chars().count()
}

#[pymethod(name = "__sizeof__")]
fn sizeof(&self, _vm: &VirtualMachine) -> usize {
size_of::<Self>() + self.value.capacity() * size_of::<u8>()
}

#[pymethod(name = "__mul__")]
fn mul(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
if !objtype::isinstance(&val, &vm.ctx.int_type()) {
Expand Down