[Lldb-commits] [clang] [clang-tools-extra] [lld] [lldb] [llvm] Rework the `Option` library to reduce dynamic relocations (PR #119198)

Reid Kleckner via lldb-commits lldb-commits at lists.llvm.org
Mon Dec 9 12:47:50 PST 2024


================
@@ -80,15 +78,56 @@ class OptTable {
     const char *AliasArgs;
     const char *Values;
 
-    StringRef getName() const {
-      unsigned PrefixLength = Prefixes.empty() ? 0 : Prefixes[0].size();
-      return PrefixedName.drop_front(PrefixLength);
+    bool hasNoPrefix() const { return PrefixesOffset == 0; }
+
+    unsigned getNumPrefixes(ArrayRef<unsigned> PrefixesTable) const {
+      return PrefixesTable[PrefixesOffset];
+    }
+
+    ArrayRef<unsigned>
+    getPrefixOffsets(ArrayRef<unsigned> PrefixesTable) const {
+      return hasNoPrefix() ? ArrayRef<unsigned>()
+                           : PrefixesTable.slice(PrefixesOffset + 1,
+                                                 getNumPrefixes(PrefixesTable));
+    }
+
+    void appendPrefixes(const char *StrTable, ArrayRef<unsigned> PrefixesTable,
+                        SmallVectorImpl<StringRef> &Prefixes) const {
+      for (unsigned PrefixOffset : getPrefixOffsets(PrefixesTable))
+        Prefixes.push_back(&StrTable[PrefixOffset]);
+    }
+
+    StringRef getPrefix(const char *StrTable, ArrayRef<unsigned> PrefixesTable,
+                        unsigned PrefixIndex) const {
+      return &StrTable[getPrefixOffsets(PrefixesTable)[PrefixIndex]];
+    }
+
+    StringRef getPrefixedName(const char *StrTable) const {
+      return &StrTable[PrefixedNameOffset];
+    }
+
+    StringRef getName(const char *StrTable,
+                      ArrayRef<unsigned> PrefixesTable) const {
+      unsigned PrefixLength =
+          hasNoPrefix() ? 0 : getPrefix(StrTable, PrefixesTable, 0).size();
+      return getPrefixedName(StrTable).drop_front(PrefixLength);
     }
   };
 
 private:
+  // A unified string table for these options. Individual strings are stored as
+  // null terminated C-strings at offsets within this table.
+  const char *StrTable;
----------------
rnk wrote:

Can this be a size-bearing type like StringRef? The "safe" way to extract a C string at an offset becomes something like:
```
  StrTable.drop_front(PrefixOffset).split('\0').first;
```

https://github.com/llvm/llvm-project/pull/119198


More information about the lldb-commits mailing list