Skip to content

Commit 71760bc

Browse files
committed
Replace StringMap with DenseMap when the keys don't need to be owned
StringMap always copies its strings into its own storage. A DenseMap of StringRefs has the same caveats as any other use of StringRef, but in the cases I've changed the string has very clear ownership that outlives the map. No functionality change, but should reduce memory usage and malloc traffic a little.
1 parent 245b06b commit 71760bc

File tree

9 files changed

+14
-17
lines changed

9 files changed

+14
-17
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 {

lib/ClangImporter/ClangImporter.cpp

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

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

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
@@ -586,7 +586,7 @@ static bool populateOutOfDateMap(InputInfoMap &map,
586586
// If a file was removed, we've lost its dependency info. Rebuild everything.
587587
// FIXME: Can we do better?
588588
if (ShowIncrementalBuildDecisions) {
589-
llvm::StringSet<> inputArgs;
589+
llvm::DenseSet<StringRef> inputArgs;
590590
for (auto &inputPair : inputs) {
591591
inputArgs.insert(inputPair.second->getValue());
592592
}
@@ -1181,7 +1181,7 @@ static bool checkInputExistence(const Driver &D, const DerivedArgList &Args,
11811181
void Driver::buildInputs(const ToolChain &TC,
11821182
const DerivedArgList &Args,
11831183
InputFileList &Inputs) const {
1184-
llvm::StringMap<StringRef> SourceFileNames;
1184+
llvm::DenseMap<StringRef, StringRef> SourceFileNames;
11851185

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

lib/Driver/ToolChain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static void
290290
sortJobsToMatchCompilationInputs(ArrayRef<const Job *> unsortedJobs,
291291
SmallVectorImpl<const Job *> &sortedJobs,
292292
Compilation &C) {
293-
llvm::StringMap<const Job *> jobsByInput;
293+
llvm::DenseMap<StringRef, const Job *> jobsByInput;
294294
for (const Job *J : unsortedJobs) {
295295
const CompileJobAction *CJA = cast<CompileJobAction>(&J->getSource());
296296
const InputAction *IA = findSingleSwiftInput(CJA);

lib/Driver/ToolChains.cpp

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

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

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;

0 commit comments

Comments
 (0)