drt: contains instead of find == end - #11008
Conversation
Signed-off-by: bnmfw <bernardoborgessandoval@gmail.com>
There was a problem hiding this comment.
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.
| if (relabel.contains(type)) { | ||
| type = relabel.at(type); | ||
| } |
| 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; |
There was a problem hiding this comment.
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.
| 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; | |
| } | |
| } |
| if (!coords.contains(coord)) { | ||
| coords.insert(std::make_pair(coord, frAccessPointEnum::Center)); | ||
| } else { | ||
| coords[coord] = std::min(coords[coord], frAccessPointEnum::Center); |
There was a problem hiding this comment.
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);
}| if (!coords.contains(coord)) { | ||
| coords.insert(std::make_pair(coord, frAccessPointEnum::EncOpt)); | ||
| } else { | ||
| coords[coord] = std::min(coords[coord], frAccessPointEnum::EncOpt); |
There was a problem hiding this comment.
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);
}| if (!inst_to_unique_class_.contains(inst)) { | ||
| return nullptr; | ||
| } | ||
| return inst_to_unique_class_.at(inst); |
There was a problem hiding this comment.
|
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. |
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
./etc/Build.sh).Related Issues
Supports #9850