Skip to content

Commit 8278c06

Browse files
eecksteinadrian-prantl
authored andcommitted
Demangler: Reduce sizeof(Node) from 48 bytes to 24 bytes
This is done by disallowing nodes with children to also have index or text payloads. In some cases those payloads were not needed anyway, because the information can be derived later. In other cases the fix was to insert an additional child node with the index/text payload. Also, implement single or double children as "inline" children, which avoids needing a separate node vector for children. All this reduces the needed size for node trees by over 2x.
1 parent cd47b03 commit 8278c06

File tree

14 files changed

+336
-250
lines changed

14 files changed

+336
-250
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,38 @@ class Node {
142142
friend class NodeFactory;
143143

144144
private:
145-
Kind NodeKind;
146145

147-
enum class PayloadKind : uint8_t {
148-
None, Text, Index
146+
struct NodeVector {
147+
NodePointer *Nodes;
148+
uint32_t Number = 0;
149+
uint32_t Capacity = 0;
149150
};
150-
PayloadKind NodePayloadKind;
151151

152152
union {
153-
llvm::StringRef TextPayload;
154-
IndexType IndexPayload;
153+
llvm::StringRef Text;
154+
IndexType Index;
155+
NodePointer InlineChildren[2];
156+
NodeVector Children;
155157
};
156158

157-
NodePointer *Children = nullptr;
158-
size_t NumChildren = 0;
159-
size_t ReservedChildren = 0;
159+
160+
Kind NodeKind;
161+
162+
enum class PayloadKind : uint8_t {
163+
None, Text, Index, OneChild, TwoChildren, ManyChildren
164+
};
165+
PayloadKind NodePayloadKind;
160166

161167
Node(Kind k)
162168
: NodeKind(k), NodePayloadKind(PayloadKind::None) {
163169
}
164170
Node(Kind k, llvm::StringRef t)
165171
: NodeKind(k), NodePayloadKind(PayloadKind::Text) {
166-
TextPayload = t;
172+
Text = t;
167173
}
168174
Node(Kind k, IndexType index)
169175
: NodeKind(k), NodePayloadKind(PayloadKind::Index) {
170-
IndexPayload = index;
176+
Index = index;
171177
}
172178
Node(const Node &) = delete;
173179
Node &operator=(const Node &) = delete;
@@ -178,37 +184,33 @@ class Node {
178184
bool hasText() const { return NodePayloadKind == PayloadKind::Text; }
179185
llvm::StringRef getText() const {
180186
assert(hasText());
181-
return TextPayload;
187+
return Text;
182188
}
183189

184190
bool hasIndex() const { return NodePayloadKind == PayloadKind::Index; }
185191
uint64_t getIndex() const {
186192
assert(hasIndex());
187-
return IndexPayload;
193+
return Index;
188194
}
189195

190-
using iterator = NodePointer *;
191-
using const_iterator = const NodePointer *;
192-
using size_type = size_t;
196+
using iterator = const NodePointer *;
197+
198+
size_t getNumChildren() const;
193199

194-
bool hasChildren() const { return NumChildren != 0; }
195-
size_t getNumChildren() const { return NumChildren; }
196-
iterator begin() { return Children; }
197-
iterator end() { return Children + NumChildren; }
198-
const_iterator begin() const { return Children; }
199-
const_iterator end() const { return Children + NumChildren; }
200+
bool hasChildren() const { return getNumChildren() != 0; }
201+
202+
iterator begin() const;
203+
204+
iterator end() const;
200205

201206
NodePointer getFirstChild() const {
202-
assert(NumChildren >= 1);
203-
return Children[0];
207+
return getChild(0);
204208
}
205209
NodePointer getChild(size_t index) const {
206-
assert(NumChildren > index);
207-
return Children[index];
210+
assert(getNumChildren() > index);
211+
return begin()[index];
208212
}
209213

210-
// inline void addChild(NodePointer Child, Context &Ctx);
211-
212214
// Only to be used by the demangler parsers.
213215
void addChild(NodePointer Child, NodeFactory &Factory);
214216
// Only to be used by the demangler parsers.

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ NODE(FullTypeMetadata)
8888
CONTEXT_NODE(Function)
8989
NODE(FunctionSignatureSpecialization)
9090
NODE(FunctionSignatureSpecializationParam)
91+
NODE(FunctionSignatureSpecializationReturn)
9192
NODE(FunctionSignatureSpecializationParamKind)
9293
NODE(FunctionSignatureSpecializationParamPayload)
9394
NODE(FunctionType)

include/swift/Demangling/Demangler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class NodeFactory {
132132
/// new memory address.
133133
/// The \p Capacity is enlarged at least by \p MinGrowth, but can also be
134134
/// enlarged by a bigger value.
135-
template<typename T> void Reallocate(T *&Objects, size_t &Capacity,
135+
template<typename T> void Reallocate(T *&Objects, uint32_t &Capacity,
136136
size_t MinGrowth) {
137137
size_t OldAllocSize = Capacity * sizeof(T);
138138
size_t AdditionalAlloc = MinGrowth * sizeof(T);
@@ -203,8 +203,8 @@ template<typename T> class Vector {
203203

204204
protected:
205205
T *Elems = nullptr;
206-
size_t NumElems = 0;
207-
size_t Capacity = 0;
206+
uint32_t NumElems = 0;
207+
uint32_t Capacity = 0;
208208

209209
public:
210210
using iterator = T *;
@@ -469,7 +469,7 @@ class Demangler : public NodeFactory {
469469
NodePointer demangleThunkOrSpecialization();
470470
NodePointer demangleGenericSpecialization(Node::Kind SpecKind);
471471
NodePointer demangleFunctionSpecialization();
472-
NodePointer demangleFuncSpecParam(Node::IndexType ParamIdx);
472+
NodePointer demangleFuncSpecParam(Node::Kind Kind);
473473
NodePointer addFuncSpecParamNumber(NodePointer Param,
474474
FunctionSigSpecializationParamKind Kind);
475475

include/swift/Demangling/TypeDecoder.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,21 +676,21 @@ class TypeDecoder {
676676
auto base = decodeMangledType(Node->getChild(0));
677677
if (!base)
678678
return BuiltType();
679-
auto member = Node->getChild(1)->getText();
680679
auto assocTypeChild = Node->getChild(1);
681-
if (assocTypeChild->getNumChildren() < 1)
680+
auto member = assocTypeChild->getFirstChild()->getText();
681+
if (assocTypeChild->getNumChildren() < 2)
682682
return Builder.createDependentMemberType(member, base);
683683

684-
auto protocol = decodeMangledProtocolType(assocTypeChild->getChild(0));
684+
auto protocol = decodeMangledProtocolType(assocTypeChild->getChild(1));
685685
if (!protocol)
686686
return BuiltType();
687687
return Builder.createDependentMemberType(member, base, protocol);
688688
}
689689
case NodeKind::DependentAssociatedTypeRef: {
690-
if (Node->getNumChildren() < 1)
690+
if (Node->getNumChildren() < 2)
691691
return BuiltType();
692692

693-
return decodeMangledType(Node->getChild(0));
693+
return decodeMangledType(Node->getChild(1));
694694
}
695695
case NodeKind::Unowned: {
696696
if (Node->getNumChildren() < 1)

include/swift/Remote/MetadataReader.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,9 +2129,10 @@ class MetadataReader {
21292129
}
21302130

21312131
if (importInfo && !importInfo->RelatedEntityName.empty()) {
2132-
auto relatedNode =
2133-
dem.createNode(Node::Kind::RelatedEntityDeclName,
2134-
std::move(importInfo->RelatedEntityName));
2132+
auto kindNode = dem.createNode(Node::Kind::Identifier,
2133+
std::move(importInfo->RelatedEntityName));
2134+
auto relatedNode = dem.createNode(Node::Kind::RelatedEntityDeclName);
2135+
relatedNode->addChild(kindNode, dem);
21352136
relatedNode->addChild(nameNode, dem);
21362137
nameNode = relatedNode;
21372138
}

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,8 @@ ASTBuilder::findDeclContext(NodePointer node) {
820820

821821
} else if (declNameNode->getKind() ==
822822
Demangle::Node::Kind::RelatedEntityDeclName) {
823-
name = declNameNode->getChild(0)->getText();
824-
relatedEntityKind = declNameNode->getText();
823+
name = declNameNode->getChild(1)->getText();
824+
relatedEntityKind = declNameNode->getFirstChild()->getText();
825825

826826
// Ignore any other decl-name productions for now.
827827
} else {

0 commit comments

Comments
 (0)