-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-44525: Specialize for calls to type and other builtin classes with 1 argument. #29942
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
Merged
markshannon
merged 5 commits into
python:main
from
faster-cpython:specialize-calls-to-builtin-types
Dec 15, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e63a49
Specialize for calls to type and other builtin classes with 1 argument.
markshannon 6b0738c
Add news.
markshannon 8672012
Merge branch 'main' into specialize-calls-to-builtin-types
markshannon 82bc708
Only specialize non-method calls to builtin classes.
markshannon ac14f9b
Fix CALL_NO_KW_BUILTIN_CLASS_1
markshannon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core and Builtins/2021-12-07-11-04-21.bpo-44525.6OWCgr.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Specialize the CALL_FUNCTION instruction for calls to builtin types with a | ||
single argument. Speeds up ``range(x)``, ``list(x)``, and specifically | ||
``type(obj)``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,8 +491,10 @@ initial_counter_value(void) { | |
#define SPEC_FAIL_PYCFUNCTION_NOARGS 16 | ||
#define SPEC_FAIL_BAD_CALL_FLAGS 17 | ||
#define SPEC_FAIL_CLASS 18 | ||
#define SPEC_FAIL_C_METHOD_CALL 19 | ||
#define SPEC_FAIL_METHDESCR_NON_METHOD 20 | ||
#define SPEC_FAIL_PYTHON_CLASS 19 | ||
#define SPEC_FAIL_C_METHOD_CALL 20 | ||
#define SPEC_FAIL_METHDESCR_NON_METHOD 21 | ||
#define SPEC_FAIL_METHOD_CALL_CLASS 22 | ||
|
||
/* COMPARE_OP */ | ||
#define SPEC_FAIL_STRING_COMPARE 13 | ||
|
@@ -1263,6 +1265,27 @@ specialize_class_call( | |
PyObject *callable, _Py_CODEUNIT *instr, | ||
int nargs, SpecializedCacheEntry *cache) | ||
{ | ||
assert(PyType_Check(callable)); | ||
PyTypeObject *tp = (PyTypeObject *)callable; | ||
if (_Py_OPCODE(instr[-1]) == PRECALL_METHOD) { | ||
SPECIALIZATION_FAIL(CALL_NO_KW, SPEC_FAIL_METHOD_CALL_CLASS); | ||
return -1; | ||
} | ||
if (tp->tp_new == PyBaseObject_Type.tp_new) { | ||
SPECIALIZATION_FAIL(CALL_NO_KW, SPEC_FAIL_PYTHON_CLASS); | ||
return -1; | ||
} | ||
if (nargs == 1) { | ||
if (tp == &PyType_Type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
*instr = _Py_MAKECODEUNIT(CALL_NO_KW_TYPE_1, _Py_OPARG(*instr)); | ||
return 0; | ||
} | ||
if ((tp->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) && tp->tp_vectorcall != NULL) { | ||
cache->adaptive.version = tp->tp_version_tag; | ||
*instr = _Py_MAKECODEUNIT(CALL_NO_KW_BUILTIN_CLASS_1, _Py_OPARG(*instr)); | ||
return 0; | ||
} | ||
} | ||
SPECIALIZATION_FAIL(CALL_NO_KW, SPEC_FAIL_CLASS); | ||
return -1; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't remember if you like using the
Py_IS_TYPE
inline function, but just mentioning it here and leaving it up to you :).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.
I'll skip on the obfuscation macro 🙂