KEMBAR78
[mypyc] Replace integer floor division by a power of two with a shift by JukkaL · Pull Request #12870 · python/mypy · GitHub
Skip to content

Conversation

@JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented May 25, 2022

In a microbenchmark right shift was a bit faster.

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A classic

if not isinstance(expr.right, IntExpr):
return expr
divisor = expr.right.value
n = 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't super important, but I think it would be cleaner (and faster, though that doesn't really matter) to directly compute the shift instead of testing for it? I think you can do shift = divisor.bit_length() - 1 and then check that the shift is correct

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. We don't have a primitive for bit_length() so it may actually be slower, but I think that the code looks cleaner if we use it.

@JukkaL JukkaL merged commit d3ef642 into master May 27, 2022
@JukkaL JukkaL deleted the div-pow2 branch May 27, 2022 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants