Skip to content

Commit dc9ca0a

Browse files
committed
[nfc] speed-up enabled-Features lookups
With the growing popularity of using Features, I think it's important to ensure our Features querying set is as fast as possible. In particular, we sometimes may end up putting a feature check in a hot function, so the `contains` overhead is important. While I haven't done *any* measurements to verify this, I think a `llvm::SmallSet<Feature, 2>` is slower than a FixedBitSet. For the former, if the set grows more than the constant size, it switches over to using a `std::set`. Setting the size to `numFeatures()` is not particularly attractive or possible, because `SmallSet` is limited to a size of 32 to be considered small, as it uses linear search over a SmallVector to service a `contains` query. Meanwhile, a FixedBitSet has a small constant factor overhead for querying if the element is contained: two divisions by a constant power-of-two, a bit shift, and a memory-read / compare. I think that'll beat a SmallSet in all cases.
1 parent 6fe00b6 commit dc9ca0a

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_BASIC_LANGOPTIONS_H
2020

2121
#include "swift/Basic/Feature.h"
22+
#include "swift/Basic/FixedBitSet.h"
2223
#include "swift/Basic/FunctionBodySkipping.h"
2324
#include "swift/Basic/LLVM.h"
2425
#include "swift/Basic/Version.h"
@@ -28,7 +29,6 @@
2829
#include "llvm/ADT/Hashing.h"
2930
#include "llvm/ADT/None.h"
3031
#include "llvm/ADT/Optional.h"
31-
#include "llvm/ADT/SmallSet.h"
3232
#include "llvm/ADT/SmallString.h"
3333
#include "llvm/ADT/SmallVector.h"
3434
#include "llvm/ADT/StringRef.h"
@@ -426,7 +426,7 @@ namespace swift {
426426
bool EnableNewOperatorLookup = false;
427427

428428
/// The set of features that have been enabled.
429-
llvm::SmallSet<Feature, 2> Features;
429+
FixedBitSet<numFeatures(), Feature> Features;
430430

431431
/// Temporary flag to support LLDB's transition to using \c Features.
432432
bool EnableBareSlashRegexLiterals = false;

0 commit comments

Comments
 (0)