Skip to content

BitSet shift left - skip zero words #50

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

Merged
merged 2 commits into from
Sep 26, 2019

Conversation

linasm
Copy link
Contributor

@linasm linasm commented Sep 16, 2019

I've realized that Scala BitSets might have (and usually they do have) some zero words above the highest set bit, which I guess contradicts the statement in the documentation:

The memory footprint of a bitset is determined by the largest number stored in it.

My initial implementation of << kept those zero words and pushed them to the left. As a result, there was an edge case with large BitSets, where those zero words would not fit in the size limit.

For example, BitSet(Int.MaxValue - 64) << 64 would throw an IllegalArgumentException while it should result in BitSet(Int.MaxValue).

I am submitting a fix which addresses the issue by skipping any zero words existing above the highest set bit. In many cases, this also reduces the amount of memory allocated to the resulting BitSet.

@julienrf

@julienrf julienrf self-assigned this Sep 23, 2019
var significantWordCount = bs.nwords
while (significantWordCount > 0 && bs.word(significantWordCount - 1) == 0) {
significantWordCount -= 1
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to compute the amount to subtract from significantWordCount in O(1) time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really doubt this. AFAIK BitSet implementation does not keep track of its most significant word/bit. Or maybe you have some idea?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, I haven’t thought about how to do this. This was just a question that popped up as I was quickly reading the diff.

@julienrf julienrf merged commit 7c01a17 into scala:master Sep 26, 2019
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