Skip to content

Eliminate double-lookups in map/set operations#39

Merged
nunoplopes merged 1 commit intomasterfrom
copilot/optimize-containment-check-insert
Apr 29, 2026
Merged

Eliminate double-lookups in map/set operations#39
nunoplopes merged 1 commit intomasterfrom
copilot/optimize-containment-check-insert

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 28, 2026

Containment checks followed by insertion or access into the same container incur two hash lookups where one suffices. Three such patterns are addressed:

  • AddRuleForUserDefinedType (mapper.cpp): types_.contains + types_[key] = replaced with a single try_emplace:

    // Before: two lookups
    if (types_.contains(cpp_name)) { return; }
    types_[cpp_name] = TranslationRule::TypeTgt::Plain(rs_name);
    
    // After: one lookup
    if (!types_.try_emplace(cpp_name, TranslationRule::TypeTgt::Plain(rs_name)).second) {
        return;
    }
  • matchTemplate (mapper.cpp): captured.find + captured[name] = collapsed into a single try_emplace; the returned iterator is used directly to assign the captured value in the else branch, eliminating the second lookup on the insert path.

  • visit_token (syntactic.rs): token.text().to_string() was called twice — once for generic_names.contains() and again to populate BodyFragment::Generic. The result is now computed once and reused.

Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/3fe734b7-50b5-40fd-bf72-4065e4326706

Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
@nunoplopes nunoplopes marked this pull request as ready for review April 28, 2026 20:49
@nunoplopes nunoplopes merged commit dde10a7 into master Apr 29, 2026
9 checks passed
@nunoplopes nunoplopes deleted the copilot/optimize-containment-check-insert branch April 29, 2026 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants