forked from llvm-mirror/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ELFObjectWriter: deduplicate suffices in strtab
We already do this for shstrtab, so might as well do it for strtab. This extracts the string table building code into a separate class. The idea is to use it for other object formats too. I mostly wanted to do this for the general principle, but it does save a little bit on object file size. I tried this on a clang bootstrap and saved 0.54% on the sum of object file sizes (1.14 MB out of 212 MB for a release build). Differential Revision: http://reviews.llvm.org/D3533 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207670 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
Showing
22 changed files
with
282 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//===-- StringTableBuilder.h - String table building utility ------*- C++ -*-=// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_OBJECT_STRINGTABLE_BUILDER_H | ||
#define LLVM_OBJECT_STRINGTABLE_BUILDER_H | ||
|
||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/ADT/StringMap.h" | ||
#include <cassert> | ||
|
||
namespace llvm { | ||
|
||
/// \brief Utility for building string tables with deduplicated suffixes. | ||
class StringTableBuilder { | ||
SmallString<256> StringTable; | ||
StringMap<size_t> StringIndexMap; | ||
|
||
public: | ||
/// \brief Add a string to the builder. Returns a StringRef to the internal | ||
/// copy of s. Can only be used before the table is finalized. | ||
StringRef add(StringRef s) { | ||
assert(!isFinalized()); | ||
return StringIndexMap.GetOrCreateValue(s, 0).getKey(); | ||
} | ||
|
||
/// \brief Analyze the strings and build the final table. No more strings can | ||
/// be added after this point. | ||
void finalize(); | ||
|
||
/// \brief Retrieve the string table data. Can only be used after the table | ||
/// is finalized. | ||
StringRef data() { | ||
assert(isFinalized()); | ||
return StringTable; | ||
} | ||
|
||
/// \brief Get the offest of a string in the string table. Can only be used | ||
/// after the table is finalized. | ||
size_t getOffset(StringRef s) { | ||
assert(isFinalized()); | ||
assert(StringIndexMap.count(s) && "String is not in table!"); | ||
return StringIndexMap[s]; | ||
} | ||
|
||
private: | ||
bool isFinalized() { | ||
return !StringTable.empty(); | ||
} | ||
}; | ||
|
||
} // end llvm namespace | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===-- StringTableBuilder.cpp - String table building utility ------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/Object/StringTableBuilder.h" | ||
|
||
using namespace llvm; | ||
|
||
static bool compareBySuffix(StringRef a, StringRef b) { | ||
size_t sizeA = a.size(); | ||
size_t sizeB = b.size(); | ||
size_t len = std::min(sizeA, sizeB); | ||
for (size_t i = 0; i < len; ++i) { | ||
char ca = a[sizeA - i - 1]; | ||
char cb = b[sizeB - i - 1]; | ||
if (ca != cb) | ||
return ca > cb; | ||
} | ||
return sizeA > sizeB; | ||
} | ||
|
||
void StringTableBuilder::finalize() { | ||
SmallVector<StringRef, 8> Strings; | ||
for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i) | ||
Strings.push_back(i->getKey()); | ||
|
||
std::sort(Strings.begin(), Strings.end(), compareBySuffix); | ||
|
||
// FIXME: Starting with a null byte is ELF specific. Generalize this so we | ||
// can use the class with other object formats. | ||
StringTable += '\x00'; | ||
|
||
StringRef Previous; | ||
for (StringRef s : Strings) { | ||
if (Previous.endswith(s)) { | ||
StringIndexMap[s] = StringTable.size() - 1 - s.size(); | ||
continue; | ||
} | ||
|
||
StringIndexMap[s] = StringTable.size(); | ||
StringTable += s; | ||
StringTable += '\x00'; | ||
Previous = s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.