-
Notifications
You must be signed in to change notification settings - Fork 1.3k
int.__sizeof__
from #1426
#1541
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
Conversation
vm/src/obj/objint.rs
Outdated
@@ -186,6 +186,11 @@ impl PyInt { | |||
PyInt::new(options.get_int_value(vm)?).into_ref_with_type(vm, cls) | |||
} | |||
|
|||
#[pymethod(name = "__sizeof__")] | |||
fn sizeof(&self, _vm: &VirtualMachine) -> usize { | |||
(self.value.bits() as f64 / 8.).ceil() as usize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results in:
>>>>> (2**1).__sizeof__()
1
>>>>> (2**8).__sizeof__()
2
>>>>> (2**16).__sizeof__()
3
>>>>> (2**32).__sizeof__()
5
>>>>> (2**64).__sizeof__()
9
Edit: tested better values @coolreader18
vm/src/obj/objint.rs
Outdated
@@ -188,7 +189,7 @@ impl PyInt { | |||
|
|||
#[pymethod(name = "__sizeof__")] | |||
fn sizeof(&self, _vm: &VirtualMachine) -> usize { | |||
(self.value.bits() as f64 / 8.).ceil() as usize | |||
mem::size_of::<Self>() as usize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results in:
>>>>> for i in range(0, 100, 10):
..... print((i).__sizeof__())
.....
32
32
32
.
.
.
32
32
mem::size_of::<Self>() as usize This'll just always return 32 I believe, I'm not sure how useful this is. You're right that it goes deeper than I thought 😅 |
Yeah, it's not as simple as |
Although I don't think there was, it just didn't go above 1 byte because 100 can be contained with just 8 binary digits; you'd have to go above 256 to see a change. |
vm/src/obj/objint.rs
Outdated
@@ -189,7 +188,7 @@ impl PyInt { | |||
|
|||
#[pymethod(name = "__sizeof__")] | |||
fn sizeof(&self, _vm: &VirtualMachine) -> usize { | |||
mem::size_of::<Self>() as usize | |||
self.value.bits() + 7 & !7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>>>> (2**1).__sizeof__()
8
>>>>> (2**8).__sizeof__()
16
>>>>> (2**16).__sizeof__()
24
>>>>> (2**32).__sizeof__()
40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coolreader18 I think you're right about just not going high enough, I'll test that out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sizeof int is sum of size of int object itself + sum of its heap allocated memory.
So, roughly, mem::sizeof<T> + 4 * self.value.data.len()
is expected.
If current code describes the latter, adding mem::sizeof
will make the value correct
vm/src/obj/objint.rs
Outdated
@@ -188,7 +189,7 @@ impl PyInt { | |||
|
|||
#[pymethod(name = "__sizeof__")] | |||
fn sizeof(&self, _vm: &VirtualMachine) -> usize { | |||
self.value.bits() + 7 & !7 | |||
mem::size_of::<Self>() + (self.value.bits() + 7) & !7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be 32 bits on the stack + bits on the heap rounded up to the nearest 8 bits.
>>>>> (2**2).__sizeof__()
40
>>>>> (2**8).__sizeof__()
48
>>>>> (2**16).__sizeof__()
56
>>>>> (2**32).__sizeof__()
72
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__sizeof__
should actually return bytes, so you'd probably want to divide the bits you get by 8
5015024
to
8fff494
Compare
vm/src/obj/objint.rs
Outdated
@@ -576,7 +576,7 @@ impl PyInt { | |||
|
|||
#[pymethod(name = "__sizeof__")] | |||
fn sizeof(&self, _vm: &VirtualMachine) -> usize { | |||
size_of::<Self>() | |||
(size_of::<Self>() + (self.value.bits() + 7) & !7) / 8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Divided by 8 to return bytes instead of bits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mem::size_of
actually does return bytes, it's the BigInt::bits
method that returns bits.
Co-Authored-By: Noah <33094578+coolreader18@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for working on this!
Impl from @RobertBerglund 's comment, working on #304