Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit cfb2d8c

Browse files
committed
[CodeGen] Optimize AccelTable
Summary: The class contained arrays of two structures (DataArray and HashData). These structures were in 1:1 correspondence, and one of them contained pointers to the other (and *both* contained a "Name" field). By merging these two structures into one, we can save a bit of space without negatively impacting much of anything. Reviewers: JDevlieghere, aprantl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D43073 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324724 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 562d4e5 commit cfb2d8c

File tree

2 files changed

+28
-41
lines changed

2 files changed

+28
-41
lines changed

include/llvm/CodeGen/AccelTable.h

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -214,26 +214,19 @@ class AppleAccelTableData {
214214
/// Apple-style accelerator table base class.
215215
class AppleAccelTableBase {
216216
protected:
217-
struct DataArray {
218-
DwarfStringPoolEntryRef Name;
219-
std::vector<AppleAccelTableData *> Values;
220-
};
221-
222-
friend struct HashData;
223-
224217
struct HashData {
225-
StringRef Str;
218+
DwarfStringPoolEntryRef Name;
226219
uint32_t HashValue;
220+
std::vector<AppleAccelTableData *> Values;
227221
MCSymbol *Sym;
228-
DataArray &Data;
229222

230-
HashData(StringRef S, DataArray &Data) : Str(S), Data(Data) {
231-
HashValue = djbHash(S);
223+
HashData(DwarfStringPoolEntryRef Name) : Name(Name) {
224+
HashValue = djbHash(Name.getString());
232225
}
233226

234227
#ifndef NDEBUG
235-
void print(raw_ostream &OS);
236-
void dump() { print(dbgs()); }
228+
void print(raw_ostream &OS) const;
229+
void dump() const { print(dbgs()); }
237230
#endif
238231
};
239232

@@ -243,9 +236,7 @@ class AppleAccelTableBase {
243236
/// Header containing both the header and header data.
244237
AppleAccelTableHeader Header;
245238

246-
std::vector<HashData *> Data;
247-
248-
using StringEntries = StringMap<DataArray, BumpPtrAllocator &>;
239+
using StringEntries = StringMap<HashData, BumpPtrAllocator &>;
249240
StringEntries Entries;
250241

251242
using HashList = std::vector<HashData *>;
@@ -323,13 +314,12 @@ template <typename AppleAccelTableDataT>
323314
template <class... Types>
324315
void AppleAccelTable<AppleAccelTableDataT>::addName(
325316
DwarfStringPoolEntryRef Name, Types... Args) {
326-
assert(Data.empty() && "Already finalized!");
317+
assert(Buckets.empty() && "Already finalized!");
327318
// If the string is in the list already then add this die to the list
328319
// otherwise add a new one.
329-
DataArray &DA = Entries[Name.getString()];
330-
assert(!DA.Name || DA.Name == Name);
331-
DA.Name = Name;
332-
DA.Values.push_back(new (Allocator) AppleAccelTableDataT(Args...));
320+
auto Iter = Entries.try_emplace(Name.getString(), Name).first;
321+
assert(Iter->second.Name == Name);
322+
Iter->second.Values.push_back(new (Allocator) AppleAccelTableDataT(Args...));
333323
}
334324

335325
/// Accelerator table data implementation for simple accelerator tables with

lib/CodeGen/AsmPrinter/AccelTable.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ void AppleAccelTableBase::emitData(AsmPrinter *Asm) {
138138
Asm->EmitInt32(0);
139139
// Remember to emit the label for our offset.
140140
Asm->OutStreamer->EmitLabel(Hash->Sym);
141-
Asm->OutStreamer->AddComment(Hash->Str);
142-
Asm->emitDwarfStringOffset(Hash->Data.Name);
141+
Asm->OutStreamer->AddComment(Hash->Name.getString());
142+
Asm->emitDwarfStringOffset(Hash->Name);
143143
Asm->OutStreamer->AddComment("Num DIEs");
144-
Asm->EmitInt32(Hash->Data.Values.size());
145-
for (const auto *V : Hash->Data.Values) {
144+
Asm->EmitInt32(Hash->Values.size());
145+
for (const auto *V : Hash->Values) {
146146
V->emit(Asm);
147147
}
148148
PrevHash = Hash->HashValue;
@@ -155,9 +155,10 @@ void AppleAccelTableBase::emitData(AsmPrinter *Asm) {
155155

156156
void AppleAccelTableBase::computeBucketCount() {
157157
// First get the number of unique hashes.
158-
std::vector<uint32_t> uniques(Data.size());
159-
for (size_t i = 0, e = Data.size(); i < e; ++i)
160-
uniques[i] = Data[i]->HashValue;
158+
std::vector<uint32_t> uniques;
159+
uniques.reserve(Entries.size());
160+
for (const auto &E : Entries)
161+
uniques.push_back(E.second.HashValue);
161162
array_pod_sort(uniques.begin(), uniques.end());
162163
std::vector<uint32_t>::iterator p =
163164
std::unique(uniques.begin(), uniques.end());
@@ -169,7 +170,6 @@ void AppleAccelTableBase::computeBucketCount() {
169170

170171
void AppleAccelTableBase::finalizeTable(AsmPrinter *Asm, StringRef Prefix) {
171172
// Create the individual hash data outputs.
172-
Data.reserve(Entries.size());
173173
for (auto &E : Entries) {
174174
// Unique the entries.
175175
std::stable_sort(E.second.Values.begin(), E.second.Values.end(),
@@ -178,9 +178,6 @@ void AppleAccelTableBase::finalizeTable(AsmPrinter *Asm, StringRef Prefix) {
178178
E.second.Values.erase(
179179
std::unique(E.second.Values.begin(), E.second.Values.end()),
180180
E.second.Values.end());
181-
182-
HashData *Entry = new (Allocator) HashData(E.first(), E.second);
183-
Data.push_back(Entry);
184181
}
185182

186183
// Figure out how many buckets we need, then compute the bucket contents and
@@ -192,10 +189,10 @@ void AppleAccelTableBase::finalizeTable(AsmPrinter *Asm, StringRef Prefix) {
192189

193190
// Compute bucket contents and final ordering.
194191
Buckets.resize(Header.getBucketCount());
195-
for (auto &D : Data) {
196-
uint32_t bucket = D->HashValue % Header.getBucketCount();
197-
Buckets[bucket].push_back(D);
198-
D->Sym = Asm->createTempSymbol(Prefix);
192+
for (auto &E : Entries) {
193+
uint32_t bucket = E.second.HashValue % Header.getBucketCount();
194+
Buckets[bucket].push_back(&E.second);
195+
E.second.Sym = Asm->createTempSymbol(Prefix);
199196
}
200197

201198
// Sort the contents of the buckets by hash value so that hash collisions end
@@ -286,16 +283,16 @@ void AppleAccelTableHeader::print(raw_ostream &OS) const {
286283
HeaderData.print(OS);
287284
}
288285

289-
void AppleAccelTableBase::HashData::print(raw_ostream &OS) {
290-
OS << "Name: " << Str << "\n";
286+
void AppleAccelTableBase::HashData::print(raw_ostream &OS) const {
287+
OS << "Name: " << Name.getString() << "\n";
291288
OS << " Hash Value: " << format("0x%x", HashValue) << "\n";
292289
OS << " Symbol: ";
293290
if (Sym)
294291
OS << *Sym;
295292
else
296293
OS << "<none>";
297294
OS << "\n";
298-
for (auto *Value : Data.Values)
295+
for (auto *Value : Values)
299296
Value->print(OS);
300297
}
301298

@@ -317,8 +314,8 @@ void AppleAccelTableBase::print(raw_ostream &OS) const {
317314
Hash->print(OS);
318315

319316
OS << "Data: \n";
320-
for (auto &D : Data)
321-
D->print(OS);
317+
for (auto &E : Entries)
318+
E.second.print(OS);
322319
}
323320

324321
void AppleAccelTableOffsetData::print(raw_ostream &OS) const {

0 commit comments

Comments
 (0)