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

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 12 02:31:32 PDT 2026


================
@@ -110,19 +113,37 @@ static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
     OS << L << '\n';
   };
 
-  const char *B = Contents.begin();
-  const char *P = nullptr, *E = nullptr, *S = nullptr;
-  for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
-    if (isPrint(*P) || *P == '\t') {
-      if (S == nullptr)
-        S = P;
-    } else if (S) {
-      print(S - B, StringRef(S, P - S));
-      S = nullptr;
+  Buffer.resize_for_overwrite(sys::fs::DefaultReadChunkSize);
+  std::string StringBuffer;
----------------
jh7370 wrote:

I'm concerned about the performance of copying every character into this string buffer. This feels like it would be unnecessarily time-consuming.

I think we need to do something smarter. We can use the old approach of storing pointers to the strings to be printed as long as we are within an iteration of the while loop. These only become invalidated when we need to perform another read. To handle this case, when you get to the end of a loop iteration, if there are characters that are in the local pointer range, copy those into some kind of local char buffer (`SmallString`, with size corresponding to the initial value of `MinLength` seems like a reasonable option). Then, when `print` is next called, it uses this buffer as a prefix to the rest of the string (so total size for `MinLength` comparison is "current buffer size + pointer range size, then print buffer before pointer range").

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


More information about the llvm-commits mailing list