- 
                Notifications
    You must be signed in to change notification settings 
- Fork 847
Faster hilbert.Encode and hilbert.Decode #234
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
base: master
Are you sure you want to change the base?
Conversation
| } | ||
|  | ||
| func rotate(n, rx, ry int32, x, y *int32) { | ||
| func rotate(n, rx, ry int32, x, y int32) (int32, int32) { | 
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.
🌵
| func rotate(n, rx, ry int32, x, y int32) (int32, int32) { | |
| func rotate(n, rx, ry, x, y int32) (int32, int32) { | 
| x += int32(s * rx) | ||
| y += int32(s * ry) | 
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.
🔮 I don't know definitively, but aren't rx and ry only ever zero or one? Aren't they essentially bools? Isn't it always true that rx != ry? I'm not sure. But if so, would a
if rx {
	x += int32(s)
} else {
	y += int32(s)
}be faster than a multiplication?
Or maybe they're not mutually exclusive: is a bool check still faster than a mult?
if rx {
	x += int32(s)
}
if ry {
	y += int32(s)
}| ry = boolToInt(y&s > 0) | ||
| d += int64(int64(s) * int64(s) * int64(((3 * rx) ^ ry))) | ||
| rotate(s, rx, ry, &x, &y) | ||
| d += int64(int64(s) * int64(s) * int64(rx<<1|(rx^ry))) | 
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.
📖 There's a lot magic bitwise stuff happening in this file (there was before!). Especially since this is OSS, perhaps we could add comments to the code describing the intent of what's happening (and why we're doing it the obscure way, not the clear way).
|  | ||
| for s := int64(1); s < int64(n); s *= 2 { | ||
| rx = 1 & (t / 2) | ||
| for s := int64(1); s < int64(n); s <<= 1 { | 
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.
🌵 I wonder what the impact is to cast n to int64 on every iteration. Could we just have a sister const at the top of the file?
const (
	n = int32(1 << 31)
	n_64 = int64(n)
)
Description
Encode/Decode can be improved.
This PR makes the following changes:
Results: