Skip to content

drt: contains instead of find == end - #11008

Open
bnmfw wants to merge 2 commits into
The-OpenROAD-Project:masterfrom
The-OpenROAD-Project-staging:drt_contains
Open

drt: contains instead of find == end#11008
bnmfw wants to merge 2 commits into
The-OpenROAD-Project:masterfrom
The-OpenROAD-Project-staging:drt_contains

Conversation

@bnmfw

@bnmfw bnmfw commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Just applies the contains() method over some parts of DRT when appropriate. This does not impact behaivour. Revised by me twice and by claude as to all changes being correct.

Type of Change

Pure refactor

Impact

No changes

Verification

  • I have verified that the local build succeeds (./etc/Build.sh).
  • I have run the relevant tests and they pass.
  • My code follows the repository's formatting guidelines.
  • I have signed my commits (DCO).

Related Issues

Supports #9850

@bnmfw bnmfw self-assigned this Jul 27, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modernizes the codebase by replacing verbose find-and-end checks with the C++20 contains method across various maps and sets. The review feedback correctly identifies several locations where using contains followed by map access (such as at, operator[], or insert) introduces redundant lookups. To optimize performance, these instances should be refactored to use a single find or insert operation as suggested.

Comment thread src/drt/src/dr/FlexDR.cpp
Comment on lines +670 to 672
if (relabel.contains(type)) {
type = relabel.at(type);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using contains followed by at results in a double lookup on the map. We can perform a single lookup using find to improve efficiency.

        if (auto it = relabel.find(type); it != relabel.end()) {
          type = it->second;
        }

Comment on lines 1904 to 1909
if (ripupMode_ == RipUpMode::DRC && checking_obj != nullptr
&& obj_gc_version.find(net->getFrNet()) != obj_gc_version.end()
&& obj_gc_version.find(checking_obj) != obj_gc_version.end()
&& obj_gc_version.contains(net->getFrNet())
&& obj_gc_version.contains(checking_obj)
&& obj_gc_version[net->getFrNet()] == std::make_pair(gc_version, 0)
&& obj_gc_version[checking_obj] == std::make_pair(gc_version, 0)) {
continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block performs multiple lookups on obj_gc_version for both net->getFrNet() and checking_obj (first with contains, then with operator[]). We can optimize this by performing a single find lookup for each key.

Suggested change
if (ripupMode_ == RipUpMode::DRC && checking_obj != nullptr
&& obj_gc_version.find(net->getFrNet()) != obj_gc_version.end()
&& obj_gc_version.find(checking_obj) != obj_gc_version.end()
&& obj_gc_version.contains(net->getFrNet())
&& obj_gc_version.contains(checking_obj)
&& obj_gc_version[net->getFrNet()] == std::make_pair(gc_version, 0)
&& obj_gc_version[checking_obj] == std::make_pair(gc_version, 0)) {
continue;
if (ripupMode_ == RipUpMode::DRC && checking_obj != nullptr) {
auto it1 = obj_gc_version.find(net->getFrNet());
auto it2 = obj_gc_version.find(checking_obj);
if (it1 != obj_gc_version.end() && it2 != obj_gc_version.end()
&& it1->second == std::make_pair(gc_version, 0)
&& it2->second == std::make_pair(gc_version, 0)) {
continue;
}
}

Comment on lines +110 to 113
if (!coords.contains(coord)) {
coords.insert(std::make_pair(coord, frAccessPointEnum::Center));
} else {
coords[coord] = std::min(coords[coord], frAccessPointEnum::Center);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using contains followed by insert or operator[] results in multiple lookups on the map. We can optimize this to a single lookup by using insert and checking the returned pair.

  auto [it, inserted] = coords.insert({coord, frAccessPointEnum::Center});
  if (!inserted) {
    it->second = std::min(it->second, frAccessPointEnum::Center);
  }

Comment on lines +137 to 140
if (!coords.contains(coord)) {
coords.insert(std::make_pair(coord, frAccessPointEnum::EncOpt));
} else {
coords[coord] = std::min(coords[coord], frAccessPointEnum::EncOpt);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using contains followed by insert or operator[] results in multiple lookups on the map. We can optimize this to a single lookup by using insert and checking the returned pair.

    auto [it, inserted] = coords.insert({coord, frAccessPointEnum::EncOpt});
    if (!inserted) {
      it->second = std::min(it->second, frAccessPointEnum::EncOpt);
    }

Comment on lines +336 to 339
if (!inst_to_unique_class_.contains(inst)) {
return nullptr;
}
return inst_to_unique_class_.at(inst);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using contains followed by at results in a double lookup on inst_to_unique_class_. We can perform a single lookup using find to improve efficiency.

  auto it = inst_to_unique_class_.find(inst);
  if (it == inst_to_unique_class_.end()) {
    return nullptr;
  }
  return it->second;

@bnmfw

bnmfw commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

I understand what Gemini is asking for, but I think doing it is out of scope for this PR and it should be merged as is.
If necessary I can go through the same files later and give special attention to these double lookup cases, but I think just changing find to contains here is cleaner and makes the review easier.

@bnmfw
bnmfw marked this pull request as ready for review July 27, 2026 23:40
@bnmfw
bnmfw requested a review from a team as a code owner July 27, 2026 23:40
@bnmfw
bnmfw requested a review from maliberty July 27, 2026 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant