Repository navigation
int.__sizeof__ from #1426 - #1541
Conversation
|
|
||
| #[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.
Results in:
>>>>> (2**1).__sizeof__()
1
>>>>> (2**8).__sizeof__()
2
>>>>> (2**16).__sizeof__()
3
>>>>> (2**32).__sizeof__()
5
>>>>> (2**64).__sizeof__()
9Edit: tested better values @coolreader18
| #[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.
Results in:
>>>>> for i in range(0, 100, 10):
..... print((i).__sizeof__())
.....
32
32
32
.
.
.
32
32
commented
Oct 15, 2019
mem::size_of::<Self>() as usizeThis'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 😅 |
commented
Oct 15, 2019
|
Yeah, it's not as simple as |
commented
Oct 15, 2019
|
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. |
| #[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.
>>>>> (2**1).__sizeof__()
8
>>>>> (2**8).__sizeof__()
16
>>>>> (2**16).__sizeof__()
24
>>>>> (2**32).__sizeof__()
40There was a problem hiding this comment.
@coolreader18 I think you're right about just not going high enough, I'll test that out
There was a problem hiding this comment.
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
| #[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.
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__()
72There was a problem hiding this comment.
__sizeof__ should actually return bytes, so you'd probably want to divide the bits you get by 8
5015024 to
8fff494
Compare
| #[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.
Divided by 8 to return bytes instead of bits.
There was a problem hiding this comment.
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>
Impl from @RobertBerglund 's comment, working on #304