Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use offset, not index in LOAD/STORE_ATTR_INSTANCE_VALUE for speed.
  • Loading branch information
markshannon committed Aug 21, 2024
commit 89655c994be185d2f1b28c581bea49abdcd9930f
18 changes: 10 additions & 8 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2012,9 +2012,10 @@ dummy_func(
DEOPT_IF(!_PyObject_InlineValues(owner_o)->valid);
}

split op(_LOAD_ATTR_INSTANCE_VALUE, (index/1, owner -- attr, null if (oparg & 1))) {
split op(_LOAD_ATTR_INSTANCE_VALUE, (offset/1, owner -- attr, null if (oparg & 1))) {
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
PyObject *attr_o = _PyObject_InlineValues(owner_o)->values[index];
PyObject **value_ptr = (PyObject**)(((char *)owner_o) + offset);
PyObject *attr_o = *value_ptr;
DEOPT_IF(attr_o == NULL);
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(attr_o);
Expand Down Expand Up @@ -2187,7 +2188,7 @@ dummy_func(
DISPATCH_INLINED(new_frame);
}

op(_GUARD_DORV_NO_DICT, (owner -- owner)) {
op(_GUARD_DORV_NO_DICT, (offset -- owner)) {
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);

assert(Py_TYPE(owner_o)->tp_dictoffset < 0);
Expand All @@ -2196,16 +2197,17 @@ dummy_func(
EXIT_IF(_PyObject_InlineValues(owner_o)->valid == 0);
}

op(_STORE_ATTR_INSTANCE_VALUE, (index/1, value, owner --)) {
op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner --)) {
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);

STAT_INC(STORE_ATTR, hit);
assert(_PyObject_GetManagedDict(owner_o) == NULL);
PyDictValues *values = _PyObject_InlineValues(owner_o);

PyObject *old_value = values->values[index];
values->values[index] = PyStackRef_AsPyObjectSteal(value);
PyObject **value_ptr = (PyObject**)(((char *)owner_o) + offset);
PyObject *old_value = *value_ptr;
*value_ptr = PyStackRef_AsPyObjectSteal(value);
if (old_value == NULL) {
PyDictValues *values = _PyObject_InlineValues(owner_o);
int index = value_ptr - values->values;
_PyDictValues_AddToInsertionOrder(values, index);
}
else {
Expand Down
22 changes: 13 additions & 9 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,15 +849,19 @@ specialize_dict_access(
assert(PyUnicode_CheckExact(name));
Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
assert (index != DKIX_ERROR);
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(base_op,
index == DKIX_EMPTY ?
SPEC_FAIL_ATTR_NOT_IN_KEYS :
SPEC_FAIL_OUT_OF_RANGE);
if (index == DKIX_EMPTY) {
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_ATTR_NOT_IN_KEYS);
return 0;
}
assert(index >= 0);
char *value_addr = (char *)&_PyObject_InlineValues(owner)->values[index];
Py_ssize_t offset = value_addr - (char *)owner;
if (offset != (uint16_t)offset) {
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_OUT_OF_RANGE);
return 0;
}
write_u32(cache->version, type->tp_version_tag);
cache->index = (uint16_t)index;
cache->index = (uint16_t)offset;
instr->op.code = values_op;
}
else {
Expand Down