]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Avoid an extra data copy when rewriting stdout
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Oct 2022 08:17:01 +0000 (10:17 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Oct 2022 08:50:10 +0000 (10:50 +0200)
src/ccache.cpp
src/core/MsvcShowIncludesOutput.cpp

index 3086856711e1d9dfc0b0a585c3d98782802e43e0..861bc5b1ef71fcd18f0f0bf897661895466fc6db 100644 (file)
@@ -950,7 +950,7 @@ rewrite_stdout_from_compiler(const Context& ctx, util::Bytes&& stdout_data)
   using Mode = Tokenizer::Mode;
   using IncludeDelimiter = Tokenizer::IncludeDelimiter;
   if (!stdout_data.empty()) {
-    std::string new_stdout_text;
+    util::Bytes new_stdout_data;
     for (const auto line : Tokenizer(util::to_string_view(stdout_data),
                                      "\n",
                                      Mode::include_empty,
@@ -973,12 +973,14 @@ rewrite_stdout_from_compiler(const Context& ctx, util::Bytes&& stdout_data)
           ctx, Util::normalize_concrete_absolute_path(abs_inc_path));
         std::string line_with_rel_inc =
           util::replace_first(orig_line, abs_inc_path, rel_inc_path);
-        new_stdout_text.append(line_with_rel_inc);
+        new_stdout_data.insert(new_stdout_data.begin(),
+                               line_with_rel_inc.data(),
+                               line_with_rel_inc.size());
       } else {
-        new_stdout_text.append(line.data(), line.length());
+        new_stdout_data.insert(new_stdout_data.end(), line.data(), line.size());
       }
     }
-    return util::Bytes(new_stdout_text.data(), new_stdout_text.size());
+    return new_stdout_data;
   } else {
     return std::move(stdout_data);
   }
index 5289eadc6f65dae82bdf2694f0116e1a461df4b9..311a1bcbb811f28a46bfd17628cc67d8745b8249 100644 (file)
@@ -61,16 +61,16 @@ strip_includes(const Context& ctx, util::Bytes&& stdout_data)
     return std::move(stdout_data);
   }
 
-  std::string new_stdout_text;
+  util::Bytes new_stdout_data;
   for (const auto line : Tokenizer(util::to_string_view(stdout_data),
                                    "\n",
                                    Mode::include_empty,
                                    IncludeDelimiter::yes)) {
     if (!util::starts_with(line, ctx.config.msvc_dep_prefix())) {
-      new_stdout_text.append(line.data(), line.length());
+      new_stdout_data.insert(new_stdout_data.end(), line.data(), line.size());
     }
   }
-  return util::Bytes(new_stdout_text.data(), new_stdout_text.size());
+  return new_stdout_data;
 }
 
 } // namespace core::MsvcShowIncludesOutput