From 3ed8fb69d1afeb40c6f2aacdd0502f2184df09f5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 19 Apr 2026 21:02:31 +0000 Subject: [PATCH] perf: reduce small string overhead in converter Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/8c0eb26b-9b94-4eda-b871-1f2b17fe1aa4 Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com> --- cpp2rust/converter/converter.cpp | 13 ++++++------- cpp2rust/converter/converter.h | 12 ++++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 8c27c56a..c7b7a744 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1153,8 +1153,8 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt, } else if (arg_str.contains("Setw")) { fmt_width = ToString(arg); // Delete leading and trailing whitespaces - fmt_width.erase(0, fmt_width.find_first_not_of(" ")); - fmt_width.erase(fmt_width.find_last_not_of(" ") + 1); + fmt_width.erase(0, fmt_width.find_first_not_of(' ')); + fmt_width.erase(fmt_width.find_last_not_of(' ') + 1); } else if (!arg->getType()->isCharType() && Mapper::Map(arg->getType()) != "Vec") { fmt += ("{:" + fmt_width + fmt_trait + "}"); @@ -2962,11 +2962,10 @@ void Converter::ConvertCXXMethodDecls(const clang::CXXRecordDecl *decl, StrCat(token::kCloseCurlyBracket); } -void Converter::ConvertOrdAndPartialOrdTraitsBase(std::string first_branch, - std::string second_branch, - std::string first_return, - std::string second_return, - std::string record_name) { +void Converter::ConvertOrdAndPartialOrdTraitsBase( + std::string_view first_branch, std::string_view second_branch, + std::string_view first_return, std::string_view second_return, + std::string_view record_name) { StrCat(keyword::kImpl, "Ord for ", record_name, "{"); StrCat("fn cmp(&self, other: &Self) -> std::cmp::Ordering {"); StrCat(std::format("{} {{", keyword_unsafe_)); diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 899cc965..620126a3 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -295,7 +295,7 @@ class Converter : public clang::RecursiveASTVisitor { #define StrCat(...) _StrCat(__FUNCTION__, __LINE__, __VA_ARGS__) template - inline void _StrCat(const char *func, int line, Ts... vals) { + inline void _StrCat(const char *func, int line, const Ts &...vals) { llvm::errs() << '[' << func << ':' << line << "] "; ((llvm::errs() << vals << '\n', *rs_code_ += vals, *rs_code_ += ' '), ...); } @@ -389,11 +389,11 @@ class Converter : public clang::RecursiveASTVisitor { virtual void ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl, const clang::FunctionDecl *op); - void ConvertOrdAndPartialOrdTraitsBase(std::string first_branch, - std::string second_branch, - std::string first_return, - std::string second_return, - std::string record_name); + void ConvertOrdAndPartialOrdTraitsBase(std::string_view first_branch, + std::string_view second_branch, + std::string_view first_return, + std::string_view second_return, + std::string_view record_name); virtual void AddCloneTrait(const clang::CXXRecordDecl *decl);