Skip to content

Commit eb9e570

Browse files
Copilotnunoplopes
andauthored
Reduce converter string overhead in hot paths (#17)
This PR applies targeted micro-optimizations in the converter to reduce avoidable allocations/copies and use cheaper character APIs where applicable. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Nuno Lopes <nuno.lopes@tecnico.ulisboa.pt>
1 parent 959af94 commit eb9e570

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,8 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
11531153
} else if (arg_str.contains("Setw")) {
11541154
fmt_width = ToString(arg);
11551155
// Delete leading and trailing whitespaces
1156-
fmt_width.erase(0, fmt_width.find_first_not_of(" "));
1157-
fmt_width.erase(fmt_width.find_last_not_of(" ") + 1);
1156+
fmt_width.erase(0, fmt_width.find_first_not_of(' '));
1157+
fmt_width.erase(fmt_width.find_last_not_of(' ') + 1);
11581158
} else if (!arg->getType()->isCharType() &&
11591159
Mapper::Map(arg->getType()) != "Vec<u8>") {
11601160
fmt += ("{:" + fmt_width + fmt_trait + "}");
@@ -2962,11 +2962,10 @@ void Converter::ConvertCXXMethodDecls(const clang::CXXRecordDecl *decl,
29622962
StrCat(token::kCloseCurlyBracket);
29632963
}
29642964

2965-
void Converter::ConvertOrdAndPartialOrdTraitsBase(std::string first_branch,
2966-
std::string second_branch,
2967-
std::string first_return,
2968-
std::string second_return,
2969-
std::string record_name) {
2965+
void Converter::ConvertOrdAndPartialOrdTraitsBase(
2966+
std::string_view first_branch, std::string_view second_branch,
2967+
std::string_view first_return, std::string_view second_return,
2968+
std::string_view record_name) {
29702969
StrCat(keyword::kImpl, "Ord for ", record_name, "{");
29712970
StrCat("fn cmp(&self, other: &Self) -> std::cmp::Ordering {");
29722971
StrCat(std::format("{} {{", keyword_unsafe_));

cpp2rust/converter/converter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
295295
#define StrCat(...) _StrCat(__FUNCTION__, __LINE__, __VA_ARGS__)
296296

297297
template <typename... Ts>
298-
inline void _StrCat(const char *func, int line, Ts... vals) {
298+
inline void _StrCat(const char *func, int line, const Ts &...vals) {
299299
llvm::errs() << '[' << func << ':' << line << "] ";
300300
((llvm::errs() << vals << '\n', *rs_code_ += vals, *rs_code_ += ' '), ...);
301301
}
@@ -389,11 +389,11 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
389389
virtual void ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl,
390390
const clang::FunctionDecl *op);
391391

392-
void ConvertOrdAndPartialOrdTraitsBase(std::string first_branch,
393-
std::string second_branch,
394-
std::string first_return,
395-
std::string second_return,
396-
std::string record_name);
392+
void ConvertOrdAndPartialOrdTraitsBase(std::string_view first_branch,
393+
std::string_view second_branch,
394+
std::string_view first_return,
395+
std::string_view second_return,
396+
std::string_view record_name);
397397

398398
virtual void AddCloneTrait(const clang::CXXRecordDecl *decl);
399399

0 commit comments

Comments
 (0)