Skip to content

Commit

Permalink
Remove some unnecessary uses of utilities
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 654745047
  • Loading branch information
oprypin authored and copybara-github committed Jul 22, 2024
1 parent 015b299 commit 520f0a5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 55 deletions.
11 changes: 0 additions & 11 deletions pytype/typegraph/map_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,6 @@ V<M> FindPtrOrNull(const M& map, const K<M>& key) {
return nullptr;
}

// FindOrDefault returns a const reference to the value associated with the
// given key if it exists, otherwise returns a const reference to the given
// default value.
template<typename M>
const V<M>& FindOrDefault(const M& map, const K<M>& key, const V<M>& value) {
auto it = map.find(key);
if (it != map.end())
return it->second;
return value;
}

} // namespace map_util

} // namespace devtools_python_typegraph
Expand Down
7 changes: 0 additions & 7 deletions pytype/typegraph/map_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ TEST(MapUtilTest, FindPtrOrNullTest) {
EXPECT_EQ(*res, val);
}

TEST(MapUtilTest, FindOrDefaultTest) {
std::unordered_map<int, int> m;
EXPECT_EQ(FindOrDefault(m, 1, 2), 2);
m[1] = 3;
EXPECT_EQ(FindOrDefault(m, 1, 2), 3);
}

} // namespace

} // namespace map_util
Expand Down
9 changes: 0 additions & 9 deletions pytype/typegraph/memory_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#define PYTYPE_TYPEGRAPH_MEMORY_UTIL_H_

#include <memory>
#include <type_traits>

namespace devtools_python_typegraph {

Expand All @@ -21,14 +20,6 @@ std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

// WrapUnique wraps a pointer in a unique_ptr. Unlike make_unique, it can be
// used for classes that have private constructors.
template<typename T>
std::unique_ptr<T> WrapUnique(T* ptr) {
static_assert(!std::is_array<T>::value, "array types are unsupported");
static_assert(std::is_object<T>::value, "non-object types are unsupported");
return std::unique_ptr<T>(ptr);
}
} // namespace memory_util

} // namespace devtools_python_typegraph
Expand Down
38 changes: 16 additions & 22 deletions pytype/typegraph/solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ static std::vector<RemoveResult> remove_finished_goals(const CFGNode* pos,
std::inserter(new_goals, new_goals.begin()),
pointer_less<Binding>());
std::vector<std::tuple<GoalSet, GoalSet, GoalSet, GoalSet>> stack;
stack.push_back(
std::make_tuple(goals_to_remove, seen_goals, removed_goals, new_goals));
stack.emplace_back(goals_to_remove, seen_goals, removed_goals, new_goals);
std::vector<RemoveResult> results;
while (!stack.empty()) {
std::tie(
Expand All @@ -68,24 +67,22 @@ static std::vector<RemoveResult> remove_finished_goals(const CFGNode* pos,
goals_to_remove.erase(goals_to_remove.begin());
if (seen_goals.count(goal)) {
// Only process a goal once, to prevent infinite loops.
stack.push_back(std::make_tuple(
goals_to_remove, seen_goals, removed_goals, new_goals));
stack.emplace_back(goals_to_remove, seen_goals, removed_goals, new_goals);
continue;
}
seen_goals.insert(goal);
const auto* origin = goal->FindOrigin(pos);
if (!origin) {
new_goals.insert(goal);
stack.push_back(std::make_tuple(
goals_to_remove, seen_goals, removed_goals, new_goals));
stack.emplace_back(goals_to_remove, seen_goals, removed_goals, new_goals);
continue;
}
removed_goals.insert(goal);
for (const auto& source_set : origin->source_sets) {
GoalSet next_goals_to_remove(goals_to_remove);
next_goals_to_remove.insert(source_set.begin(), source_set.end());
stack.push_back(std::make_tuple(
next_goals_to_remove, seen_goals, removed_goals, new_goals));
stack.emplace_back(std::move(next_goals_to_remove), seen_goals,
removed_goals, new_goals);
}
}
return results;
Expand Down Expand Up @@ -171,7 +168,6 @@ const CFGNode* PathFinder::FindHighestReachableWeight(
std::vector<const CFGNode*> stack;
stack.insert(stack.end(), start->incoming().begin(), start->incoming().end());
int best_weight = -1;
int weight;
const CFGNode* best_node = nullptr;
const CFGNode* node;
while (!stack.empty()) {
Expand All @@ -180,9 +176,9 @@ const CFGNode* PathFinder::FindHighestReachableWeight(
if (node == start)
// Don't allow loops back to the start.
continue;
weight = map_util::FindOrDefault(weight_map, node, -1);
if (weight > best_weight) {
best_weight = weight;
const auto weight = weight_map.find(node);
if (weight != weight_map.end() && weight->second > best_weight) {
best_weight = weight->second;
best_node = node;
}
if (!seen.insert(node).second) continue;
Expand Down Expand Up @@ -216,9 +212,9 @@ QueryResult PathFinder::FindNodeBackwards(
blocked_.insert(shortest_path.begin(), shortest_path.end());
std::unordered_map<const CFGNode*, int, CFGNodePtrHash> weights;
int w = 0;
std::deque<const CFGNode*>::const_iterator it = shortest_path.cbegin();
for (; it != shortest_path.cend(); w++, it++)
weights[*it] = w;
for (const auto& x : shortest_path) {
weights[x] = w++;
}
std::deque<const CFGNode*> path;
const CFGNode* node = start;
while (true) {
Expand Down Expand Up @@ -249,15 +245,13 @@ SolverMetrics Solver::CalculateMetrics() const {
bool Solver::GoalsConflict(const internal::GoalSet& goals) const {
std::unordered_map<const Variable*, const Binding*> variables;
for (const Binding* goal : goals) {
const Binding* existing = map_util::FindPtrOrNull(variables,
goal->variable());
if (existing) {
CHECK(existing != goal) << "Internal error. Duplicate goal.";
CHECK(existing->data() != goal->data()) <<
"Internal error. Duplicate data across bindings.";
const auto& [it, inserted] = variables.emplace(goal->variable(), goal);
if (!inserted) {
CHECK(it->second != goal) << "Internal error. Duplicate goal.";
CHECK(it->second->data() != goal->data())
<< "Internal error. Duplicate data across bindings.";
return true;
}
variables[goal->variable()] = goal;
}
return false;
}
Expand Down
10 changes: 4 additions & 6 deletions pytype/typegraph/typegraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CFGNode* Program::NewCFGNode(std::string name, Binding* condition) {
int n = backward_reachability_->add_node();
CHECK(n == node_nr) <<
"internal error: wrong reachability cache node count.";
auto node = memory_util::WrapUnique(new CFGNode(
std::unique_ptr<CFGNode> node(new CFGNode(
this, std::move(name), node_nr, condition, backward_reachability_.get()));
CFGNode* np = node.get();
cfg_nodes_.push_back(std::move(node));
Expand All @@ -37,7 +37,7 @@ CFGNode* Program::NewCFGNode(std::string name, Binding* condition) {

Variable* Program::NewVariable() {
LOG(DEBUG) << "Creating Variable v" << next_variable_id_;
auto u = memory_util::WrapUnique(new Variable(this, next_variable_id_));
std::unique_ptr<Variable> u(new Variable(this, next_variable_id_));
next_variable_id_ += 1;
Variable* up = u.get();
variables_.push_back(std::move(u));
Expand Down Expand Up @@ -284,10 +284,8 @@ Binding* Variable::FindOrAddBinding(const BindingData& data) {
}

void Variable::RegisterBindingAtNode(Binding* binding, const CFGNode* node) {
if (!map_util::ContainsKey(cfg_node_to_bindings_, node)) {
cfg_node_to_bindings_[node] = SourceSet();
}
cfg_node_to_bindings_[node].insert(binding);
const auto& [it, _] = cfg_node_to_bindings_.emplace(node, SourceSet());
it->second.insert(binding);
}

Binding* Variable::AddBinding(const BindingData& data) {
Expand Down

0 comments on commit 520f0a5

Please sign in to comment.