-
Notifications
You must be signed in to change notification settings - Fork 14
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
Inline double word integers #13
Comments
I think I'd rather have this done more generally, with up to N words inlined (where N is a constant, perhaps different on different platforms), rather than with a special case for 2. The benefit wouldn't really be that there are better algorithms for 2 words (current representation doesn't prevent using those), but rather avoiding heap allocations. |
But I think the limit of inlined words (without increasing the size of UBig) will be 3 regardless of platform right? Correct me if I'm wrong. If arbitrary size is desired, then a very simple way is to replace |
Usually, not always.
Yes. That would be good I think. Also then there is no real need for the Large / Small distinction, because that's redundant with the However I have tried this a while ago and saw significant performance deterioration for some reason. I may have badly interacted with some optimizations. There is probably a good way to do this. |
I guess it might be better to implement a customized barebone use smallvec::SmallVec;
enum UBig {
Small(usize),
Large(Vec<usize>)
}
struct UBig2(SmallVec<[usize; 2]>); // it should be able to inline [usize; 3]
fn main() {
dbg!(std::mem::size_of::<UBig>());
dbg!(std::mem::size_of::<UBig2>());
} And one performance problem for inlining integers with larger size is that, you have to keep track of the position of highest non-zero word. The overhead of this can be large for basic arithmetic operations. This is also why I think double word is a sweet spot for inlining. 3-word is also feasible if we have inlined double word since we don't have to track the zero state of the highest word, but it will lead to explosion of number of matching arms. |
Besides, a quick and easy improvement in terms of memory usage is to let |
The same would be true for |
hmm, that's correct |
I actually just looked round for this, GMP says in their documentation:
And in their header files, they have the option macro
So I can confidently say that for GMP, a |
On x32 architecture ( |
Ok I thought you were referring to some cases where the processor doesn't provide enough arithmetic support for native bit size. But in this case, it makes more sense to me to use 32bit word and inline 64bit double word, which still utilize the full CPU capability. |
Definitely not -- |
Closing. Inlining a few words on the stack might be beneficial, but specializing for |
As mentioned in #6 (comment), inline integers larger than a word are feasible without increasing the size of UBig. Specifically, at most 3 words can be inlined in the struct. However, it doesn't benefit much to inline a 3 word (most algorithms don't have specialization for 3-word integers). However, inlining a double word integer is pretty easy and beneficial. I'm thinking of creating a PR to add double word into the UBig struct, resulting in something like:
How's your opinion on this please? 😃
The text was updated successfully, but these errors were encountered: