[PDB] Remove unique_ptr wrapper around C13 line table subsections
authorReid Kleckner <rnk@google.com>
Sat, 2 May 2020 23:31:43 +0000 (16:31 -0700)
committerReid Kleckner <rnk@google.com>
Sat, 2 May 2020 23:35:07 +0000 (16:35 -0700)
This accounts for a large portion of the memory allocations in LLD.
This DebugSubsectionRecordBuilder object can be stored directly in
C13Builders, it mostly wraps other subsections.

Remove the container kind field from the object. It is always the same
for all elements in the vector, and we can pass it in during writing.

llvm/include/llvm/DebugInfo/CodeView/DebugSubsectionRecord.h
llvm/include/llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h
llvm/lib/DebugInfo/CodeView/DebugSubsectionRecord.cpp
llvm/lib/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.cpp
llvm/lib/ObjectYAML/COFFEmitter.cpp

index bcb379f..e915d8a 100644 (file)
@@ -35,44 +35,38 @@ struct DebugSubsectionHeader {
 class DebugSubsectionRecord {
 public:
   DebugSubsectionRecord();
-  DebugSubsectionRecord(DebugSubsectionKind Kind, BinaryStreamRef Data,
-                        CodeViewContainer Container);
+  DebugSubsectionRecord(DebugSubsectionKind Kind, BinaryStreamRef Data);
 
-  static Error initialize(BinaryStreamRef Stream, DebugSubsectionRecord &Info,
-                          CodeViewContainer Container);
+  static Error initialize(BinaryStreamRef Stream, DebugSubsectionRecord &Info);
 
   uint32_t getRecordLength() const;
   DebugSubsectionKind kind() const;
   BinaryStreamRef getRecordData() const;
 
 private:
-  CodeViewContainer Container = CodeViewContainer::ObjectFile;
   DebugSubsectionKind Kind = DebugSubsectionKind::None;
   BinaryStreamRef Data;
 };
 
 class DebugSubsectionRecordBuilder {
 public:
-  DebugSubsectionRecordBuilder(std::shared_ptr<DebugSubsection> Subsection,
-                               CodeViewContainer Container);
+  DebugSubsectionRecordBuilder(std::shared_ptr<DebugSubsection> Subsection);
 
   /// Use this to copy existing subsections directly from source to destination.
   /// For example, line table subsections in an object file only need to be
   /// relocated before being copied into the PDB.
-  DebugSubsectionRecordBuilder(const DebugSubsectionRecord &Contents,
-                               CodeViewContainer Container);
+  DebugSubsectionRecordBuilder(const DebugSubsectionRecord &Contents);
 
-  uint32_t calculateSerializedLength();
-  Error commit(BinaryStreamWriter &Writer) const;
+  uint32_t calculateSerializedLength() const;
+  Error commit(BinaryStreamWriter &Writer, CodeViewContainer Container) const;
 
 private:
   /// The subsection to build. Will be null if Contents is non-empty.
   std::shared_ptr<DebugSubsection> Subsection;
 
   /// The bytes of the subsection. Only non-empty if Subsection is null.
+  /// FIXME: Reduce the size of this.
   DebugSubsectionRecord Contents;
-
-  CodeViewContainer Container;
 };
 
 } // end namespace codeview
@@ -83,8 +77,7 @@ template <> struct VarStreamArrayExtractor<codeview::DebugSubsectionRecord> {
     // FIXME: We need to pass the container type through to this function.  In
     // practice this isn't super important since the subsection header describes
     // its length and we can just skip it.  It's more important when writing.
-    if (auto EC = codeview::DebugSubsectionRecord::initialize(
-            Stream, Info, codeview::CodeViewContainer::Pdb))
+    if (auto EC = codeview::DebugSubsectionRecord::initialize(Stream, Info))
       return EC;
     Length = alignTo(Info.getRecordLength(), 4);
     return Error::success();
index 4f5d28b..beaaef0 100644 (file)
@@ -93,8 +93,7 @@ private:
   std::vector<std::string> SourceFiles;
   std::vector<ArrayRef<uint8_t>> Symbols;
 
-  std::vector<std::unique_ptr<codeview::DebugSubsectionRecordBuilder>>
-      C13Builders;
+  std::vector<codeview::DebugSubsectionRecordBuilder> C13Builders;
 
   ModuleInfoHeader Layout;
 };
index 0f704f2..3c8a301 100644 (file)
@@ -23,13 +23,11 @@ using namespace llvm::codeview;
 DebugSubsectionRecord::DebugSubsectionRecord() = default;
 
 DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind,
-                                             BinaryStreamRef Data,
-                                             CodeViewContainer Container)
-    : Container(Container), Kind(Kind), Data(Data) {}
+                                             BinaryStreamRef Data)
+    : Kind(Kind), Data(Data) {}
 
 Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream,
-                                        DebugSubsectionRecord &Info,
-                                        CodeViewContainer Container) {
+                                        DebugSubsectionRecord &Info) {
   const DebugSubsectionHeader *Header;
   BinaryStreamReader Reader(Stream);
   if (auto EC = Reader.readObject(Header))
@@ -39,7 +37,6 @@ Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream,
       static_cast<DebugSubsectionKind>(uint32_t(Header->Kind));
   if (auto EC = Reader.readStreamRef(Info.Data, Header->Length))
     return EC;
-  Info.Container = Container;
   Info.Kind = Kind;
   return Error::success();
 }
@@ -53,14 +50,14 @@ DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; }
 BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; }
 
 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
-    std::shared_ptr<DebugSubsection> Subsection, CodeViewContainer Container)
-    : Subsection(std::move(Subsection)), Container(Container) {}
+    std::shared_ptr<DebugSubsection> Subsection)
+    : Subsection(std::move(Subsection)) {}
 
 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
-    const DebugSubsectionRecord &Contents, CodeViewContainer Container)
-    : Contents(Contents), Container(Container) {}
+    const DebugSubsectionRecord &Contents)
+    : Contents(Contents) {}
 
-uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
+uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() const {
   uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize()
                                  : Contents.getRecordData().getLength();
   // The length of the entire subsection is always padded to 4 bytes,
@@ -68,7 +65,8 @@ uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
   return sizeof(DebugSubsectionHeader) + alignTo(DataSize, 4);
 }
 
-Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const {
+Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer,
+                                           CodeViewContainer Container) const {
   assert(Writer.getOffset() % alignOf(Container) == 0 &&
          "Debug Subsection not properly aligned");
 
index 06bbbde..9b24300 100644 (file)
@@ -90,7 +90,7 @@ uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
   uint32_t Result = 0;
   for (const auto &Builder : C13Builders) {
     assert(Builder && "Empty C13 Fragment Builder!");
-    Result += Builder->calculateSerializedLength();
+    Result += Builder.calculateSerializedLength();
   }
   return Result;
 }
@@ -164,7 +164,7 @@ Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
     // TODO: Write C11 Line data
     for (const auto &Builder : C13Builders) {
       assert(Builder && "Empty C13 Fragment Builder!");
-      if (auto EC = Builder->commit(SymbolWriter))
+      if (auto EC = Builder.commit(SymbolWriter, CodeViewContainer::Pdb))
         return EC;
     }
 
@@ -180,12 +180,10 @@ Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
 void DbiModuleDescriptorBuilder::addDebugSubsection(
     std::shared_ptr<DebugSubsection> Subsection) {
   assert(Subsection);
-  C13Builders.push_back(std::make_unique<DebugSubsectionRecordBuilder>(
-      std::move(Subsection), CodeViewContainer::Pdb));
+  C13Builders.push_back(DebugSubsectionRecordBuilder(std::move(Subsection)));
 }
 
 void DbiModuleDescriptorBuilder::addDebugSubsection(
     const DebugSubsectionRecord &SubsectionContents) {
-  C13Builders.push_back(std::make_unique<DebugSubsectionRecordBuilder>(
-      SubsectionContents, CodeViewContainer::Pdb));
+  C13Builders.push_back(DebugSubsectionRecordBuilder(SubsectionContents));
 }
index ec3ec55..734e1be 100644 (file)
@@ -187,7 +187,7 @@ toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections,
   std::vector<DebugSubsectionRecordBuilder> Builders;
   uint32_t Size = sizeof(uint32_t);
   for (auto &SS : CVSS) {
-    DebugSubsectionRecordBuilder B(SS, CodeViewContainer::ObjectFile);
+    DebugSubsectionRecordBuilder B(SS);
     Size += B.calculateSerializedLength();
     Builders.push_back(std::move(B));
   }
@@ -197,7 +197,7 @@ toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections,
 
   Err(Writer.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC));
   for (const auto &B : Builders) {
-    Err(B.commit(Writer));
+    Err(B.commit(Writer, CodeViewContainer::ObjectFile));
   }
   return {Output};
 }