Просмотр исходного кода

Some minor efficiency fixes. (#3835)

This doesn't move the needle too much, but cleans some things up. It let
me see an underlying issue that I'll try to fix next.
Chandler Carruth 2 лет назад
Родитель
Сommit
d7fb1b287d
2 измененных файлов с 18 добавлено и 7 удалено
  1. 17 6
      testing/file_test/autoupdate.cpp
  2. 1 1
      testing/file_test/autoupdate.h

+ 17 - 6
testing/file_test/autoupdate.cpp

@@ -103,7 +103,7 @@ auto FileTestAutoupdater::CheckLine::RemapLineNumbers(
 }
 }
 
 
 auto FileTestAutoupdater::GetFileAndLineNumber(
 auto FileTestAutoupdater::GetFileAndLineNumber(
-    llvm::DenseMap<llvm::StringRef, int> file_to_number_map,
+    const llvm::DenseMap<llvm::StringRef, int>& file_to_number_map,
     int default_file_number, const std::string& check_line)
     int default_file_number, const std::string& check_line)
     -> FileAndLineNumber {
     -> FileAndLineNumber {
   for (const auto& replacement : line_number_replacements_) {
   for (const auto& replacement : line_number_replacements_) {
@@ -157,18 +157,29 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output,
   }
   }
 
 
   // `{{` and `[[` are escaped as a regex matcher.
   // `{{` and `[[` are escaped as a regex matcher.
-  RE2 double_brace_re(R"(\{\{)");
-  RE2 double_square_bracket_re(R"(\[\[)");
+  static RE2 double_brace_re(R"(\{\{)");
+  static RE2 double_square_bracket_re(R"(\[\[)");
   // End-of-line whitespace is replaced with a regex matcher to make it visible.
   // End-of-line whitespace is replaced with a regex matcher to make it visible.
-  RE2 end_of_line_whitespace_re(R"((\s+)$)");
+  static RE2 end_of_line_whitespace_re(R"((\s+)$)");
 
 
   // The default file number for when no specific file is found.
   // The default file number for when no specific file is found.
   int default_file_number = 0;
   int default_file_number = 0;
 
 
   llvm::SmallVector<CheckLine> check_lines;
   llvm::SmallVector<CheckLine> check_lines;
   for (const auto& line : lines) {
   for (const auto& line : lines) {
-    std::string check_line = llvm::formatv("// CHECK:{0}:{1}{2}", label,
-                                           line.empty() ? "" : " ", line);
+    // This code is relatively hot in our testing, and because when testing it
+    // isn't run with an optimizer we benefit from making it use simple
+    // constructs. For this reason, we avoid `llvm::formatv` and similar tools.
+    std::string check_line;
+    check_line.reserve(line.size() + strlen(label) + strlen("// CHECK:: "));
+    check_line.append("// CHECK:");
+    check_line.append(label);
+    check_line.append(":");
+    if (!line.empty()) {
+      check_line.append(" ");
+      check_line.append(line);
+    }
+
     RE2::Replace(&check_line, double_brace_re, R"({{\\{\\{}})");
     RE2::Replace(&check_line, double_brace_re, R"({{\\{\\{}})");
     RE2::Replace(&check_line, double_square_bracket_re, R"({{\\[\\[}})");
     RE2::Replace(&check_line, double_square_bracket_re, R"({{\\[\\[}})");
     RE2::Replace(&check_line, end_of_line_whitespace_re, R"({{\1}})");
     RE2::Replace(&check_line, end_of_line_whitespace_re, R"({{\1}})");

+ 1 - 1
testing/file_test/autoupdate.h

@@ -139,7 +139,7 @@ class FileTestAutoupdater {
   // Looks for the patterns in the line. Returns the first match, or defaulted
   // Looks for the patterns in the line. Returns the first match, or defaulted
   // information if not found.
   // information if not found.
   auto GetFileAndLineNumber(
   auto GetFileAndLineNumber(
-      llvm::DenseMap<llvm::StringRef, int> file_to_number_map,
+      const llvm::DenseMap<llvm::StringRef, int>& file_to_number_map,
       int default_file_number, const std::string& check_line)
       int default_file_number, const std::string& check_line)
       -> FileAndLineNumber;
       -> FileAndLineNumber;