-
Notifications
You must be signed in to change notification settings - Fork 39
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
Correctness::compute branchless optimization #10
Comments
Nice! You should try cloning the code and modifying |
I wrote this test and it works:
I cloned the repo and replaced the
|
Neat! Care to submit a PR? That should make it a little easier to review this change and potentially merge it! |
I was going to do so, but i found out that my implementation diverges from yours for some cases. For example: |
Hmm, that suggests there's a bug in your implementation, since the computation of a correctness mask should be deterministic, so any optimization should still yield exactly the same result. |
Yeah, I tried to solve this problem, but any changes lead to a different case diverging from your implementation. |
The underlying problem is as follows: For some combinations of word and guess, there is more than one correct mask. Namely, if a misplaced character occurs at two locations in the guess, but only one location in the word. Either of the two locations can be marked as misplaced while the other one is marked as wrong. You get the exact same information. Now the implemented algorithm requires masks to be normalized. This is a bit more than computation of masks just being deterministic. Deterministic just says for every combination of word and guess, you always have to produce the same mask. This holds true for both algorithms the one in the repo and the proposed one. But the proposed algorithm has a problem. If you have two words w1 and w2 that both have the character that occurs in two locations in the guess, then it is possible that for w1 the mask computation marks the first location as misplaced and for w2 the second. And this is a big problem for the algorithm that relies on both masks being identical in order to determine that w1 and w2 are compatible with the guess and the mask. This comes from the optimization that reuses the mask computation to check whether a word is compatible with guess and mask. The mask computation in the repo satisfies this needs, as it always marks the leftmost location in the guess as misplaced, whenever there is a choice. But the fancy rotation and bit-operation stuff in the branchless version has the problem, that the location that is being marked as misplaced depends on the position of the misplaced character in the word. |
I was thinking about a possible branchless implementation for the function
Correctness::compute
and I wrote this:I tested it in release with this:
And it seems to be about 33 times faster on my PC:
The text was updated successfully, but these errors were encountered: