Skip to content

Commit 5bdf6a1

Browse files
Copilotnunoplopes
andauthored
Avoid unnecessary heap allocations for short-lived strings (#41)
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 dde10a7 commit 5bdf6a1

3 files changed

Lines changed: 11 additions & 10 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,7 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
21362136
return std::format("{}::{}",
21372137
GetRecordName(clang::dyn_cast<clang::EnumDecl>(
21382138
enum_constant->getDeclContext())),
2139-
enum_constant->getName().str());
2139+
std::string_view(enum_constant->getName()));
21402140
} else if (IsGlobalVar(expr)) {
21412141
return ReplaceAll(Mapper::ToString(expr->getDecl()), "::", "_");
21422142
}
@@ -2612,12 +2612,12 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
26122612
for (auto e : decl->enumerators()) {
26132613
llvm::SmallVector<char, 32> init;
26142614
e->getInitVal().toString(init, 10);
2615-
std::string init_str(init.begin(), init.end());
26162615
if (first_enumerator) {
26172616
StrCat("#[default]");
26182617
first_enumerator = false;
26192618
}
2620-
StrCat(std::format("{} = {},", e->getNameAsString(), init_str));
2619+
StrCat(std::format("{} = {},", std::string_view(e->getName()),
2620+
std::string_view(init.data(), init.size())));
26212621
}
26222622
StrCat("}");
26232623
return false;
@@ -2854,7 +2854,7 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) {
28542854

28552855
std::string
28562856
Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
2857-
std::string name(decl->getNameAsString());
2857+
auto name = decl->getNameAsString();
28582858

28592859
if (decl->getNumParams() != 0U) {
28602860
name += '_';

cpp2rust/converter/mapper.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void addBuiltinTypes(Model model) {
443443
auto build_rust_type = [&](clang::QualType qt) {
444444
unsigned bits = ctx_->getTypeSize(qt);
445445
char sign = qt->isSignedIntegerType() ? 'i' : 'u';
446-
return std::string(1, sign) + std::to_string(bits);
446+
return std::format("{}{}", sign, bits);
447447
};
448448

449449
// Misc
@@ -858,10 +858,11 @@ std::string ToString(const clang::Expr *expr) {
858858
}
859859

860860
if (const auto *uop = llvm::dyn_cast<clang::UnaryOperator>(expr)) {
861-
std::string opcode =
862-
clang::UnaryOperator::getOpcodeStr(uop->getOpcode()).str();
863-
return uop->isPostfix() ? ToString(uop->getSubExpr()) + std::move(opcode)
864-
: std::move(opcode) + ToString(uop->getSubExpr());
861+
auto sub = ToString(uop->getSubExpr());
862+
std::string_view opcode =
863+
clang::UnaryOperator::getOpcodeStr(uop->getOpcode());
864+
return uop->isPostfix() ? std::format("{}{}", sub, opcode)
865+
: std::format("{}{}", opcode, sub);
865866
}
866867

867868
return "Unhandled case in ToString";

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ void ConverterRefCount::ConvertGenericBinaryOperator(
17571757
clang::BinaryOperator *expr) {
17581758
auto lhs = expr->getLHS();
17591759
auto rhs = expr->getRHS();
1760-
auto opcode = expr->getOpcodeStr().str();
1760+
std::string_view opcode = expr->getOpcodeStr();
17611761

17621762
auto lhs_vars = GetAllVars(lhs);
17631763
auto rhs_vars = GetAllVars(rhs);

0 commit comments

Comments
 (0)