-
Notifications
You must be signed in to change notification settings - Fork 123
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
Faster integer formatting to arbitrary base #2360
Comments
Rust uses the ryu crate to print floating point numbers to strings quickly but I'm not sure if there is an equivalent for numbers. perhaps @cmpute could advise? |
There are several ways to support fast printing in
Not sure which approach is best suitable for Scryer Prolog. Let me know as well if you want different APIs :) |
Thank you a lot @cmpute! I have published a draft (#2362) that makes the It may be interesting to have this functionality, for very large integers, if the need arises. I will first try to improve |
Oh my bad, there was a typo, |
@cmpute: Thank you a lot for the clarification! In my tests, the test case above can be made to run about 4 times faster by using the native I wonder whether adding a more general predicate to Scryer that also covers the functionality of the The "arbitrary base digits" API mentioned above seems interesting, and may indeed be worth exposing as an interface predicate when it becomes available, especially if it satisfies these desiderata. |
I will link this issue to the commit when the digits conversion functionality is added. However, it worth noting that this API won't get as much optimization as the small base conversions (at least not in the near future), i.e. the speed improvement can be smaller. |
In one of my cryptographic applications of Scryer Prolog, the bottleneck turns out to be formatting large integers in base 16, using
library(format)
.We can use the
~Nr
format control sequence to format an integer to base (radix) N, such as for example:With larger integers, this is currently somewhat slow. For example, with the following definitions:
We have:
The formatting code is written in Prolog and should preferably remain in Prolog, because if every tiny detail is hardcoded in Rust itself, then there will never be enough incentive to make actual Prolog code faster:
scryer-prolog/src/lib/format.pl
Line 372 in 7b33eaf
The question I have is therefore: What are the most fundamental, smallest building blocks that would help to make this Prolog code faster? Only that functionality should be implemented in Rust, if at all.
For example, currently, both
I0 mod R
andI0 // R
are computed in the code:scryer-prolog/src/lib/format.pl
Line 392 in 7b33eaf
I see that the dashu crate exposes functionality to compute both results in a single operation:
https://docs.rs/dashu-int/latest/dashu_int/struct.IBig.html#impl-DivRem-for-IBig
Would such a predicate be a sensible addition to
library(arithmetic)
, to obtain both the quotient and the remainder of two integers with better efficiency than is currently possible?Any other ideas? I would greatly appreciate all suggestions and help with this issue. Thank you a lot!
The text was updated successfully, but these errors were encountered: