Skip to content

Commit ac4a92a

Browse files
authored
Merge pull request #19292 from jrose-apple/get-out-the-map
Minor improvements to the use of StringMap/StringSet
2 parents 665db87 + 71760bc commit ac4a92a

File tree

13 files changed

+27
-57
lines changed

13 files changed

+27
-57
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ class SourceManager {
3131
unsigned CodeCompletionOffset;
3232

3333
/// Associates buffer identifiers to buffer IDs.
34-
llvm::StringMap<unsigned> BufIdentIDMap;
34+
llvm::DenseMap<StringRef, unsigned> BufIdentIDMap;
3535

3636
/// A cache mapping buffer identifiers to vfs Status entries.
3737
///
3838
/// This is as much a hack to prolong the lifetime of status objects as it is
3939
/// to speed up stats.
40-
mutable llvm::StringMap<clang::vfs::Status> StatusCache;
40+
mutable llvm::DenseMap<StringRef, clang::vfs::Status> StatusCache;
4141

4242
// \c #sourceLocation directive handling.
4343
struct VirtualFile {

include/swift/Driver/DependencyGraph.h

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -143,33 +143,6 @@ class DependencyGraphImpl {
143143

144144
LoadResult loadFromBuffer(const void *node, llvm::MemoryBuffer &buffer);
145145

146-
// FIXME: We should be able to use llvm::mapped_iterator for this, but
147-
// StringMapConstIterator isn't quite an InputIterator (no ->).
148-
class StringSetIterator {
149-
llvm::StringSet<>::const_iterator I;
150-
public:
151-
using value_type = llvm::StringSet<>::const_iterator::value_type;
152-
using iterator_category = std::input_iterator_tag;
153-
using difference_type = ptrdiff_t;
154-
using reference = value_type &;
155-
using pointer = value_type *;
156-
157-
/*implicit*/ StringSetIterator(llvm::StringSet<>::const_iterator base)
158-
: I(base) {}
159-
160-
StringSetIterator &operator++() {
161-
++I;
162-
return *this;
163-
}
164-
165-
StringRef operator*() const {
166-
return I->getKey();
167-
}
168-
169-
bool operator==(StringSetIterator other) const { return I == other.I; }
170-
bool operator!=(StringSetIterator other) const { return I != other.I; }
171-
};
172-
173146
protected:
174147
LoadResult loadFromString(const void *node, StringRef data);
175148
LoadResult loadFromPath(const void *node, StringRef path);
@@ -195,9 +168,8 @@ class DependencyGraphImpl {
195168
}
196169

197170
public:
198-
llvm::iterator_range<StringSetIterator> getExternalDependencies() const {
199-
return llvm::make_range(StringSetIterator(ExternalDependencies.begin()),
200-
StringSetIterator(ExternalDependencies.end()));
171+
decltype(ExternalDependencies.keys()) getExternalDependencies() const {
172+
return ExternalDependencies.keys();
201173
}
202174
};
203175

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ struct ASTContext::Implementation {
124124
/// lazy parsers for imported module files and source files.
125125
llvm::SmallPtrSet<LazyMemberParser*, 2> lazyParsers;
126126

127+
// FIXME: This is a StringMap rather than a StringSet because StringSet
128+
// doesn't allow passing in a pre-existing allocator.
127129
llvm::StringMap<char, llvm::BumpPtrAllocator&> IdentifierTable;
128130

129131
/// The declaration of Swift.AssignmentPrecedence.

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,7 +3289,7 @@ bool ClangImporter::Implementation::forEachLookupTable(
32893289
// Collect and sort the set of module names.
32903290
SmallVector<StringRef, 4> moduleNames;
32913291
for (const auto &entry : LookupTables) {
3292-
moduleNames.push_back(entry.first());
3292+
moduleNames.push_back(entry.first);
32933293
}
32943294
llvm::array_pod_sort(moduleNames.begin(), moduleNames.end());
32953295

@@ -3597,7 +3597,7 @@ void ClangImporter::Implementation::dumpSwiftLookupTables() {
35973597
// Sort the module names so we can print in a deterministic order.
35983598
SmallVector<StringRef, 4> moduleNames;
35993599
for (const auto &lookupTable : LookupTables) {
3600-
moduleNames.push_back(lookupTable.first());
3600+
moduleNames.push_back(lookupTable.first);
36013601
}
36023602
array_pod_sort(moduleNames.begin(), moduleNames.end());
36033603

lib/ClangImporter/ImporterImpl.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ struct PlatformAvailability {
279279
};
280280
}
281281

282-
using LookupTableMap = llvm::StringMap<std::unique_ptr<SwiftLookupTable>>;
282+
using LookupTableMap =
283+
llvm::DenseMap<StringRef, std::unique_ptr<SwiftLookupTable>>;
283284

284285
/// The result of importing a clang type. It holds both the Swift Type
285286
/// as well as a bool in which 'true' indicates either:
@@ -489,10 +490,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
489490
return getNameImporter().getEnumKind(decl);
490491
}
491492

492-
// TODO: drop this accessor as soon as we further de-couple the swift name
493-
// lookup tables from the Impl.
494-
LookupTableMap &getLookupTables() { return LookupTables; }
495-
496493
private:
497494
/// A mapping from imported declarations to their "alternate" declarations,
498495
/// for cases where a single Clang declaration is imported to two

lib/Driver/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ static bool populateOutOfDateMap(InputInfoMap &map,
591591
// If a file was removed, we've lost its dependency info. Rebuild everything.
592592
// FIXME: Can we do better?
593593
if (ShowIncrementalBuildDecisions) {
594-
llvm::StringSet<> inputArgs;
594+
llvm::DenseSet<StringRef> inputArgs;
595595
for (auto &inputPair : inputs) {
596596
inputArgs.insert(inputPair.second->getValue());
597597
}
@@ -1186,7 +1186,7 @@ static bool checkInputExistence(const Driver &D, const DerivedArgList &Args,
11861186
void Driver::buildInputs(const ToolChain &TC,
11871187
const DerivedArgList &Args,
11881188
InputFileList &Inputs) const {
1189-
llvm::StringMap<StringRef> SourceFileNames;
1189+
llvm::DenseMap<StringRef, StringRef> SourceFileNames;
11901190

11911191
for (Arg *A : Args) {
11921192
if (A->getOption().getKind() == Option::InputClass) {

lib/Driver/ToolChain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static void
296296
sortJobsToMatchCompilationInputs(ArrayRef<const Job *> unsortedJobs,
297297
SmallVectorImpl<const Job *> &sortedJobs,
298298
Compilation &C) {
299-
llvm::StringMap<const Job *> jobsByInput;
299+
llvm::DenseMap<StringRef, const Job *> jobsByInput;
300300
for (const Job *J : unsortedJobs) {
301301
const CompileJobAction *CJA = cast<CompileJobAction>(&J->getSource());
302302
const InputAction *IA = findSingleSwiftInput(CJA);

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ void ToolChain::JobContext::addFrontendCommandLineInputArguments(
504504
const bool mayHavePrimaryInputs, const bool useFileList,
505505
const bool usePrimaryFileList, const bool filterByType,
506506
ArgStringList &arguments) const {
507-
llvm::StringSet<> primaries;
507+
llvm::DenseSet<StringRef> primaries;
508508

509509
if (mayHavePrimaryInputs) {
510510
for (const Action *A : InputActions) {

lib/IDE/Refactoring.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,17 @@ class RenameRangeDetailCollector : public Renamer {
387387
};
388388

389389
class TextReplacementsRenamer : public Renamer {
390-
llvm::StringMap<char> &ReplaceTextContext;
390+
llvm::StringSet<> &ReplaceTextContext;
391391
std::vector<Replacement> Replacements;
392392

393393
public:
394394
const DeclNameViewer New;
395395

396396
private:
397397
StringRef registerText(StringRef Text) {
398-
return ReplaceTextContext.insert({Text, char()}).first->getKey();
398+
if (Text.empty())
399+
return Text;
400+
return ReplaceTextContext.insert(Text).first->getKey();
399401
}
400402

401403
StringRef getCallArgLabelReplacement(StringRef OldLabelRange,
@@ -497,7 +499,7 @@ class TextReplacementsRenamer : public Renamer {
497499
public:
498500
TextReplacementsRenamer(const SourceManager &SM, StringRef OldName,
499501
StringRef NewName,
500-
llvm::StringMap<char> &ReplaceTextContext)
502+
llvm::StringSet<> &ReplaceTextContext)
501503
: Renamer(SM, OldName), ReplaceTextContext(ReplaceTextContext),
502504
New(NewName) {
503505
assert(Old.isValid() && New.isValid());
@@ -3295,7 +3297,7 @@ int swift::ide::syntacticRename(SourceFile *SF, ArrayRef<RenameLoc> RenameLocs,
32953297
return true; // Already diagnosed.
32963298

32973299
size_t index = 0;
3298-
llvm::StringMap<char> ReplaceTextContext;
3300+
llvm::StringSet<> ReplaceTextContext;
32993301
for(const RenameLoc &Rename: RenameLocs) {
33003302
ResolvedLoc &Resolved = ResolvedLocs[index++];
33013303
TextReplacementsRenamer Renamer(SM, Rename.OldName, Rename.NewName,

lib/IRGen/IRGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ static void performParallelIRGeneration(
10101010
PrimaryGM->addLinkLibrary(linkLib);
10111011
});
10121012

1013-
llvm::StringSet<> referencedGlobals;
1013+
llvm::DenseSet<StringRef> referencedGlobals;
10141014

10151015
for (auto it = irgen.begin(); it != irgen.end(); ++it) {
10161016
IRGenModule *IGM = it->second;

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ static bool isAnyObjectOrAny(Type type) {
6565

6666
/// Returns true if \p name matches a keyword in any Clang language mode.
6767
static bool isClangKeyword(Identifier name) {
68-
static const llvm::StringSet<> keywords = []{
69-
llvm::StringSet<> set;
68+
static const llvm::DenseSet<StringRef> keywords = []{
69+
llvm::DenseSet<StringRef> set;
7070
// FIXME: clang::IdentifierInfo /nearly/ has the API we need to do this
7171
// in a more principled way, but not quite.
7272
#define KEYWORD(SPELLING, FLAGS) \

lib/SIL/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5065,7 +5065,7 @@ void SILModule::verify() const {
50655065
return;
50665066
#endif
50675067
// Uniquing set to catch symbol name collisions.
5068-
llvm::StringSet<> symbolNames;
5068+
llvm::DenseSet<StringRef> symbolNames;
50695069

50705070
// When merging partial modules, we only link functions from the current
50715071
// module, without enabling "LinkAll" mode or running the SILLinker pass;

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/Basic/SourceManager.h"
2121
#include "swift/Basic/Version.h"
2222
#include "llvm/ADT/SmallString.h"
23+
#include "llvm/ADT/StringSet.h"
2324
#include "llvm/Support/FileSystem.h"
2425
#include "llvm/Support/MemoryBuffer.h"
2526
#include "llvm/Support/Path.h"
@@ -376,19 +377,15 @@ FileUnit *SerializedModuleLoader::loadAST(
376377
// Figure out /which/ dependencies are missing.
377378
// FIXME: Dependencies should be de-duplicated at serialization time,
378379
// not now.
379-
llvm::StringMap<bool> duplicates;
380+
llvm::StringSet<> duplicates;
380381
llvm::SmallVector<ModuleFile::Dependency, 4> missing;
381382
std::copy_if(loadedModuleFile->getDependencies().begin(),
382383
loadedModuleFile->getDependencies().end(),
383384
std::back_inserter(missing),
384385
[&duplicates](const ModuleFile::Dependency &dependency)->bool {
385386
if (dependency.isLoaded() || dependency.isHeader())
386387
return false;
387-
bool &seen = duplicates[dependency.RawPath];
388-
if (seen)
389-
return false;
390-
seen = true;
391-
return true;
388+
return duplicates.insert(dependency.RawPath).second;
392389
});
393390

394391
// FIXME: only show module part of RawAccessPath

0 commit comments

Comments
 (0)