Skip to content

Commit d0d6bc5

Browse files
Copilotnunoplopes
andauthored
fix: eliminate unnecessary string copies in EmitDeref and GetOrMaterialize
- Change EmitDeref's `inner` parameter from `std::string` to `std::string_view` to avoid copying a string that is only ever read via StrCat. Update the three call sites accordingly (two std::move calls become plain refs). - Change GetOrMaterialize's `materialize_fn` parameter from by-value to `const std::function<...>&` to avoid copying the callable object on each call. Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/a4a77a6f-20a8-49a8-98d1-507851373647 Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent c0195b8 commit d0d6bc5

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,7 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
23212321

23222322
if (decl->getType()->getAs<clang::ReferenceType>() && !isAddrOf() &&
23232323
!map_iter_decls_.contains(clang::dyn_cast<clang::VarDecl>(decl))) {
2324-
EmitDeref(std::move(str), decl->getType().getNonReferenceType());
2324+
EmitDeref(str, decl->getType().getNonReferenceType());
23252325
SetValueFreshness(expr->getType());
23262326
return false;
23272327
}
@@ -2464,7 +2464,7 @@ bool Converter::VisitMemberExpr(clang::MemberExpr *expr) {
24642464
}
24652465

24662466
if (!isAddrOf() && member->getType()->isReferenceType()) {
2467-
EmitDeref(std::move(str), member->getType().getNonReferenceType());
2467+
EmitDeref(str, member->getType().getNonReferenceType());
24682468
return false;
24692469
}
24702470

@@ -3561,14 +3561,14 @@ void Converter::ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) {
35613561
}
35623562
}
35633563

3564-
void Converter::EmitDeref(std::string inner, clang::QualType pointee_type) {
3564+
void Converter::EmitDeref(std::string_view inner, clang::QualType pointee_type) {
35653565
auto wrap = std::exchange(autoref_mut_, std::nullopt);
35663566
PushParen outer(*this, wrap.has_value());
35673567
if (wrap) {
35683568
StrCat(*wrap ? "&mut" : "&");
35693569
}
35703570
PushParen paren(*this);
3571-
StrCat(GetPointerDerefPrefix(pointee_type), std::move(inner));
3571+
StrCat(GetPointerDerefPrefix(pointee_type), inner);
35723572
}
35733573

35743574
void Converter::ConvertDeref(clang::Expr *expr) {
@@ -3605,9 +3605,9 @@ Converter::CollectPrvalueToLRefArgs(clang::CallExpr *expr) {
36053605

36063606
const std::string &Converter::TempMaterializationCtx::GetOrMaterialize(
36073607
unsigned argument_num,
3608-
std::function<std::pair<std::string, std::string>(const std::string &,
3609-
clang::QualType)>
3610-
materialize_fn) {
3608+
const std::function<std::pair<std::string, std::string>(const std::string &,
3609+
clang::QualType)>
3610+
&materialize_fn) {
36113611
auto &str = materialized_refs_.at(argument_num);
36123612
if (!str.empty()) {
36133613
return str;

cpp2rust/converter/converter.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
168168

169169
const std::string &GetOrMaterialize(
170170
unsigned argument_num,
171-
std::function<std::pair<std::string, std::string>(const std::string &,
172-
clang::QualType)>
173-
materialize_fn);
171+
const std::function<std::pair<std::string, std::string>(
172+
const std::string &, clang::QualType)> &materialize_fn);
174173

175174
private:
176175
std::vector<std::string> materialized_refs_;
@@ -476,7 +475,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
476475

477476
virtual void ConvertDeref(clang::Expr *expr);
478477

479-
void EmitDeref(std::string inner, clang::QualType pointee_type);
478+
void EmitDeref(std::string_view inner, clang::QualType pointee_type);
480479

481480
virtual void ConvertArrow(clang::Expr *expr);
482481

0 commit comments

Comments
 (0)