[llvm] [llvm-strings] Use small buffer instead of reading whole file (PR #163073)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 00:52:45 PDT 2026


================
@@ -92,31 +93,39 @@ static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
 static void strings(raw_ostream &OS, StringRef FileName,
                     sys::fs::file_t Handle) {
   SmallString<sys::fs::DefaultReadChunkSize> Buffer;
-  auto print = [&OS, FileName](unsigned Offset, StringRef L) {
-    if (L.size() < static_cast<size_t>(MinLength))
-      return;
-    if (PrintFileName)
-      OS << FileName << ": ";
-    switch (Radix) {
-    case none:
-      break;
-    case octal:
-      OS << format("%7o ", Offset);
-      break;
-    case hexadecimal:
-      OS << format("%7x ", Offset);
-      break;
-    case decimal:
-      OS << format("%7u ", Offset);
-      break;
+  SmallString<DefaultMinLength> Prefix;
+  auto print = [&OS, FileName, &Prefix](unsigned Offset, StringRef L) {
+    if (Prefix.size() + L.size() >= static_cast<size_t>(MinLength)) {
+      if (PrintFileName)
+        OS << FileName << ": ";
+      switch (Radix) {
+      case none:
+        break;
+      case octal:
+        OS << format("%7o ", Offset);
+        break;
+      case hexadecimal:
+        OS << format("%7x ", Offset);
+        break;
+      case decimal:
+        OS << format("%7u ", Offset);
+        break;
+      }
+      OS << Prefix << L << '\n';
     }
-    OS << L << '\n';
+    Prefix.clear();
   };
 
   Buffer.resize_for_overwrite(sys::fs::DefaultReadChunkSize);
-  std::string StringBuffer;
+  // Offset of the start of the current chunk within the file.
   unsigned Offset = 0;
   while (true) {
+    /*
+     * llvm-strings should be able to process a very large file on a
+     * memory-budgeted machine. To handle this, we read the file in chunk
+     * instead of allocate a very large memory and copy the whole file to the
+     * memory.
+     */
----------------
jh7370 wrote:

Use C++ style comments, even for longer comments. Also some wording tweaks:
"in chunk" -> "in chunks"
"allocate a very ... to the memory." -> "copying the whole file into memory." (It's implicit that when you copy something into memory, you allocate the space for it).

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


More information about the llvm-commits mailing list