Skip to content

Commit 5db45a4

Browse files
committed
demangler: A few more cosmetic performance improvements.
Avoid some string copies. Although it has no significant measurable effect, it makes me feel better.
1 parent 031ecc2 commit 5db45a4

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

include/swift/Basic/Demangler.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,18 @@ class NodeFactory {
170170
/// Creates a node of kind \p K with an \p Index payload.
171171
NodePointer createNode(Node::Kind K, Node::IndexType Index);
172172

173+
/// Creates a node of kind \p K with a \p Text payload.
174+
///
175+
/// The \p Text string must be already allocted with the Factory and therefore
176+
/// it is _not_ copied.
177+
NodePointer createNodeWithAllocatedText(Node::Kind K, llvm::StringRef Text);
178+
173179
/// Creates a node of kind \p K with a \p Text payload.
174180
///
175181
/// The \p Text string is copied.
176-
NodePointer createNode(Node::Kind K, llvm::StringRef Text);
182+
NodePointer createNode(Node::Kind K, llvm::StringRef Text) {
183+
return createNodeWithAllocatedText(K, Text.copy(*this));
184+
}
177185

178186
/// Creates a node of kind \p K with a \p Text payload.
179187
///
@@ -387,7 +395,7 @@ class Demangler : public NodeFactory {
387395
NodePointer demangleOperatorIdentifier();
388396

389397
NodePointer demangleMultiSubstitutions();
390-
NodePointer createSwiftType(Node::Kind typeKind, StringRef name);
398+
NodePointer createSwiftType(Node::Kind typeKind, const char *name);
391399
NodePointer demangleKnownType();
392400
NodePointer demangleLocalIdentifier();
393401

lib/Basic/Demangler.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,12 @@ NodePointer NodeFactory::createNode(Node::Kind K) {
352352
NodePointer NodeFactory::createNode(Node::Kind K, Node::IndexType Index) {
353353
return new (Allocate<Node>()) Node(K, Index);
354354
}
355-
NodePointer NodeFactory::createNode(Node::Kind K, llvm::StringRef Text) {
356-
return new (Allocate<Node>()) Node(K, Text.copy(*this));
355+
NodePointer NodeFactory::createNodeWithAllocatedText(Node::Kind K,
356+
llvm::StringRef Text) {
357+
return new (Allocate<Node>()) Node(K, Text);
357358
}
358359
NodePointer NodeFactory::createNode(Node::Kind K, const CharVector &Text) {
359-
return new (Allocate<Node>()) Node(K, Text.str());
360+
return createNodeWithAllocatedText(K, Text.str());
360361
}
361362
NodePointer NodeFactory::createNode(Node::Kind K, const char *Text) {
362363
return new (Allocate<Node>()) Node(K, llvm::StringRef(Text));
@@ -527,7 +528,7 @@ NodePointer Demangler::changeKind(NodePointer Node, Node::Kind NewKind) {
527528
return nullptr;
528529
NodePointer NewNode = nullptr;
529530
if (Node->hasText()) {
530-
NewNode = createNode(NewKind, Node->getText());
531+
NewNode = createNodeWithAllocatedText(NewKind, Node->getText());
531532
} else if (Node->hasIndex()) {
532533
NewNode = createNode(NewKind, Node->getIndex());
533534
} else {
@@ -649,7 +650,7 @@ NodePointer Demangler::demangleMultiSubstitutions() {
649650
}
650651
}
651652

652-
NodePointer Demangler::createSwiftType(Node::Kind typeKind, StringRef name) {
653+
NodePointer Demangler::createSwiftType(Node::Kind typeKind, const char *name) {
653654
return createType(createWithChildren(typeKind,
654655
createNode(Node::Kind::Module, STDLIB_NAME),
655656
createNode(Node::Kind::Identifier, name)));
@@ -985,8 +986,9 @@ NodePointer Demangler::popTuple() {
985986
firstElem = (popNode(Node::Kind::FirstElementMarker) != nullptr);
986987
NodePointer TupleElmt = createNode(Node::Kind::TupleElement);
987988
if (NodePointer Ident = popNode(Node::Kind::Identifier)) {
988-
TupleElmt->addChild(createNode(Node::Kind::TupleElementName,
989-
Ident->getText()), *this);
989+
TupleElmt->addChild(createNodeWithAllocatedText(
990+
Node::Kind::TupleElementName, Ident->getText()),
991+
*this);
990992
}
991993
NodePointer Ty = popNode(Node::Kind::Type);
992994
if (!Ty)
@@ -1135,7 +1137,7 @@ NodePointer Demangler::demangleImplFunctionType() {
11351137
if (GenSig && nextIf('P'))
11361138
GenSig = changeKind(GenSig, Node::Kind::DependentPseudogenericSignature);
11371139

1138-
StringRef CAttr;
1140+
const char *CAttr = nullptr;
11391141
switch (nextChar()) {
11401142
case 'y': CAttr = "@callee_unowned"; break;
11411143
case 'g': CAttr = "@callee_guaranteed"; break;
@@ -1145,7 +1147,7 @@ NodePointer Demangler::demangleImplFunctionType() {
11451147
}
11461148
type->addChild(createNode(Node::Kind::ImplConvention, CAttr), *this);
11471149

1148-
StringRef FAttr;
1150+
const char *FAttr = nullptr;
11491151
switch (nextChar()) {
11501152
case 'B': FAttr = "@convention(block)"; break;
11511153
case 'C': FAttr = "@convention(c)"; break;
@@ -1157,7 +1159,7 @@ NodePointer Demangler::demangleImplFunctionType() {
11571159
pushBack();
11581160
break;
11591161
}
1160-
if (!FAttr.empty())
1162+
if (FAttr)
11611163
type->addChild(createNode(Node::Kind::ImplFunctionAttribute, FAttr), *this);
11621164

11631165
addChild(type, GenSig);
@@ -1473,7 +1475,7 @@ NodePointer Demangler::demangleFunctionSpecialization() {
14731475
// A '_' escapes a leading digit or '_' of a string constant.
14741476
Text = Text.drop_front(1);
14751477
}
1476-
addChild(Param, createNode(
1478+
addChild(Param, createNodeWithAllocatedText(
14771479
Node::Kind::FunctionSignatureSpecializationParamPayload, Text));
14781480
Param->reverseChildren(FixedChildren);
14791481
break;
@@ -1523,7 +1525,7 @@ NodePointer Demangler::demangleFuncSpecParam(Node::IndexType ParamIdx) {
15231525
case 's': {
15241526
// Consumes an identifier parameter (the string constant),
15251527
// which will be added later.
1526-
StringRef Encoding;
1528+
const char *Encoding = nullptr;
15271529
switch (nextChar()) {
15281530
case 'b': Encoding = "u8"; break;
15291531
case 'w': Encoding = "u16"; break;

0 commit comments

Comments
 (0)