bpo-27535: Optimize warnings.warn()#4508
bpo-27535: Optimize warnings.warn()#4508vstinner merged 1 commit intopython:masterfrom vstinner:warn_optim2
Conversation
* Optimize warnings.filterwarnings(). Replace re.compile('') with
None to avoid the cost of calling a regex.match() method, whereas
it always matchs.
* Optimize get_warnings_attr(): replace PyObject_GetAttrString() with
_PyObject_GetAttrId().
Cleanup also create_filter():
* Use _Py_IDENTIFIER() to allow to cleanup strings at Python
finalization
* Replace Py_FatalError() with a regular exceptions
| } | ||
| else { | ||
| Py_FatalError("unknown action"); | ||
| PyErr_SetString(PyExc_ValueError, "unknown action"); |
There was a problem hiding this comment.
Please revert this change. This is an internal function called with predefined set of arguments. The code in the else branch is a dead code. Here should be Py_FatalError() or Py_UNREACHABLE().
There was a problem hiding this comment.
I like your idea of using _Py_Identifier: I proposed #4516 to implement your idea. It avoids the need to have to unhandle "unknown action".
FYI I'm trying to remove all Py_FatalError() calls from the Python source code, one by one, when it's possible and when it makes sense. I added a new _PyInitError API to report errors at Python initialization, so the caller can decide when Py_FatalError() will be called, and maybe release memory before calling Py_FatalError() (or flush files, or something else).
| static PyObject *default_str = NULL; | ||
| static PyObject *always_str = NULL; | ||
| PyObject *action_obj = NULL; | ||
| _Py_IDENTIFIER(ignore); |
There was a problem hiding this comment.
This optimization doesn't make sense since create_filter() is used only few times at start. It would be better to pass _Py_Identifier * instead of const char *, this could save few lines of code.
There was a problem hiding this comment.
Sorry, I chose to put two changes into the same commit. This change was made to allow to free strings at Python finallization, it's not an optimization.
None to avoid the cost of calling a regex.match() method, whereas
it always matchs.
_PyObject_GetAttrId().
Cleanup also create_filter():
finalization
https://bugs.python.org/issue27535