Skip to content

demangler: A few more cosmetic performance improvements. #7922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/swift/Basic/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,18 @@ class NodeFactory {
/// Creates a node of kind \p K with an \p Index payload.
NodePointer createNode(Node::Kind K, Node::IndexType Index);

/// Creates a node of kind \p K with a \p Text payload.
///
/// The \p Text string must be already allocted with the Factory and therefore
/// it is _not_ copied.
NodePointer createNodeWithAllocatedText(Node::Kind K, llvm::StringRef Text);

/// Creates a node of kind \p K with a \p Text payload.
///
/// The \p Text string is copied.
NodePointer createNode(Node::Kind K, llvm::StringRef Text);
NodePointer createNode(Node::Kind K, llvm::StringRef Text) {
return createNodeWithAllocatedText(K, Text.copy(*this));
}

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

NodePointer demangleMultiSubstitutions();
NodePointer createSwiftType(Node::Kind typeKind, StringRef name);
NodePointer createSwiftType(Node::Kind typeKind, const char *name);
NodePointer demangleKnownType();
NodePointer demangleLocalIdentifier();

Expand Down
26 changes: 14 additions & 12 deletions lib/Basic/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,12 @@ NodePointer NodeFactory::createNode(Node::Kind K) {
NodePointer NodeFactory::createNode(Node::Kind K, Node::IndexType Index) {
return new (Allocate<Node>()) Node(K, Index);
}
NodePointer NodeFactory::createNode(Node::Kind K, llvm::StringRef Text) {
return new (Allocate<Node>()) Node(K, Text.copy(*this));
NodePointer NodeFactory::createNodeWithAllocatedText(Node::Kind K,
llvm::StringRef Text) {
return new (Allocate<Node>()) Node(K, Text);
}
NodePointer NodeFactory::createNode(Node::Kind K, const CharVector &Text) {
return new (Allocate<Node>()) Node(K, Text.str());
return createNodeWithAllocatedText(K, Text.str());
}
NodePointer NodeFactory::createNode(Node::Kind K, const char *Text) {
return new (Allocate<Node>()) Node(K, llvm::StringRef(Text));
Expand Down Expand Up @@ -527,7 +528,7 @@ NodePointer Demangler::changeKind(NodePointer Node, Node::Kind NewKind) {
return nullptr;
NodePointer NewNode = nullptr;
if (Node->hasText()) {
NewNode = createNode(NewKind, Node->getText());
NewNode = createNodeWithAllocatedText(NewKind, Node->getText());
} else if (Node->hasIndex()) {
NewNode = createNode(NewKind, Node->getIndex());
} else {
Expand Down Expand Up @@ -649,7 +650,7 @@ NodePointer Demangler::demangleMultiSubstitutions() {
}
}

NodePointer Demangler::createSwiftType(Node::Kind typeKind, StringRef name) {
NodePointer Demangler::createSwiftType(Node::Kind typeKind, const char *name) {
return createType(createWithChildren(typeKind,
createNode(Node::Kind::Module, STDLIB_NAME),
createNode(Node::Kind::Identifier, name)));
Expand Down Expand Up @@ -985,8 +986,9 @@ NodePointer Demangler::popTuple() {
firstElem = (popNode(Node::Kind::FirstElementMarker) != nullptr);
NodePointer TupleElmt = createNode(Node::Kind::TupleElement);
if (NodePointer Ident = popNode(Node::Kind::Identifier)) {
TupleElmt->addChild(createNode(Node::Kind::TupleElementName,
Ident->getText()), *this);
TupleElmt->addChild(createNodeWithAllocatedText(
Node::Kind::TupleElementName, Ident->getText()),
*this);
}
NodePointer Ty = popNode(Node::Kind::Type);
if (!Ty)
Expand Down Expand Up @@ -1135,7 +1137,7 @@ NodePointer Demangler::demangleImplFunctionType() {
if (GenSig && nextIf('P'))
GenSig = changeKind(GenSig, Node::Kind::DependentPseudogenericSignature);

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

StringRef FAttr;
const char *FAttr = nullptr;
switch (nextChar()) {
case 'B': FAttr = "@convention(block)"; break;
case 'C': FAttr = "@convention(c)"; break;
Expand All @@ -1157,7 +1159,7 @@ NodePointer Demangler::demangleImplFunctionType() {
pushBack();
break;
}
if (!FAttr.empty())
if (FAttr)
type->addChild(createNode(Node::Kind::ImplFunctionAttribute, FAttr), *this);

addChild(type, GenSig);
Expand Down Expand Up @@ -1473,7 +1475,7 @@ NodePointer Demangler::demangleFunctionSpecialization() {
// A '_' escapes a leading digit or '_' of a string constant.
Text = Text.drop_front(1);
}
addChild(Param, createNode(
addChild(Param, createNodeWithAllocatedText(
Node::Kind::FunctionSignatureSpecializationParamPayload, Text));
Param->reverseChildren(FixedChildren);
break;
Expand Down Expand Up @@ -1523,7 +1525,7 @@ NodePointer Demangler::demangleFuncSpecParam(Node::IndexType ParamIdx) {
case 's': {
// Consumes an identifier parameter (the string constant),
// which will be added later.
StringRef Encoding;
const char *Encoding = nullptr;
switch (nextChar()) {
case 'b': Encoding = "u8"; break;
case 'w': Encoding = "u16"; break;
Expand Down