From 3ec0d4ec961cb0c67ff1603b83a6026d9f3abbb5 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 16 Oct 2022 10:17:01 +0200 Subject: [PATCH] refactor: Avoid an extra data copy when rewriting stdout --- src/ccache.cpp | 10 ++++++---- src/core/MsvcShowIncludesOutput.cpp | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/ccache.cpp b/src/ccache.cpp index 308685671..861bc5b1e 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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); } diff --git a/src/core/MsvcShowIncludesOutput.cpp b/src/core/MsvcShowIncludesOutput.cpp index 5289eadc6..311a1bcbb 100644 --- a/src/core/MsvcShowIncludesOutput.cpp +++ b/src/core/MsvcShowIncludesOutput.cpp @@ -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 -- 2.47.2