Skip to content

Commit 9359210

Browse files
Copilotnunoplopes
andauthored
Reduce avoidable string allocations in converter helpers and mapper lookups (#19)
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 2498fbc commit 9359210

4 files changed

Lines changed: 8 additions & 7 deletions

File tree

cpp2rust/converter/converter_lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ clang::QualType GetReturnTypeOfFunction(const clang::CallExpr *expr) {
380380
return {};
381381
}
382382

383-
std::string GetOverloadedOperator(const clang::FunctionDecl *decl) {
383+
const char *GetOverloadedOperator(const clang::FunctionDecl *decl) {
384384
switch (decl->getOverloadedOperator()) {
385385
case clang::OO_Less:
386386
return "lt";

cpp2rust/converter/converter_lib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ template <class T> llvm::SmallString<16> GetNumAsString(const T &num) {
9191

9292
clang::QualType GetReturnTypeOfFunction(const clang::CallExpr *expr);
9393

94-
std::string GetOverloadedOperator(const clang::FunctionDecl *decl);
94+
const char *GetOverloadedOperator(const clang::FunctionDecl *decl);
9595

9696
bool IsOverloadedComparisonOperator(const clang::CXXMethodDecl *decl);
9797

cpp2rust/converter/mapper.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ Map::const_iterator parallel_search(const Map &container,
367367

368368
decltype(exprs_)::const_iterator search(const clang::Expr *expr) {
369369
auto qualified_name = ToString(expr);
370-
auto result = parallel_search(exprs_, [&](std::string tpl) {
370+
auto result = parallel_search(exprs_, [&](const std::string &tpl) {
371371
return matchTemplate(tpl, qualified_name);
372372
});
373373
llvm::errs() << "search expr " << qualified_name << ", result:\n";
@@ -382,7 +382,7 @@ decltype(exprs_)::const_iterator search(const clang::Expr *expr) {
382382
decltype(types_)::const_iterator search(clang::QualType qual_type) {
383383
auto type = ToString(qual_type);
384384
auto result = parallel_search(
385-
types_, [&](std::string tpl) { return matchTemplate(tpl, type); });
385+
types_, [&](const std::string &tpl) { return matchTemplate(tpl, type); });
386386
llvm::errs() << "search type " << type << ", result: "
387387
<< ((result == types_.end()) ? "None"
388388
: result->second.type_info.type)
@@ -531,8 +531,9 @@ clang::QualType normalizeQualType(clang::QualType qual_type) {
531531
}
532532

533533
std::string mapTypeStringRecursive(const std::string &cpp_type) {
534-
auto rule = parallel_search(
535-
types_, [&](std::string tpl) { return matchTemplate(tpl, cpp_type); });
534+
auto rule = parallel_search(types_, [&](const std::string &tpl) {
535+
return matchTemplate(tpl, cpp_type);
536+
});
536537
if (rule == types_.end()) {
537538
llvm::errs() << "cpp_type: " << cpp_type << '\n';
538539
assert(0 && "Type is not present in types_");

cpp2rust/converter/models/converter_refcount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class ConverterRefCount final : public Converter {
196196
FullRefCount,
197197
};
198198

199-
std::string ConversionKindToString(ConversionKind k) {
199+
const char *ConversionKindToString(ConversionKind k) {
200200
switch (k) {
201201
case ConversionKind::Unboxed:
202202
return "Unboxed";

0 commit comments

Comments
 (0)