From 12f8c31c04eaa72e480f95ee8123c3aa3dbebcca Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Mon, 22 May 2017 21:07:14 +0000 Subject: Make TypeSerializer's StringMap use the same allocator. llvm-svn: 303576 --- llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp | 43 ++++++---------------- .../lib/DebugInfo/CodeView/TypeTableCollection.cpp | 2 +- 2 files changed, 13 insertions(+), 32 deletions(-) (limited to 'llvm/lib/DebugInfo') diff --git a/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp b/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp index 3b061e67e05..f9865600bf8 100644 --- a/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeSerializer.cpp @@ -52,43 +52,24 @@ Error TypeSerializer::writeRecordPrefix(TypeLeafKind Kind) { } TypeIndex -TypeSerializer::insertRecordBytesPrivate(MutableArrayRef Record) { +TypeSerializer::insertRecordBytesPrivate(ArrayRef &Record) { assert(Record.size() % 4 == 0 && "Record is not aligned to 4 bytes!"); StringRef S(reinterpret_cast(Record.data()), Record.size()); TypeIndex NextTypeIndex = calcNextTypeIndex(); auto Result = HashedRecords.try_emplace(S, NextTypeIndex); + + StringRef NewData = Result.first->getKey(); + Record = ArrayRef(NewData.bytes_begin(), NewData.bytes_end()); + if (Result.second) { + // If this triggered an insert into the map, store the bytes. LastTypeIndex = NextTypeIndex; SeenRecords.push_back(Record); } - return Result.first->getValue(); -} - -TypeIndex -TypeSerializer::insertRecordBytesWithCopy(CVType &Record, - MutableArrayRef Data) { - assert(Data.size() % 4 == 0 && "Record is not aligned to 4 bytes!"); - - StringRef S(reinterpret_cast(Data.data()), Data.size()); - - // Do a two state lookup / insert so that we don't have to allocate unless - // we're going - // to do an insert. This is a big memory savings. - auto Iter = HashedRecords.find(S); - if (Iter != HashedRecords.end()) - return Iter->second; - LastTypeIndex = calcNextTypeIndex(); - uint8_t *Copy = RecordStorage.Allocate(Data.size()); - ::memcpy(Copy, Data.data(), Data.size()); - Data = MutableArrayRef(Copy, Data.size()); - S = StringRef(reinterpret_cast(Data.data()), Data.size()); - HashedRecords.insert(std::make_pair(S, LastTypeIndex)); - SeenRecords.push_back(Data); - Record.RecordData = Data; - return LastTypeIndex; + return Result.first->getValue(); } Expected> @@ -112,19 +93,19 @@ TypeSerializer::TypeSerializer(BumpPtrAllocator &Storage) : RecordStorage(Storage), LastTypeIndex(), RecordBuffer(MaxRecordLength * 2), Stream(RecordBuffer, llvm::support::little), Writer(Stream), - Mapping(Writer) { + Mapping(Writer), HashedRecords(Storage) { // RecordBuffer needs to be able to hold enough data so that if we are 1 // byte short of MaxRecordLen, and then we try to write MaxRecordLen bytes, // we won't overflow. } -ArrayRef> TypeSerializer::records() const { +ArrayRef> TypeSerializer::records() const { return SeenRecords; } TypeIndex TypeSerializer::getLastTypeIndex() const { return LastTypeIndex; } -TypeIndex TypeSerializer::insertRecordBytes(MutableArrayRef Record) { +TypeIndex TypeSerializer::insertRecordBytes(ArrayRef Record) { assert(!TypeKind.hasValue() && "Already in a type mapping!"); assert(Writer.getOffset() == 0 && "Stream has data already!"); @@ -163,8 +144,8 @@ Expected TypeSerializer::visitTypeEndGetIndex(CVType &Record) { Prefix->RecordLen = ThisRecordData.size() - sizeof(uint16_t); Record.Type = *TypeKind; - TypeIndex InsertedTypeIndex = - insertRecordBytesWithCopy(Record, ThisRecordData); + Record.RecordData = ThisRecordData; + TypeIndex InsertedTypeIndex = insertRecordBytesPrivate(Record.RecordData); // Write out each additional segment in reverse order, and update each // record's continuation index to point to the previous one. diff --git a/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp b/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp index a18710d6ab5..4adecbc483e 100644 --- a/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeTableCollection.cpp @@ -25,7 +25,7 @@ static void error(Error &&EC) { } TypeTableCollection::TypeTableCollection( - ArrayRef> Records) + ArrayRef> Records) : Records(Records), Database(Records.size()) {} Optional TypeTableCollection::getFirst() { -- cgit v1.2.3