-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
bpo-42152: Optimize out uses of PyDict_GetItemWithError if its result is not used. #22986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-42152: Optimize out uses of PyDict_GetItemWithError if its result is not used. #22986
Conversation
… is not used. Use PyDict_Contains and PyDict_SetDefault if appropriate.
| int r = _PyDict_ContainsId(d, &PyId___builtins__); | ||
| if (r == 0) { | ||
| r = _PyDict_SetItemId(d, &PyId___builtins__, | ||
| PyEval_GetBuiltins()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyEval_GetBuiltins() is cheap function so we can use PyDict_SetDefault() here, and builtin_eval_impl, builtin_exec_impl if you want to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no _PyDict_SetDefaultId() and I do not want to introduce it just for these three cases. Using PyDict_SetDefault() + _PyUnicode_FromId() will not make large difference:
int r = _PyDict_ContainsId(d, &PyId___builtins__);
if (r == 0) {
r = _PyDict_SetItemId(d, &PyId___builtins__,
PyEval_GetBuiltins());
}
if (r < 0) {
remove_module(tstate, name);
return NULL;
}vs
PyObject *tmp = _PyUnicode_FromId(&PyId___builtins__);
if (tmp != NULL) {
tmp = PyDict_SetDefault(d, tmp, PyEval_GetBuiltins());
}
if (tmp == NULL) {
remove_module(tstate, name);
return NULL;
}
methane
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very nice.
|
LGTM. The PR title say this is an optimization, but I don't see that as relevant or justified. |
…ythonGH-22986) If PyDict_GetItemWithError is only used to check whether the key is in dict, it is better to use PyDict_Contains instead. And if it is used in combination with PyDict_SetItem, PyDict_SetDefault can replace the combination.
Use PyDict_Contains and PyDict_SetDefault if appropriate.
https://bugs.python.org/issue42152