Skip to content

Commit ecd28d2

Browse files
Copilotnunoplopes
andauthored
Optimize unnecessary object duplication: avoid regex recompilation, string copies
Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/67031e6a-ec2f-4f6c-a534-ac63f300898d Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent 9a3bdee commit ecd28d2

6 files changed

Lines changed: 37 additions & 28 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <llvm/Support/ConvertUTF.h>
1010

1111
#include <format>
12-
#include <regex>
1312

1413
#include "compiler.h"
1514
#include "converter/converter_lib.h"
@@ -381,7 +380,7 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
381380
}
382381

383382
if (decl->isFileVarDecl()) {
384-
name = std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_");
383+
name = ReplaceAll(Mapper::ToString(decl), "::", "_");
385384
if ((decl->isExternallyDeclarable() && !decl->hasInit()) ||
386385
!globals_.insert(name).second) {
387386
return false;
@@ -2133,10 +2132,9 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
21332132
return std::format("{}::{}",
21342133
GetRecordName(clang::dyn_cast<clang::EnumDecl>(
21352134
enum_constant->getDeclContext())),
2136-
enum_constant->getName().str());
2135+
std::string_view(enum_constant->getName()));
21372136
} else if (IsGlobalVar(expr)) {
2138-
return std::regex_replace(Mapper::ToString(expr->getDecl()),
2139-
std::regex("::"), "_");
2137+
return ReplaceAll(Mapper::ToString(expr->getDecl()), "::", "_");
21402138
}
21412139

21422140
return GetNamedDeclAsString(decl);
@@ -2610,12 +2608,12 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
26102608
for (auto e : decl->enumerators()) {
26112609
llvm::SmallVector<char, 32> init;
26122610
e->getInitVal().toString(init, 10);
2613-
std::string init_str(init.begin(), init.end());
26142611
if (first_enumerator) {
26152612
StrCat("#[default]");
26162613
first_enumerator = false;
26172614
}
2618-
StrCat(std::format("{} = {},", e->getNameAsString(), init_str));
2615+
StrCat(std::format("{} = {},", std::string_view(e->getName()),
2616+
std::string_view(init.data(), init.size())));
26192617
}
26202618
StrCat("}");
26212619
return false;
@@ -2852,7 +2850,7 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) {
28522850

28532851
std::string
28542852
Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
2855-
std::string name(decl->getNameAsString());
2853+
auto name = decl->getNameAsString();
28562854

28572855
if (decl->getNumParams() != 0U) {
28582856
name += '_';
@@ -2884,7 +2882,7 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
28842882
if (auto it = inner_structs_.find(ID); it != inner_structs_.end()) {
28852883
return it->second;
28862884
}
2887-
return std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_");
2885+
return ReplaceAll(Mapper::ToString(decl), "::", "_");
28882886
}
28892887

28902888
std::vector<const char *>

cpp2rust/converter/converter_lib.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,4 +762,13 @@ void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix) {
762762
}
763763
}
764764

765+
std::string ReplaceAll(std::string str, std::string_view from,
766+
std::string_view to) {
767+
for (size_t pos = 0; (pos = str.find(from, pos)) != std::string::npos;
768+
pos += to.size()) {
769+
str.replace(pos, from.size(), to);
770+
}
771+
return str;
772+
}
773+
765774
} // namespace cpp2rust

cpp2rust/converter/converter_lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,7 @@ std::vector<clang::Stmt *> GetSwitchCaseBody(clang::CompoundStmt *body,
167167

168168
void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix);
169169

170+
std::string ReplaceAll(std::string str, std::string_view from,
171+
std::string_view to);
172+
170173
} // namespace cpp2rust

cpp2rust/converter/mapper.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ void addBuiltinTypes(Model model) {
447447
auto build_rust_type = [&](clang::QualType qt) {
448448
unsigned bits = ctx_->getTypeSize(qt);
449449
char sign = qt->isSignedIntegerType() ? 'i' : 'u';
450-
return std::string(1, sign) + std::to_string(bits);
450+
return std::format("{}{}", sign, bits);
451451
};
452452

453453
// Misc
@@ -547,13 +547,14 @@ std::string mapTypeStringRecursive(const std::string &cpp_type) {
547547
}
548548

549549
std::string normalizeTranslationRule(std::string rule) {
550-
const std::array<std::pair<std::regex, std::string>, 2> normalization_rules{{
551-
// Dettach pointer from double reference. Useful for matching translation
552-
// rules.
553-
{std::regex(R"(\*\&\&)"), "* &&"},
554-
// Ignore constant template parameters, i.e. replace them with _.
555-
{std::regex(R"(\b\d+\b)"), "_"},
556-
}};
550+
static const std::array<std::pair<std::regex, std::string>, 2>
551+
normalization_rules{{
552+
// Dettach pointer from double reference. Useful for matching
553+
// translation rules.
554+
{std::regex(R"(\*\&\&)"), "* &&"},
555+
// Ignore constant template parameters, i.e. replace them with _.
556+
{std::regex(R"(\b\d+\b)"), "_"},
557+
}};
557558

558559
for (const auto &r : normalization_rules) {
559560
rule = std::regex_replace(rule, r.first, r.second);
@@ -567,7 +568,7 @@ static std::string synthesizeAnonRecordName(const clang::RecordDecl *record) {
567568
if (auto *parent =
568569
clang::dyn_cast<clang::RecordDecl>(record->getDeclContext())) {
569570
parent_name = parent->getIdentifier()
570-
? parent->getIdentifier()->getName().str()
571+
? std::string(parent->getIdentifier()->getName())
571572
: synthesizeAnonRecordName(parent);
572573
parent_name += '_';
573574
}
@@ -674,7 +675,7 @@ bool ParamIsPointer(const clang::Expr *expr, unsigned index) {
674675

675676
void AddRuleForUserDefinedType(clang::NamedDecl *decl) {
676677
auto cpp_name = ToString(decl);
677-
auto rs_name = std::regex_replace(cpp_name, std::regex("::"), "_");
678+
auto rs_name = ReplaceAll(cpp_name, "::", "_");
678679

679680
if (types_.contains(cpp_name)) {
680681
return;
@@ -863,10 +864,11 @@ std::string ToString(const clang::Expr *expr) {
863864
}
864865

865866
if (const auto *uop = llvm::dyn_cast<clang::UnaryOperator>(expr)) {
866-
std::string opcode =
867-
clang::UnaryOperator::getOpcodeStr(uop->getOpcode()).str();
868-
return uop->isPostfix() ? ToString(uop->getSubExpr()) + std::move(opcode)
869-
: std::move(opcode) + ToString(uop->getSubExpr());
867+
auto sub = ToString(uop->getSubExpr());
868+
std::string_view opcode =
869+
clang::UnaryOperator::getOpcodeStr(uop->getOpcode());
870+
return uop->isPostfix() ? std::format("{}{}", sub, opcode)
871+
: std::format("{}{}", opcode, sub);
870872
}
871873

872874
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
@@ -1754,7 +1754,7 @@ void ConverterRefCount::ConvertGenericBinaryOperator(
17541754
clang::BinaryOperator *expr) {
17551755
auto lhs = expr->getLHS();
17561756
auto rhs = expr->getRHS();
1757-
auto opcode = expr->getOpcodeStr().str();
1757+
std::string_view opcode = expr->getOpcodeStr();
17581758

17591759
auto lhs_vars = GetAllVars(lhs);
17601760
auto rhs_vars = GetAllVars(rhs);

cpp2rust/converter/plugins/emplace_back.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include <clang/Sema/Initialization.h>
55

6-
#include <regex>
7-
86
#include "converter/converter_lib.h"
97
#include "converter/mapper.h"
108
#include "converter/models/converter_refcount.h"
@@ -139,8 +137,7 @@ clang::CXXConstructExpr *buildConstructExpr(clang::CXXMemberCallExpr *call,
139137
void Converter::emplace_back_emit_push_open(clang::CXXMemberCallExpr *call) {
140138
{
141139
PushExprKind push(*this, ExprKind::LValue);
142-
StrCat(std::regex_replace(ToString(call->getCallee()),
143-
std::regex("emplace_back"), "push"));
140+
StrCat(ReplaceAll(ToString(call->getCallee()), "emplace_back", "push"));
144141
}
145142
StrCat("(");
146143
}

0 commit comments

Comments
 (0)