[NFC] Simplify StackTrace class by storing a pair inside a vector instead of two pairs

PiperOrigin-RevId: 346931686
Change-Id: I8f171113c65fc0604d14d775ba357e9450a75c15
This commit is contained in:
George Karpenkov 2020-12-10 22:02:50 -08:00 committed by TensorFlower Gardener
parent 5a85d44038
commit 43a080a530
2 changed files with 9 additions and 15 deletions

View File

@ -46,9 +46,10 @@ std::vector<StackFrame> StackTrace::ToStackFrames(
result.reserve(code_objs_.size()); result.reserve(code_objs_.size());
for (int i = code_objs_.size() - 1; i >= 0; --i) { for (int i = code_objs_.size() - 1; i >= 0; --i) {
const char* file_name = GetPythonString(code_objs_[i]->co_filename); const std::pair<PyCodeObject*, int>& code_obj = code_objs_[i];
const char* file_name = GetPythonString(code_obj.first->co_filename);
const int line_number = const int line_number =
PyCode_Addr2Line(code_objs_[i], last_instructions_[i]); PyCode_Addr2Line(code_objs_[i].first, code_obj.second);
if (!result.empty() && filtered.count(file_name)) { if (!result.empty() && filtered.count(file_name)) {
continue; // Never filter the innermost frame. continue; // Never filter the innermost frame.
@ -60,7 +61,7 @@ std::vector<StackFrame> StackTrace::ToStackFrames(
result.push_back(it->second); result.push_back(it->second);
} else { } else {
result.emplace_back(StackFrame{file_name, line_number, result.emplace_back(StackFrame{file_name, line_number,
GetPythonString(code_objs_[i]->co_name)}); GetPythonString(code_obj.first->co_name)});
} }
} }

View File

@ -74,8 +74,7 @@ class StackTrace final {
DCHECK(code_obj != nullptr); DCHECK(code_obj != nullptr);
Py_INCREF(code_obj); Py_INCREF(code_obj);
result.code_objs_.push_back(code_obj); result.code_objs_.push_back(std::make_pair(code_obj, frame->f_lasti));
result.last_instructions_.push_back(frame->f_lasti);
} }
return result; return result;
} }
@ -84,18 +83,13 @@ class StackTrace final {
ABSL_ATTRIBUTE_HOT ABSL_ATTRIBUTE_HOT
~StackTrace() { Clear(); } ~StackTrace() { Clear(); }
StackTrace(StackTrace&& other) { StackTrace(StackTrace&& other) { std::swap(code_objs_, other.code_objs_); }
code_objs_ = std::move(other.code_objs_);
last_instructions_ = std::move(other.last_instructions_);
other.code_objs_ = {};
}
// Python GIL must be acquired beforehand. // Python GIL must be acquired beforehand.
ABSL_ATTRIBUTE_HOT ABSL_ATTRIBUTE_HOT
StackTrace& operator=(StackTrace&& other) { StackTrace& operator=(StackTrace&& other) {
Clear(); Clear();
std::swap(code_objs_, other.code_objs_); std::swap(code_objs_, other.code_objs_);
std::swap(last_instructions_, other.last_instructions_);
return *this; return *this;
} }
@ -111,14 +105,13 @@ class StackTrace final {
ABSL_ATTRIBUTE_HOT ABSL_ATTRIBUTE_HOT
void Clear() { void Clear() {
if (!code_objs_.empty()) DCheckPyGilStateForStackTrace(); if (!code_objs_.empty()) DCheckPyGilStateForStackTrace();
for (PyCodeObject* obj : code_objs_) Py_DECREF(obj); for (const auto& p : code_objs_) Py_DECREF(p.first);
code_objs_.clear(); code_objs_.clear();
last_instructions_.clear();
} }
private: private:
absl::InlinedVector<PyCodeObject*, kStackTraceInitialSize> code_objs_; absl::InlinedVector<std::pair<PyCodeObject*, int>, kStackTraceInitialSize>
absl::InlinedVector<int, kStackTraceInitialSize> last_instructions_; code_objs_;
StackTrace(const StackTrace&) = delete; StackTrace(const StackTrace&) = delete;
StackTrace& operator=(const StackTrace&) = delete; StackTrace& operator=(const StackTrace&) = delete;