Skip to content

[NFC] Add FixedBitSet #41398

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 1 commit into from
Feb 17, 2022
Merged

[NFC] Add FixedBitSet #41398

merged 1 commit into from
Feb 17, 2022

Conversation

rjmccall
Copy link
Contributor

I wanted a bit vector that I could use as a compact sorted set of enum values: an inline-allocated, fixed-size array of bits supporting efficient and convenient set operations and iteration.

The C++ standard library offers std::bitset, but the API is far from ideal for this purpose. It's positioned as an abstract bit-vector rather than as a set. To use it as a set, you have to turn your values into indices, which for enums means explicitly casting them all in the caller. There's also no iteration operation, so to find the elements of the set, you have to iterate over all possible indices, test whether they're in the set, and (if so) cast the current index back to the enum. Not only is that much more awkward than normal iteration, but it's also substantially less efficient than what you can get by counting trailing zeroes in a mask.

LLVM and Swift offer a number of other bit vectors, but they're all dynamically allocated because they're meant to track arbitrary sets. That's not a non-starter for my use case, which is in textual serialization and so rather slow anyway, but it's also not very hard to whip together the optimal data structure here.

I have committed the cardinal sin of C++ data structure design and provided the operations as ordinary methods instead of operators.

@rjmccall
Copy link
Contributor Author

@swift-ci Please smoke test and merge

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

Wow. That's really nice. And does distance with only bit masks (just for fun I guess). Hopefully it'll move to LLVM.

I wanted a bit vector that I could use as a compact sorted
set of enum values: an inline-allocated, fixed-size array
of bits supporting efficient and convenient set operations
and iteration.

The C++ standard library offers std::bitset, but the API
is far from ideal for this purpose.  It's positioned as
an abstract bit-vector rather than as a set.  To use it
as a set, you have to turn your values into indices, which
for enums means explicitly casting them all in the caller.
There's also no iteration operation, so to find the
elements of the set, you have to iterate over all possible
indices, test whether they're in the set, and (if so)
cast the current index back to the enum.  Not only is that
much more awkward than normal iteration, but it's also
substantially less efficient than what you can get by
counting trailing zeroes in a mask.

LLVM and Swift offer a number of other bit vectors, but
they're all dynamically allocated because they're meant
to track arbitrary sets.  That's not a non-starter for my
use case, which is in textual serialization and so rather
slow anyway, but it's also not very hard to whip together
the optimal data structure here.

I have committed the cardinal sin of C++ data structure
design and provided the operations as ordinary methods
instead of operators.
@rjmccall
Copy link
Contributor Author

@swift-ci Please smoke test and merge

@rjmccall
Copy link
Contributor Author

@swift-ci Please smoke test macOS

@rjmccall
Copy link
Contributor Author

@swift-ci Please smoke test Linux

@rjmccall rjmccall merged commit bdaf537 into swiftlang:main Feb 17, 2022
@rjmccall rjmccall deleted the fixed-bit-set branch February 17, 2022 02:37
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