-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
GH-93657: More LOAD_ATTR specializations #93892
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
Changes from 2 commits
c9966d1
e66782b
c7bde64
6a03dad
c78831d
d03cc61
36af644
c20af47
File filter
Filter by extension
Conversations
Jump to
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Specialize `LOAD_ATTR` for mutable class attribute descriptors and ``property()`` attributes. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3627,7 +3627,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int | |
} | ||
|
||
TARGET(LOAD_ATTR_CLASS) { | ||
/* LOAD_METHOD, for class methods */ | ||
assert(cframe.use_tracing == 0); | ||
_PyLoadMethodCache *cache = (_PyLoadMethodCache *)next_instr; | ||
|
||
|
@@ -3650,6 +3649,76 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int | |
NOTRACE_DISPATCH(); | ||
} | ||
|
||
TARGET(LOAD_ATTR_CLASS_MUTABLE_DESCRIPTOR) { | ||
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. Is this common enough to justify its own specialization, especially as we need to do much of the work of general lookup? 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. According to the stats on the faster-cpython repo, this represents 5.7% of the remaining failures. In contrast, properties are 13.8%. Class method objects are quite close, at 6.2%. Here's a microbenchmark, with no PGO nor LTO, but it's a Windows release build (similar to -O3).
With PGO and LTO, I expect the difference to be less dramatic. But I still expect this form to be ~=15% faster than the base version. 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. Do you have a non-enum example? enums members should be simple class attributes. 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. I don't. This opcode was specifically designed with enum in mind. |
||
assert(cframe.use_tracing == 0); | ||
_PyLoadMethodCache *cache = (_PyLoadMethodCache *)next_instr; | ||
|
||
PyObject *cls = TOP(); | ||
DEOPT_IF(!PyType_Check(cls), LOAD_ATTR); | ||
uint32_t type_version = read_u32(cache->type_version); | ||
DEOPT_IF(((PyTypeObject *)cls)->tp_version_tag != type_version, | ||
LOAD_ATTR); | ||
assert(type_version != 0); | ||
PyObject *descr = read_obj(cache->descr); | ||
descrgetfunc get = Py_TYPE(descr)->tp_descr_get; | ||
DEOPT_IF(get == NULL, LOAD_ATTR); | ||
STAT_INC(LOAD_ATTR, hit); | ||
PyObject *res = get(descr, NULL, cls); | ||
if (res == NULL) { | ||
goto error; | ||
} | ||
assert(res != NULL); | ||
SET_TOP(NULL); | ||
STACK_GROW((oparg & 1)); | ||
SET_TOP(res); | ||
Py_DECREF(cls); | ||
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR); | ||
NOTRACE_DISPATCH(); | ||
} | ||
|
||
TARGET(LOAD_ATTR_PROPERTY) { | ||
assert(cframe.use_tracing == 0); | ||
_PyLoadMethodCache *cache = (_PyLoadMethodCache *)next_instr; | ||
|
||
PyObject *owner = TOP(); | ||
PyTypeObject *cls = Py_TYPE(owner); | ||
uint32_t type_version = read_u32(cache->type_version); | ||
DEOPT_IF(cls->tp_version_tag != type_version, LOAD_ATTR); | ||
assert(type_version != 0); | ||
PyObject *prop = read_obj(cache->descr); | ||
assert(Py_TYPE(prop) == &PyProperty_Type); | ||
PyObject *fget = _PyProperty_PropGet(prop); | ||
DEOPT_IF(!PyFunction_Check(fget), LOAD_ATTR); | ||
Fidget-Spinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyFunctionObject *f = (PyFunctionObject *)fget; | ||
DEOPT_IF(f->func_version != cache->keys_version[0], LOAD_ATTR); | ||
PyCodeObject *code = (PyCodeObject *)f->func_code; | ||
Fidget-Spinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
size_t size = code->co_nlocalsplus + code->co_stacksize + FRAME_SPECIALS_SIZE; | ||
assert(code->co_argcount == 1); | ||
STAT_INC(LOAD_ATTR, hit); | ||
|
||
_PyInterpreterFrame *new_frame = _PyThreadState_BumpFramePointer(tstate, size); | ||
if (new_frame == NULL) { | ||
goto error; | ||
} | ||
CALL_STAT_INC(frames_pushed); | ||
Py_INCREF(f); | ||
_PyFrame_InitializeSpecials(new_frame, f, | ||
NULL, code->co_nlocalsplus); | ||
SET_TOP(NULL); | ||
STACK_SHRINK(!(oparg & 1)); | ||
new_frame->localsplus[0] = owner; | ||
for (int i = 1; i < code->co_nlocalsplus; i++) { | ||
new_frame->localsplus[i] = NULL; | ||
} | ||
_PyFrame_SetStackPointer(frame, stack_pointer); | ||
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR); | ||
frame->prev_instr = next_instr - 1; | ||
new_frame->previous = frame; | ||
frame = cframe.current_frame = new_frame; | ||
CALL_STAT_INC(inlined_py_calls); | ||
goto start_frame; | ||
} | ||
|
||
TARGET(STORE_ATTR_ADAPTIVE) { | ||
assert(cframe.use_tracing == 0); | ||
_PyAttrCache *cache = (_PyAttrCache *)next_instr; | ||
|
@@ -4549,7 +4618,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int | |
} | ||
|
||
TARGET(LOAD_ATTR_METHOD_WITH_VALUES) { | ||
/* LOAD_METHOD, with cached method object */ | ||
/* Cached method object */ | ||
assert(cframe.use_tracing == 0); | ||
PyObject *self = TOP(); | ||
PyTypeObject *self_cls = Py_TYPE(self); | ||
|
@@ -4575,8 +4644,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int | |
} | ||
|
||
TARGET(LOAD_ATTR_METHOD_WITH_DICT) { | ||
/* LOAD_METHOD, with a dict | ||
Can be either a managed dict, or a tp_dictoffset offset.*/ | ||
/* Can be either a managed dict, or a tp_dictoffset offset.*/ | ||
assert(cframe.use_tracing == 0); | ||
PyObject *self = TOP(); | ||
PyTypeObject *self_cls = Py_TYPE(self); | ||
|
@@ -5527,6 +5595,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int | |
} | ||
|
||
TARGET(CACHE) { | ||
printf("cache\n"); | ||
Fidget-Spinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Py_UNREACHABLE(); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.