Skip to content

Commit cdc2776

Browse files
h-joocopybara-github
authored andcommitted
Convert TraverseState::new_goals and TraverseState::removed_goals into std::vector and push/pop instead of insert/erase.
These containers were never used to check set membership during the loop, it was practically being used to later construct a set after the algorithm, of which in previous state of the code it made sense(because it was being copied all over the place), but while traversing the problem space through DFS it doesn't make sense just because pushing and poping on a `std::vector` is way more cheaper than insertion and deletion within a `std::set`, as we only want to remember the increment of the state change between DFSs. PiperOrigin-RevId: 707890975
1 parent 9c6c21b commit cdc2776

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

pytype/typegraph/solver.cc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ struct RemoveResult {
3636
struct TraverseState {
3737
GoalSet goals_to_remove;
3838
GoalSet seen_goals;
39-
GoalSet removed_goals;
40-
GoalSet new_goals;
41-
TraverseState() {}
39+
std::vector<const Binding*> removed_goals;
40+
std::vector<const Binding*> new_goals;
4241
};
4342

4443
enum ActionType {
@@ -78,8 +77,9 @@ static void traverse(const CFGNode* position,
7877
std::vector<RemoveResult>& results,
7978
std::stack<Action>& actions, TraverseState& state) {
8079
if (state.goals_to_remove.empty()) {
81-
results.emplace_back(GoalSet(state.removed_goals),
82-
GoalSet(state.new_goals));
80+
results.emplace_back(
81+
GoalSet(state.removed_goals.begin(), state.removed_goals.end()),
82+
GoalSet(state.new_goals.begin(), state.new_goals.end()));
8383
return;
8484
}
8585

@@ -92,23 +92,19 @@ static void traverse(const CFGNode* position,
9292
actions.emplace(TRAVERSE, nullptr);
9393
return;
9494
}
95-
auto [it, added] = state.seen_goals.insert(goal);
95+
auto [it, _] = state.seen_goals.insert(goal);
9696
actions.emplace(ERASE_SEEN_GOALS, nullptr, it);
9797

9898
const auto* origin = goal->FindOrigin(position);
9999
if (!origin) {
100-
std::tie(it, added) = state.new_goals.insert(goal);
101-
if (added) {
102-
actions.emplace(ERASE_NEW_GOALS, nullptr, it);
103-
}
100+
state.new_goals.push_back(goal);
101+
actions.emplace(ERASE_NEW_GOALS, nullptr);
104102
actions.emplace(TRAVERSE, nullptr);
105103
return;
106104
}
107105

108-
std::tie(it, added) = state.removed_goals.insert(goal);
109-
if (added) {
110-
actions.emplace(ERASE_REMOVED_GOALS, nullptr, it);
111-
}
106+
state.removed_goals.push_back(goal);
107+
actions.emplace(ERASE_REMOVED_GOALS, nullptr);
112108
for (const auto& source_set : origin->source_sets) {
113109
for (const Binding* next_goal : source_set) {
114110
if (!state.goals_to_remove.count(next_goal)) {
@@ -163,10 +159,10 @@ static std::vector<RemoveResult> remove_finished_goals(const CFGNode* pos,
163159
state.seen_goals.erase(action.erase_it);
164160
break;
165161
case ERASE_NEW_GOALS:
166-
state.new_goals.erase(action.erase_it);
162+
state.new_goals.pop_back();
167163
break;
168164
case ERASE_REMOVED_GOALS:
169-
state.removed_goals.erase(action.erase_it);
165+
state.removed_goals.pop_back();
170166
break;
171167
}
172168
}

0 commit comments

Comments
 (0)