-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[WIP] Add int __sizeof__ implementation #1172
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
Do we have to account for unused extra capacity in the underlying |
I'm not really sure where to look for the official spec, but from the python3 docs it sounds like it's supposed to return the amount of memory in bytes that the object is holding. |
Are you aware that we implemented Maybe you could look at the Maybe the |
A good test would be to check what cpython reports as a size on various ineteger value. Keep in mind however, that this is probably implementation specific, and that we probably cannot make testcases for it in the tests/snippets directory. |
I don't think The cpython implementation in offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit); And in practice that translates to: >>> (0).__sizeof__()
24
>>> (1).__sizeof__()
28
>>> (1000000000000000).__sizeof__()
32
>>> (10000000000000000000000000000000000000).__sizeof__()
44 And my implementation for RustPython: >>>>> (0).__sizeof__()
32
>>>>> (1).__sizeof__()
36
>>>>> (1000000000000000).__sizeof__()
40
>>>>> (10000000000000000000000000000000000000).__sizeof__()
48 I would guess that the cpython ints are 4 bytes smaller because they combine the sign with the number of digits i.e. -4 digits means 4 digits, but the sign of the number is negative. That's why they use I think using |
I think we should search in the direction of modifying |
Well, the reason why you can't write a function for this in general is the same reason why you can't write a hashing function that works for all objects: Some objects have pointers, and how can you tell which pointers are important and which ones aren't? You may be able to come up with something that works most of the time if the language has good support for introspection, which I have no experience with in Rust. Really the only problem here is that we're using the I see that in the code I've written here I could use As an aside. I've been writing a lot of Python, and of course in python there is no public/private access control (the best you can do is use |
Hmm. This is too bad. I suggest to implement the I would really like to prevent that we require a specific sizeof method for each type. Does this sound reasonable to you? |
What is it that you want me to mark with TODO? I have a suspicion that implementing a generic I don't see a lot of point in incorrectly implementing BTW, a half baked idea I had for a workaround for ints was to maybe store the data vec directly in The other foolproof solution would be to write our own implementation of BigInt (or maybe to fork the num repo) and give data members sane access controls. |
Sorry, I accidentally closed-reopened this PR. The reason for me to skip the I do not think forking the bigint crate is a good idea. @coolreader18 and @palaviv do you have any ideas on this? For the moment, I would like to park this method, and implement other modules first. I think we can postpone this method for some time. |
Forking |
Please see also this issue: rust-num/num-bigint#98 |
I propose to hold off this issue until the bigint crate has support for querying memory. It appears that there might be an option to do this in future. |
I filed an issue for this in the rust repo: rust-lang/rust#63073 |
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.
Let's wait with this change a while until we might get a method from the BigInt crate.
@Askaholic do you agree on closing this pull request, and creating an issue for this topic? |
Yea, good idea! |
Changed into issue #1250 |
Leaving this here in hopes that someone will tell me there's actually a really easy way of doing this!
On my system (x86_64 ubuntu 18) I've determined that the compiler lays out the BigInt structs like this:
So I can simply cast the
&BigInt
to a&Vec
and get the capacity from it.TODO: Test snippets