diff --git a/tensorflow/python/util/stack_trace.cc b/tensorflow/python/util/stack_trace.cc index 40e05e612c4..eec83476d25 100644 --- a/tensorflow/python/util/stack_trace.cc +++ b/tensorflow/python/util/stack_trace.cc @@ -46,9 +46,10 @@ std::vector StackTrace::ToStackFrames( result.reserve(code_objs_.size()); for (int i = code_objs_.size() - 1; i >= 0; --i) { - const char* file_name = GetPythonString(code_objs_[i]->co_filename); + const std::pair& code_obj = code_objs_[i]; + const char* file_name = GetPythonString(code_obj.first->co_filename); 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)) { continue; // Never filter the innermost frame. @@ -60,7 +61,7 @@ std::vector StackTrace::ToStackFrames( result.push_back(it->second); } else { result.emplace_back(StackFrame{file_name, line_number, - GetPythonString(code_objs_[i]->co_name)}); + GetPythonString(code_obj.first->co_name)}); } } diff --git a/tensorflow/python/util/stack_trace.h b/tensorflow/python/util/stack_trace.h index b416e3b0435..be874d60dc7 100644 --- a/tensorflow/python/util/stack_trace.h +++ b/tensorflow/python/util/stack_trace.h @@ -74,8 +74,7 @@ class StackTrace final { DCHECK(code_obj != nullptr); Py_INCREF(code_obj); - result.code_objs_.push_back(code_obj); - result.last_instructions_.push_back(frame->f_lasti); + result.code_objs_.push_back(std::make_pair(code_obj, frame->f_lasti)); } return result; } @@ -84,18 +83,13 @@ class StackTrace final { ABSL_ATTRIBUTE_HOT ~StackTrace() { Clear(); } - StackTrace(StackTrace&& other) { - code_objs_ = std::move(other.code_objs_); - last_instructions_ = std::move(other.last_instructions_); - other.code_objs_ = {}; - } + StackTrace(StackTrace&& other) { std::swap(code_objs_, other.code_objs_); } // Python GIL must be acquired beforehand. ABSL_ATTRIBUTE_HOT StackTrace& operator=(StackTrace&& other) { Clear(); std::swap(code_objs_, other.code_objs_); - std::swap(last_instructions_, other.last_instructions_); return *this; } @@ -111,14 +105,13 @@ class StackTrace final { ABSL_ATTRIBUTE_HOT void Clear() { 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(); - last_instructions_.clear(); } private: - absl::InlinedVector code_objs_; - absl::InlinedVector last_instructions_; + absl::InlinedVector, kStackTraceInitialSize> + code_objs_; StackTrace(const StackTrace&) = delete; StackTrace& operator=(const StackTrace&) = delete;