Skip to content

Commit dde10a7

Browse files
Copilotnunoplopes
andauthored
Eliminate double-lookups in map/set operations (#39)
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 4bce2c7 commit dde10a7

2 files changed

Lines changed: 9 additions & 15 deletions

File tree

cpp2rust/converter/mapper.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,14 @@ matchTemplate(const std::string &template_str,
206206
}
207207
nextLit = template_str.substr(ti, scan - ti);
208208

209-
auto it = captured.find(name);
210-
if (it != captured.end()) {
209+
auto [it, inserted] = captured.try_emplace(std::move(name));
210+
if (!inserted) {
211211
size_t end_pos = 0;
212212
if (!matchLiteralAt(instantiated, si, it->second, end_pos)) {
213213
return std::nullopt;
214214
}
215215
si = end_pos;
216216
} else {
217-
std::string value;
218-
219217
if (!nextLit.empty()) {
220218
size_t k = findNextLiteralSameDepth(instantiated, si, nextLit);
221219
if (k == std::string::npos) {
@@ -232,7 +230,7 @@ matchTemplate(const std::string &template_str,
232230
b--;
233231
}
234232

235-
value = instantiated.substr(a, b - a);
233+
it->second = instantiated.substr(a, b - a);
236234
si = k;
237235
} else {
238236
size_t a = si;
@@ -245,11 +243,9 @@ matchTemplate(const std::string &template_str,
245243
b--;
246244
}
247245

248-
value = instantiated.substr(a, b - a);
246+
it->second = instantiated.substr(a, b - a);
249247
si = instantiated.size();
250248
}
251-
252-
captured[name] = value;
253249
}
254250

255251
if (!nextLit.empty()) {
@@ -676,12 +672,11 @@ void AddRuleForUserDefinedType(clang::NamedDecl *decl) {
676672
auto cpp_name = ToString(decl);
677673
auto rs_name = ReplaceAll(cpp_name, "::", "_");
678674

679-
if (types_.contains(cpp_name)) {
675+
if (!types_.try_emplace(cpp_name, TranslationRule::TypeTgt::Plain(rs_name))
676+
.second) {
680677
return;
681678
}
682679

683-
types_[cpp_name] = TranslationRule::TypeTgt::Plain(rs_name);
684-
685680
if (auto record_decl = llvm::dyn_cast<clang::RecordDecl>(decl)) {
686681
// Forward declaration
687682
if (!record_decl->isThisDeclarationADefinition()) {

rule-preprocessor/src/syntactic.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,10 @@ impl<'a> FragmentCtx<'a> {
200200
});
201201
return;
202202
}
203-
if self.generic_names.contains(&token.text().to_string()) {
203+
let text = token.text().to_string();
204+
if self.generic_names.contains(&text) {
204205
self.flush_text();
205-
self.fragments.push(BodyFragment::Generic {
206-
generic: token.text().to_string(),
207-
});
206+
self.fragments.push(BodyFragment::Generic { generic: text });
208207
return;
209208
}
210209
}

0 commit comments

Comments
 (0)