[llvm] r292664 - [pdb] Merge NamedStreamMapBuilder and NamedStreamMap.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 20 14:41:40 PST 2017


Author: zturner
Date: Fri Jan 20 16:41:40 2017
New Revision: 292664

URL: http://llvm.org/viewvc/llvm-project?rev=292664&view=rev
Log:
[pdb] Merge NamedStreamMapBuilder and NamedStreamMap.

While the builder pattern has proven useful for certain other
larger types, in this case it was hampering the ability to use
the data structure, as for runtime access we need a map that
we can efficiently read from and write to.  So the two are merged
into a single data structure that can efficiently be read to,
written from, deserialized from bytes, and serialized to bytes.

Removed:
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMapBuilder.h
    llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMapBuilder.cpp
Modified:
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/HashTable.h
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h
    llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMap.h
    llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt
    llvm/trunk/lib/DebugInfo/PDB/Raw/HashTable.cpp
    llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStream.cpp
    llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStreamBuilder.cpp
    llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMap.cpp
    llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp

Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/HashTable.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/HashTable.h?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/HashTable.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/HashTable.h Fri Jan 20 16:41:40 2017
@@ -47,6 +47,8 @@ public:
   uint32_t calculateSerializedLength() const;
   Error commit(msf::StreamWriter &Writer) const;
 
+  void clear();
+
   uint32_t capacity() const;
   uint32_t size() const;
 

Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h Fri Jan 20 16:41:40 2017
@@ -14,7 +14,7 @@
 #include "llvm/Support/Error.h"
 
 #include "llvm/DebugInfo/PDB/PDBTypes.h"
-#include "llvm/DebugInfo/PDB/Raw/NamedStreamMapBuilder.h"
+#include "llvm/DebugInfo/PDB/Raw/NamedStreamMap.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
 #include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
 
@@ -37,9 +37,9 @@ public:
   void setAge(uint32_t A);
   void setGuid(PDB_UniqueId G);
 
-  NamedStreamMapBuilder &getNamedStreamsBuilder();
+  NamedStreamMap &getNamedStreamsBuilder();
 
-  uint32_t calculateSerializedLength() const;
+  uint32_t finalize();
 
   Error finalizeMsfLayout();
 
@@ -54,7 +54,7 @@ private:
   uint32_t Age;
   PDB_UniqueId Guid;
 
-  NamedStreamMapBuilder NamedStreams;
+  NamedStreamMap NamedStreams;
 };
 }
 }

Modified: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMap.h?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMap.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMap.h Fri Jan 20 16:41:40 2017
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/DebugInfo/PDB/Raw/HashTable.h"
 #include "llvm/Support/Error.h"
 #include <cstdint>
 
@@ -23,18 +24,28 @@ class StreamWriter;
 namespace pdb {
 class NamedStreamMapBuilder;
 class NamedStreamMap {
+  struct FinalizationInfo {
+    uint32_t StringDataBytes = 0;
+    uint32_t SerializedLength = 0;
+  };
   friend NamedStreamMapBuilder;
 
 public:
   NamedStreamMap();
 
   Error load(msf::StreamReader &Stream);
+  Error commit(msf::StreamWriter &Writer) const;
+  uint32_t finalize();
 
-  bool tryGetValue(StringRef Name, uint32_t &Value) const;
+  bool get(StringRef Stream, uint32_t &StreamNo) const;
+  void set(StringRef Stream, uint32_t StreamNo);
+  void remove(StringRef Stream);
 
   iterator_range<StringMapConstIterator<uint32_t>> entries() const;
 
 private:
+  Optional<FinalizationInfo> FinalizedInfo;
+  HashTable FinalizedHashTable;
   StringMap<uint32_t> Mapping;
 };
 

Removed: llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMapBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMapBuilder.h?rev=292663&view=auto
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMapBuilder.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/Raw/NamedStreamMapBuilder.h (removed)
@@ -1,45 +0,0 @@
-//===- NamedStreamMapBuilder.h - PDB Named Stream Map Builder ---*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_DEBUGINFO_PDB_RAW_PDBNAMEDSTREAMMAPBUILDER_H
-#define LLVM_DEBUGINFO_PDB_RAW_PDBNAMEDSTREAMMAPBUILDER_H
-
-#include "llvm/DebugInfo/PDB/Raw/HashTable.h"
-#include "llvm/Support/Error.h"
-
-#include <cstdint>
-#include <memory>
-#include <vector>
-
-namespace llvm {
-namespace msf {
-class StreamWriter;
-}
-namespace pdb {
-
-class NamedStreamMapBuilder {
-public:
-  NamedStreamMapBuilder();
-
-  void addMapping(StringRef Name, uint32_t Mapping);
-
-  Error commit(msf::StreamWriter &Writer) const;
-
-  uint32_t calculateSerializedLength() const;
-
-private:
-  std::vector<StringRef> Strings;
-  HashTable Map;
-  uint32_t Offset = 0;
-};
-
-} // end namespace pdb
-} // end namespace llvm
-
-#endif // LLVM_DEBUGINFO_PDB_RAW_PDBNAMEMAPBUILDER_H

Modified: llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt (original)
+++ llvm/trunk/lib/DebugInfo/PDB/CMakeLists.txt Fri Jan 20 16:41:40 2017
@@ -40,7 +40,6 @@ add_pdb_impl_folder(Raw
   Raw/ModInfo.cpp
   Raw/ModStream.cpp
   Raw/NamedStreamMap.cpp
-  Raw/NamedStreamMapBuilder.cpp
   Raw/PDBFile.cpp
   Raw/PDBFileBuilder.cpp
   Raw/PublicsStream.cpp

Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/HashTable.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/HashTable.cpp?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/HashTable.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/HashTable.cpp Fri Jan 20 16:41:40 2017
@@ -99,6 +99,12 @@ Error HashTable::commit(msf::StreamWrite
   return Error::success();
 }
 
+void HashTable::clear() {
+  Buckets.resize(8);
+  Present.clear();
+  Deleted.clear();
+}
+
 uint32_t HashTable::capacity() const { return Buckets.size(); }
 uint32_t HashTable::size() const { return Present.count(); }
 

Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStream.cpp?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStream.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStream.cpp Fri Jan 20 16:41:40 2017
@@ -56,7 +56,7 @@ Error InfoStream::reload() {
 
 uint32_t InfoStream::getNamedStreamIndex(llvm::StringRef Name) const {
   uint32_t Result;
-  if (!NamedStreams.tryGetValue(Name, Result))
+  if (!NamedStreams.get(Name, Result))
     return 0;
   return Result;
 }

Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStreamBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStreamBuilder.cpp?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStreamBuilder.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/InfoStreamBuilder.cpp Fri Jan 20 16:41:40 2017
@@ -32,16 +32,12 @@ void InfoStreamBuilder::setAge(uint32_t
 
 void InfoStreamBuilder::setGuid(PDB_UniqueId G) { Guid = G; }
 
-NamedStreamMapBuilder &InfoStreamBuilder::getNamedStreamsBuilder() {
+NamedStreamMap &InfoStreamBuilder::getNamedStreamsBuilder() {
   return NamedStreams;
 }
 
-uint32_t InfoStreamBuilder::calculateSerializedLength() const {
-  return sizeof(InfoStreamHeader) + NamedStreams.calculateSerializedLength();
-}
-
 Error InfoStreamBuilder::finalizeMsfLayout() {
-  uint32_t Length = calculateSerializedLength();
+  uint32_t Length = sizeof(InfoStreamHeader) + NamedStreams.finalize();
   if (auto EC = Msf.setStreamSize(StreamPDB, Length))
     return EC;
   return Error::success();

Modified: llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMap.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMap.cpp?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMap.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMap.cpp Fri Jan 20 16:41:40 2017
@@ -27,6 +27,10 @@ using namespace llvm::pdb;
 NamedStreamMap::NamedStreamMap() = default;
 
 Error NamedStreamMap::load(StreamReader &Stream) {
+  Mapping.clear();
+  FinalizedHashTable.clear();
+  FinalizedInfo.reset();
+
   uint32_t StringBufferSize;
   if (auto EC = Stream.readInteger(StringBufferSize))
     return joinErrors(std::move(EC),
@@ -63,16 +67,69 @@ Error NamedStreamMap::load(StreamReader
   return Error::success();
 }
 
+Error NamedStreamMap::commit(msf::StreamWriter &Writer) const {
+  assert(FinalizedInfo.hasValue());
+
+  // The first field is the number of bytes of string data.
+  if (auto EC = Writer.writeInteger(
+          FinalizedInfo->StringDataBytes)) // Number of bytes of string data
+    return EC;
+
+  // Now all of the string data itself.
+  for (const auto &Item : Mapping) {
+    if (auto EC = Writer.writeZeroString(Item.getKey()))
+      return EC;
+  }
+
+  // And finally the Offset Index map.
+  if (auto EC = FinalizedHashTable.commit(Writer))
+    return EC;
+
+  return Error::success();
+}
+
+uint32_t NamedStreamMap::finalize() {
+  if (FinalizedInfo.hasValue())
+    return FinalizedInfo->SerializedLength;
+
+  // Build the finalized hash table.
+  FinalizedHashTable.clear();
+  FinalizedInfo.emplace();
+  for (const auto &Item : Mapping) {
+    FinalizedHashTable.set(FinalizedInfo->StringDataBytes, Item.getValue());
+    FinalizedInfo->StringDataBytes += Item.getKeyLength() + 1;
+  }
+
+  // Number of bytes of string data.
+  FinalizedInfo->SerializedLength += sizeof(support::ulittle32_t);
+  // Followed by that many actual bytes of string data.
+  FinalizedInfo->SerializedLength += FinalizedInfo->StringDataBytes;
+  // Followed by the mapping from Offset to Index.
+  FinalizedInfo->SerializedLength +=
+      FinalizedHashTable.calculateSerializedLength();
+  return FinalizedInfo->SerializedLength;
+}
+
 iterator_range<StringMapConstIterator<uint32_t>>
 NamedStreamMap::entries() const {
   return make_range<StringMapConstIterator<uint32_t>>(Mapping.begin(),
                                                       Mapping.end());
 }
 
-bool NamedStreamMap::tryGetValue(StringRef Name, uint32_t &Value) const {
-  auto Iter = Mapping.find(Name);
+bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
+  auto Iter = Mapping.find(Stream);
   if (Iter == Mapping.end())
     return false;
-  Value = Iter->second;
+  StreamNo = Iter->second;
   return true;
 }
+
+void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
+  FinalizedInfo.reset();
+  Mapping[Stream] = StreamNo;
+}
+
+void NamedStreamMap::remove(StringRef Stream) {
+  FinalizedInfo.reset();
+  Mapping.erase(Stream);
+}

Removed: llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMapBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMapBuilder.cpp?rev=292663&view=auto
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMapBuilder.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Raw/NamedStreamMapBuilder.cpp (removed)
@@ -1,60 +0,0 @@
-//===- NamedStreamMapBuilder.cpp - PDB Named Stream Map Builder -*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/DebugInfo/PDB/Raw/NamedStreamMapBuilder.h"
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/DebugInfo/MSF/StreamWriter.h"
-#include "llvm/DebugInfo/PDB/Raw/NamedStreamMap.h"
-#include "llvm/Support/Endian.h"
-#include "llvm/Support/Error.h"
-#include <algorithm>
-#include <cstdint>
-
-using namespace llvm;
-using namespace llvm::pdb;
-
-NamedStreamMapBuilder::NamedStreamMapBuilder() = default;
-
-void NamedStreamMapBuilder::addMapping(StringRef Name, uint32_t Mapping) {
-  Strings.push_back(Name);
-  Map.set(Offset, Mapping);
-  Offset += Name.size() + 1;
-}
-
-uint32_t NamedStreamMapBuilder::calculateSerializedLength() const {
-  uint32_t TotalLength = 0;
-
-  // Number of bytes of string data.
-  TotalLength += sizeof(support::ulittle32_t);
-  // Followed by that many actual bytes of string data.
-  TotalLength += Offset;
-  // Followed by the mapping from Name to Index.
-  TotalLength += Map.calculateSerializedLength();
-
-  return TotalLength;
-}
-
-Error NamedStreamMapBuilder::commit(msf::StreamWriter &Writer) const {
-  // The first field is the number of bytes of string data.  We've already been
-  // keeping a running total of this in `Offset`.
-  if (auto EC = Writer.writeInteger(Offset)) // Number of bytes of string data
-    return EC;
-
-  // Now all of the string data itself.
-  for (auto S : Strings) {
-    if (auto EC = Writer.writeZeroString(S))
-      return EC;
-  }
-
-  // And finally the Linear Map.
-  if (auto EC = Map.commit(Writer))
-    return EC;
-
-  return Error::success();
-}

Modified: llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp?rev=292664&r1=292663&r2=292664&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp (original)
+++ llvm/trunk/tools/llvm-pdbdump/llvm-pdbdump.cpp Fri Jan 20 16:41:40 2017
@@ -352,8 +352,7 @@ static void yamlToPdb(StringRef Path) {
     InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
     InfoBuilder.setVersion(YamlObj.PdbStream->Version);
     for (auto &NM : YamlObj.PdbStream->NamedStreams)
-      InfoBuilder.getNamedStreamsBuilder().addMapping(NM.StreamName,
-                                                      NM.StreamNumber);
+      InfoBuilder.getNamedStreamsBuilder().set(NM.StreamName, NM.StreamNumber);
   }
 
   if (YamlObj.DbiStream.hasValue()) {




More information about the llvm-commits mailing list