pytorch/torch/csrc/jit/frontend/mini_environment.h
Aaron Gokaslan a34a9c3471 Perf: Apply more clang-tidy fixups to torch headers (#91445)
Applies so more fixes to headers that may have been missed before for performance optimization.cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @EikanWang @ezyang since this more in the series of the clang-tidy fixup

This is PR fixes 3 main issues:
1. Use emplacement more in headers
1. Avoid unnecessary copies and use const ref when possible
1. Default any special functions when possible to make them potentially trivial and more readable.
1. There is also one change in this PR that tries to prevent unnecessary math promotion, the rest of these changes are in another PR
Pull Request resolved: https://github.com/pytorch/pytorch/pull/91445
Approved by: https://github.com/ezyang
2022-12-29 23:43:45 +00:00

58 lines
1.4 KiB
C++

#pragma once
#include <ATen/core/jit_type.h>
#include <torch/csrc/jit/ir/ir.h>
namespace torch {
namespace jit {
// Simple data structure for containing a type T in nested control blocks
// Should only be used after initial compilation where type checking and
// loads and stores are emitted
template <typename T>
struct MiniEnvironment {
MiniEnvironment(Block* b, std::shared_ptr<MiniEnvironment> next = nullptr)
: next(std::move(next)) {}
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
std::shared_ptr<MiniEnvironment<T>> next;
T findInThisFrame(const std::string& name) {
auto it = table.find(name);
if (it != table.end()) {
return it->second;
}
return nullptr;
}
T findInAnyFrame(const std::string& name) {
for (auto runner = this; runner; runner = runner->next.get()) {
if (auto r = runner->findInThisFrame(name)) {
return r;
}
}
return nullptr;
}
void setVar(const std::string& name, T value) {
table[name] = value;
}
std::vector<std::string> definedVariables() {
std::vector<std::string> result;
result.reserve(table.size());
for (auto& kv : table) {
result.push_back(kv.first);
}
std::sort(result.begin(), result.end());
return result;
}
private:
std::unordered_map<std::string, T> table;
};
} // namespace jit
} // namespace torch