Skip to content

Commit ae189ef

Browse files
authored
Merge pull request #23131 from eeckstein/runtime-malloc-removal
Avoid malloc allocations in the runtime
2 parents 83b29b7 + 0dd2495 commit ae189ef

File tree

17 files changed

+322
-116
lines changed

17 files changed

+322
-116
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/Demangle.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,23 +490,23 @@ void mangleIdentifier(const char *data, size_t length,
490490
bool usePunycode = true);
491491

492492
/// Remangle a demangled parse tree.
493-
///
494-
/// If \p BorrowFrom is specified, the initial bump pointer memory is
495-
/// borrowed from the free memory of BorrowFrom.
496-
std::string mangleNode(NodePointer root,
497-
NodeFactory *BorrowFrom = nullptr);
493+
std::string mangleNode(NodePointer root);
498494

499495
using SymbolicResolver =
500496
llvm::function_ref<Demangle::NodePointer (SymbolicReferenceKind,
501497
const void *)>;
502498

499+
/// Remangle a demangled parse tree, using a callback to resolve
500+
/// symbolic references.
501+
std::string mangleNode(NodePointer root, SymbolicResolver resolver);
502+
503503
/// Remangle a demangled parse tree, using a callback to resolve
504504
/// symbolic references.
505505
///
506-
/// If \p BorrowFrom is specified, the initial bump pointer memory is
507-
/// borrowed from the free memory of BorrowFrom.
508-
std::string mangleNode(NodePointer root, SymbolicResolver resolver,
509-
NodeFactory *BorrowFrom = nullptr);
506+
/// The returned string is owned by \p Factory. This means \p Factory must stay
507+
/// alive as long as the returned string is used.
508+
llvm::StringRef mangleNode(NodePointer root, SymbolicResolver resolver,
509+
NodeFactory &Factory);
510510

511511
/// Remangle in the old mangling scheme.
512512
///

include/swift/Demangling/Demangler.h

Lines changed: 13 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

@@ -299,6 +303,11 @@ template<typename T> class Vector {
299303

300304
T &back() { return (*this)[NumElems - 1]; }
301305

306+
void resetSize(size_t toPos) {
307+
assert(toPos <= NumElems);
308+
NumElems = toPos;
309+
}
310+
302311
void push_back(const T &NewElem, NodeFactory &Factory) {
303312
if (NumElems >= Capacity)
304313
Factory.Reallocate(Elems, Capacity, /*Growth*/ 1);
@@ -327,6 +336,9 @@ class CharVector : public Vector<char> {
327336
// Append an integer as readable number.
328337
void append(int Number, NodeFactory &Factory);
329338

339+
// Append an unsigned 64 bit integer as readable number.
340+
void append(unsigned long long Number, NodeFactory &Factory);
341+
330342
StringRef str() const {
331343
return StringRef(Elems, NumElems);
332344
}

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) {

include/swift/Demangling/TypeDecoder.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class TypeDecoder {
315315
if (Node->getNumChildren() < 2)
316316
return BuiltType();
317317

318-
std::vector<BuiltType> args;
318+
SmallVector<BuiltType, 8> args;
319319

320320
const auto &genericArgs = Node->getChild(1);
321321
assert(genericArgs->getKind() == NodeKind::TypeList);
@@ -427,7 +427,7 @@ class TypeDecoder {
427427
return BuiltType();
428428

429429
// Find the protocol list.
430-
std::vector<BuiltProtocolDecl> Protocols;
430+
SmallVector<BuiltProtocolDecl, 8> Protocols;
431431
auto TypeList = Node->getChild(0);
432432
if (TypeList->getKind() == NodeKind::ProtocolList &&
433433
TypeList->getNumChildren() >= 1) {
@@ -514,7 +514,7 @@ class TypeDecoder {
514514
return BuiltType();
515515

516516
bool hasParamFlags = false;
517-
std::vector<FunctionParam<BuiltType>> parameters;
517+
SmallVector<FunctionParam<BuiltType>, 8> parameters;
518518
if (!decodeMangledFunctionInputType(Node->getChild(isThrow ? 1 : 0),
519519
parameters, hasParamFlags))
520520
return BuiltType();
@@ -531,9 +531,9 @@ class TypeDecoder {
531531
}
532532
case NodeKind::ImplFunctionType: {
533533
auto calleeConvention = ImplParameterConvention::Direct_Unowned;
534-
std::vector<ImplFunctionParam<BuiltType>> parameters;
535-
std::vector<ImplFunctionResult<BuiltType>> results;
536-
std::vector<ImplFunctionResult<BuiltType>> errorResults;
534+
SmallVector<ImplFunctionParam<BuiltType>, 8> parameters;
535+
SmallVector<ImplFunctionResult<BuiltType>, 8> results;
536+
SmallVector<ImplFunctionResult<BuiltType>, 8> errorResults;
537537
ImplFunctionTypeFlags flags;
538538

539539
for (unsigned i = 0; i < Node->getNumChildren(); i++) {
@@ -611,7 +611,7 @@ class TypeDecoder {
611611
return decodeMangledType(Node->getChild(0));
612612

613613
case NodeKind::Tuple: {
614-
std::vector<BuiltType> elements;
614+
SmallVector<BuiltType, 8> elements;
615615
std::string labels;
616616
bool variadic = false;
617617
for (auto &element : *Node) {
@@ -785,7 +785,7 @@ class TypeDecoder {
785785
private:
786786
template <typename T>
787787
bool decodeImplFunctionPart(Demangle::NodePointer node,
788-
std::vector<T> &results) {
788+
SmallVectorImpl<T> &results) {
789789
if (node->getNumChildren() != 2)
790790
return true;
791791

@@ -871,7 +871,7 @@ class TypeDecoder {
871871

872872
bool decodeMangledFunctionInputType(
873873
Demangle::NodePointer node,
874-
std::vector<FunctionParam<BuiltType>> &params,
874+
SmallVectorImpl<FunctionParam<BuiltType>> &params,
875875
bool &hasParamFlags) {
876876
// Look through a couple of sugar nodes.
877877
if (node->getKind() == NodeKind::Type ||

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ class TypeRefBuilder {
210210

211211
Optional<std::string>
212212
createTypeDecl(Node *node, bool &typeAlias) {
213-
return Demangle::mangleNode(node, &Dem);
213+
return Demangle::mangleNode(node);
214214
}
215215

216216
BuiltProtocolDecl
217217
createProtocolDecl(Node *node) {
218-
return std::make_pair(Demangle::mangleNode(node, &Dem), false);
218+
return std::make_pair(Demangle::mangleNode(node), false);
219219
}
220220

221221
BuiltProtocolDecl
@@ -274,21 +274,21 @@ class TypeRefBuilder {
274274

275275
const BoundGenericTypeRef *
276276
createBoundGenericType(const Optional<std::string> &mangledName,
277-
const std::vector<const TypeRef *> &args,
277+
ArrayRef<const TypeRef *> args,
278278
const TypeRef *parent) {
279279
return BoundGenericTypeRef::create(*this, *mangledName, args, parent);
280280
}
281281

282282
const TupleTypeRef *
283-
createTupleType(const std::vector<const TypeRef *> &elements,
283+
createTupleType(ArrayRef<const TypeRef *> elements,
284284
std::string &&labels, bool isVariadic) {
285285
// FIXME: Add uniqueness checks in TupleTypeRef::Profile and
286286
// unittests/Reflection/TypeRef.cpp if using labels for identity.
287287
return TupleTypeRef::create(*this, elements, isVariadic);
288288
}
289289

290290
const FunctionTypeRef *createFunctionType(
291-
const std::vector<remote::FunctionParam<const TypeRef *>> &params,
291+
ArrayRef<remote::FunctionParam<const TypeRef *>> params,
292292
const TypeRef *result, FunctionTypeFlags flags) {
293293
return FunctionTypeRef::create(*this, params, result, flags);
294294
}
@@ -406,7 +406,7 @@ class TypeRefBuilder {
406406

407407
const ObjCClassTypeRef *
408408
createBoundGenericObjCClassType(const std::string &name,
409-
std::vector<const TypeRef *> &args) {
409+
ArrayRef<const TypeRef *> args) {
410410
// Remote reflection just ignores generic arguments for Objective-C
411411
// lightweight generic types, since they don't affect layout.
412412
return createObjCClassType(name);

lib/Demangling/Demangler.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,23 @@ void CharVector::append(StringRef Rhs, NodeFactory &Factory) {
465465
}
466466

467467
void CharVector::append(int Number, NodeFactory &Factory) {
468-
const int MaxIntPrintSize = 8;
468+
const int MaxIntPrintSize = 11;
469469
if (NumElems + MaxIntPrintSize > Capacity)
470470
Factory.Reallocate(Elems, Capacity, /*Growth*/ MaxIntPrintSize);
471471
int Length = snprintf(Elems + NumElems, MaxIntPrintSize, "%d", Number);
472472
assert(Length > 0 && Length < MaxIntPrintSize);
473473
NumElems += Length;
474474
}
475475

476+
void CharVector::append(unsigned long long Number, NodeFactory &Factory) {
477+
const int MaxPrintSize = 21;
478+
if (NumElems + MaxPrintSize > Capacity)
479+
Factory.Reallocate(Elems, Capacity, /*Growth*/ MaxPrintSize);
480+
int Length = snprintf(Elems + NumElems, MaxPrintSize, "%llu", Number);
481+
assert(Length > 0 && Length < MaxPrintSize);
482+
NumElems += Length;
483+
}
484+
476485
//////////////////////////////////
477486
// Demangler member functions //
478487
//////////////////////////////////

0 commit comments

Comments
 (0)