Skip to content

Commit de51f8c

Browse files
committed
Remangler: use the bump pointer allocator instead of std::vector for internal substitution data structures
This avoids mallocs in the runtime. SR-10028 rdar://problem/48575729
1 parent 8c95abb commit de51f8c

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

include/swift/Basic/Mangler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ class Mangler {
7878
}
7979
};
8080

81+
void addSubstWordsInIdent(const WordReplacement &repl) {
82+
SubstWordsInIdent.push_back(repl);
83+
}
84+
85+
void addWord(const SubstitutionWord &word) {
86+
Words.push_back(word);
87+
}
88+
8189
/// Returns the buffer as a StringRef, needed by mangleIdentifier().
8290
StringRef getBufferStr() const {
8391
return StringRef(Storage.data(), Storage.size());

include/swift/Demangling/Demangler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ template<typename T> class Vector {
279279
Capacity = 0;
280280
Elems = 0;
281281
}
282-
282+
283+
void clear() {
284+
NumElems = 0;
285+
}
286+
283287
iterator begin() { return Elems; }
284288
iterator end() { return Elems + NumElems; }
285289

include/swift/Demangling/ManglingUtils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ void mangleIdentifier(Mangler &M, StringRef ident) {
160160
if (WordIdx >= 0) {
161161
// We found a word substitution!
162162
assert(WordIdx < 26);
163-
M.SubstWordsInIdent.push_back({wordStartPos, WordIdx});
163+
M.addSubstWordsInIdent({wordStartPos, WordIdx});
164164
} else if (wordLen >= 2 && M.Words.size() < M.MaxNumWords) {
165165
// It's a new word: remember it.
166166
// Note: at this time the word's start position is relative to the
167167
// begin of the identifier. We must update it afterwards so that it is
168168
// relative to the begin of the whole mangled Buffer.
169-
M.Words.push_back({wordStartPos, wordLen});
169+
M.addWord({wordStartPos, wordLen});
170170
}
171171
wordStartPos = NotInsideWord;
172172
}
@@ -181,7 +181,7 @@ void mangleIdentifier(Mangler &M, StringRef ident) {
181181

182182
size_t Pos = 0;
183183
// Add a dummy-word at the end of the list.
184-
M.SubstWordsInIdent.push_back({ident.size(), -1});
184+
M.addSubstWordsInIdent({ident.size(), -1});
185185

186186
// Mangle a sequence of word substitutions and sub-strings.
187187
for (size_t Idx = 0, End = M.SubstWordsInIdent.size(); Idx < End; ++Idx) {

lib/Demangling/Remangler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ class Remangler {
155155

156156
DemanglerPrinter &Buffer;
157157

158-
std::vector<SubstitutionWord> Words;
159-
std::vector<WordReplacement> SubstWordsInIdent;
158+
Vector<SubstitutionWord> Words;
159+
Vector<WordReplacement> SubstWordsInIdent;
160160

161161
static const size_t MaxNumWords = 26;
162162

@@ -172,6 +172,14 @@ class Remangler {
172172
// A callback for resolving symbolic references.
173173
SymbolicResolver Resolver;
174174

175+
void addSubstWordsInIdent(const WordReplacement &repl) {
176+
SubstWordsInIdent.push_back(repl, Factory);
177+
}
178+
179+
void addWord(const SubstitutionWord &word) {
180+
Words.push_back(word, Factory);
181+
}
182+
175183
StringRef getBufferStr() const { return Buffer.getStringRef(); }
176184

177185
void resetBuffer(size_t toPos) { Buffer.resetSize(toPos); }

0 commit comments

Comments
 (0)