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
Suggested expansion for (/ a b) #357
Comments
Using macros we get a better picture of the expected speedup, and it's much better, about a factor of 40 when the divisor is 2.
which expands to
with timings
|
Hey, Matthew implemented something very similar in Racket's fork of Chez: but it appears that he implemented it in the runtime library, not at expansion time. This expansion will help Gambit a lot more. |
Let's do it! |
Awesome 👏 |
Microbenchmarking is hard. I added a loop to measure the overhead and got for #e1e8 reps
Then I tried this expansion, which turned out to be a bit faster:
with times
with speedups of: for dividing by constant -1
for dividing by constant 2
for dividing by variable that equals 2
So a speedup of nearly 80. |
I think the case for dividing by -1 should not be in the fast path... I expect it to be almost never the case, and it bloats the code. Implementation would be simple if it didn't have to support multi-argument division... |
But I don't think either test
work if b is -1, i.e.
I suspect that most of the time |
I mean that when b is 0 or -1 a call should be made to |
OK, so you're saving maybe a branch and an inlined
|
The Gambit RTS is not a good benchmark for what user programs do. Many programs don't use |
The problem with Multiplying by -1 is quite common, but I can't think of much natural code that divides by -1. |
A better expansion of |
I'm sorry, but I don't see it. With this source file
I get
Where's the expansion? |
Remove the |
On 1/22/20 8:57 AM, Marc Feeley wrote:
Perhaps a better divisibility test could be devised, perhaps based on
https://arxiv.org/abs/1902.01961 .
Yes, that paper was discussed in the gmp mail list last fall. Perhaps
this technique would be better placed in a C compiler.
Brad
|
It works for your example now. On my machine, with two fixnums where the first is a multiple of the second, the improvement is about 5x when the divisor is not known at compile time, and about 50x when the divisor is known at compile time. |
I see a lot of scheme code written in the style
which bothers me, but, hey, I can't change other people's programming style.
So I tried to work out a better inline expansion for (/ x y) and came up with the following suggestions and tests:
with output
So this code makes things run 2.8 to 6 times faster.
Things will go faster when the constant
2
replacesy
in the code source.The text was updated successfully, but these errors were encountered: