From 1c9ce505baa354ec01400ce252ec128436ed9a58 Mon Sep 17 00:00:00 2001 From: Hana Joo Date: Tue, 17 Dec 2024 05:06:24 -0800 Subject: [PATCH] Refactor the "TraverseState" out to make it easier to review future CL for performance improvement. This is (almost) a no-op change. PiperOrigin-RevId: 707050510 --- pytype/typegraph/solver.cc | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pytype/typegraph/solver.cc b/pytype/typegraph/solver.cc index 0007c1562..8fc20f809 100644 --- a/pytype/typegraph/solver.cc +++ b/pytype/typegraph/solver.cc @@ -27,8 +27,13 @@ namespace internal { struct RemoveResult { const GoalSet removed_goals; const GoalSet new_goals; - RemoveResult(const GoalSet& removed_goals, const GoalSet& new_goals): - removed_goals(removed_goals), new_goals(new_goals) {} +}; + +struct TraverseState { + GoalSet goals_to_remove; + GoalSet seen_goals; + GoalSet removed_goals; + GoalSet new_goals; }; // Remove all goals that can be fulfilled at the current CFG node. @@ -39,51 +44,47 @@ struct RemoveResult { // avoiding bugs related to transmitting state information across calls. static std::vector remove_finished_goals(const CFGNode* pos, const GoalSet& goals) { - GoalSet goals_to_remove; + TraverseState state; // We can't use set_intersection here because pos->bindings() is a vector. for (const auto* goal : pos->bindings()) { if (goals.count(goal)) { - goals_to_remove.insert(goal); + state.goals_to_remove.insert(goal); } } - GoalSet seen_goals; - GoalSet removed_goals; - GoalSet new_goals; - std::set_difference(goals.begin(), goals.end(), - goals_to_remove.begin(), goals_to_remove.end(), - std::inserter(new_goals, new_goals.begin()), + std::set_difference(goals.begin(), goals.end(), state.goals_to_remove.begin(), + state.goals_to_remove.end(), + std::inserter(state.new_goals, state.new_goals.begin()), pointer_less()); - std::deque> queue; - queue.emplace_back(goals_to_remove, seen_goals, removed_goals, new_goals); + std::deque queue; + queue.emplace_back(state); std::vector results; while (!queue.empty()) { - std::tie(goals_to_remove, seen_goals, removed_goals, new_goals) = - queue.front(); + state = std::move(queue.front()); queue.pop_front(); - if (goals_to_remove.empty()) { - results.push_back(RemoveResult(removed_goals, new_goals)); + if (state.goals_to_remove.empty()) { + results.push_back(RemoveResult(state.removed_goals, state.new_goals)); continue; } - const auto* goal = *goals_to_remove.begin(); - goals_to_remove.erase(goals_to_remove.begin()); - if (seen_goals.count(goal)) { + const auto* goal = *state.goals_to_remove.begin(); + state.goals_to_remove.erase(state.goals_to_remove.begin()); + if (state.seen_goals.count(goal)) { // Only process a goal once, to prevent infinite loops. - queue.emplace_back(goals_to_remove, seen_goals, removed_goals, new_goals); + queue.emplace_back(std::move(state)); continue; } - seen_goals.insert(goal); + state.seen_goals.insert(goal); const auto* origin = goal->FindOrigin(pos); if (!origin) { - new_goals.insert(goal); - queue.emplace_back(goals_to_remove, seen_goals, removed_goals, new_goals); + state.new_goals.insert(goal); + queue.emplace_back(std::move(state)); continue; } - removed_goals.insert(goal); + state.removed_goals.insert(goal); for (const auto& source_set : origin->source_sets) { - GoalSet next_goals_to_remove(goals_to_remove); + GoalSet next_goals_to_remove(state.goals_to_remove); next_goals_to_remove.insert(source_set.begin(), source_set.end()); - queue.emplace_back(std::move(next_goals_to_remove), seen_goals, - removed_goals, new_goals); + queue.emplace_back(std::move(next_goals_to_remove), state.seen_goals, + state.removed_goals, state.new_goals); } } return results;