Skip to content
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

Bit testing can use unsafeRead because the length was verified #24

Merged
merged 3 commits into from
Mar 27, 2024

Conversation

kquick
Copy link
Collaborator

@kquick kquick commented Mar 24, 2024

No description provided.

@@ -44,6 +44,5 @@ testBit (BS v sz) bitIx
| otherwise = do
let wordIx = bitIx `div` bitsPerWord
bitPos = bitIx `mod` bitsPerWord
w <- V.read v wordIx
w <- V.unsafeRead v wordIx
Copy link
Owner

Choose a reason for hiding this comment

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

I think we would also need to check that bitIx >= 0 for this to be safe-ish. If you pass in a negative index, this could be very bad. Perhaps the bug is that the API takes an index using a signed type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In general, I am saddened that so much of the prelude and containers uses Int instead of Natural. Any index, the length, the initial size, etc. should all be Natural but are all Ints. I've used Natural in other places, but it does add some noise because there's a need to convert between Natural and Int in [lots of places to impedance match with the stuff that is written to use Int.

I think this is actually safe in practice because this is an internal module that is not exported to users, and it's only used from Algorithms.DFS as the latter iterates through graph elements (practically speaking, even the bigIx >= size should be unnecessary based on the usage).

I considered switching this interface to Nat, but it's using the vertexId, so either DFS is calling toEnum on all these so that BitSet can almost immediately call fromEnum to invoke the fromEnum to call the Vector operations, or the Vertex type changes to a Natural (and although it's supposed to be internal, it's visible and a much bigger change, and it still needs the fromEnum to call the Vector operations).

In fact, I switched the other read in this file to unsafe, then I benchmarked, the original safe, the unsafe in this PR, and a unsafe Nat version with the fromEnum/toEnum I discussed above. I'll email the criterion HTML separately. tl;dr Nat+unsafe is 33% slower than unsafe, and safe is 25% slower than unsafe.

Copy link
Owner

Choose a reason for hiding this comment

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

I had forgotten that this was an internal-only module.

In that case, what if we just renamed this to unsafeSetBit and unsafeTestBit and even removed the bounds check?

I was going to say that the only uses do their own checking, but that is not entirely true. If you mix vertices from another graph, you could hit an error (and we don't have any type-level enforcement of that). It looks like it would be safe if we just filtered the root [Vertex] inputs in the DFS module.

@travitch travitch merged commit a0ef5ba into master Mar 27, 2024
20 checks passed
@travitch travitch deleted the dgb_1711253155-0 branch March 27, 2024 04:03
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