Use global empty tuple/dict.

Small modifications to a change submitted earlier.

PiperOrigin-RevId: 308434286
Change-Id: Ifef1e854e681d4b50025496451bae5a8cfe8fd0d
This commit is contained in:
Akshay Modi 2020-04-25 13:03:18 -07:00 committed by TensorFlower Gardener
parent d17488bd97
commit ef72ad0c7a

View File

@ -44,6 +44,19 @@ namespace {
// events on eager tensors. This is set by TFE_Py_InitEagerTensor, if at all. // events on eager tensors. This is set by TFE_Py_InitEagerTensor, if at all.
PyObject* eager_tensor_profiler = nullptr; PyObject* eager_tensor_profiler = nullptr;
// Read-only dict. Please don't use this in any setting where the dict might
// actually get mutated. This is only used to pass empty kwargs when creating a
// new EagerTensor.
PyObject* EmptyDict() {
static PyObject* empty_dict = PyDict_New();
return empty_dict;
}
PyObject* EmptyTuple() {
static PyObject* empty_tuple = PyTuple_New(0);
return empty_tuple;
}
TFE_Context* GetContextHandle(PyObject* py_context) { TFE_Context* GetContextHandle(PyObject* py_context) {
tensorflow::Safe_PyObjectPtr py_context_handle( tensorflow::Safe_PyObjectPtr py_context_handle(
PyObject_GetAttrString(py_context, "_handle")); PyObject_GetAttrString(py_context, "_handle"));
@ -800,19 +813,8 @@ PyObject* EagerTensorFromHandle(TFE_TensorHandle* handle) {
if (handle == nullptr) { if (handle == nullptr) {
return nullptr; return nullptr;
} }
PyObject* empty_args = PyTuple_New(0);
if (empty_args == nullptr) {
return nullptr;
}
PyObject* empty_kwargs = PyDict_New();
if (empty_kwargs == nullptr) {
Py_DECREF(empty_args);
return nullptr;
}
EagerTensor* t = reinterpret_cast<EagerTensor*>( EagerTensor* t = reinterpret_cast<EagerTensor*>(
EagerTensorType->tp_new(EagerTensorType, empty_args, empty_kwargs)); EagerTensorType->tp_new(EagerTensorType, EmptyTuple(), EmptyDict()));
Py_DECREF(empty_kwargs);
Py_DECREF(empty_args);
if (t != nullptr) { if (t != nullptr) {
t->id = get_uid(); t->id = get_uid();
Py_INCREF(Py_None); Py_INCREF(Py_None);