8000 gh-100239: specialize dict subclasses with no getitem override by iritkatriel · Pull Request #132383 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-100239: specialize dict subclasses with no getitem override #132383

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
apply review comments
  • Loading branch information
iritkatriel committed Apr 14, 2025
commit d5dc19b797c56111f06294ff190f69397bdcacd3
2 changes: 2 additions & 0 deletions Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ extern int _PyDict_Next(

extern int _PyDict_HasOnlyStringKeys(PyObject *mp);

extern PyObject *_PyDict_Subscript(PyObject *self, PyObject *key);

// Export for '_ctypes' shared extension
PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode_metadata.h

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

4 changes: 2 additions & 2 deletions Include/internal/pycore_uop_metadata.h

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

8 changes: 4 additions & 4 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3388,8 +3388,8 @@ dict_length(PyObject *self)
return FT_ATOMIC_LOAD_SSIZE_RELAXED(((PyDictObject *)self)->ma_used);
}

static PyObject *
dict_subscript(PyObject *self, PyObject *key)
PyObject *
_PyDict_Subscript(PyObject *self, PyObject *key)
{
PyDictObject *mp = (PyDictObject *)self;
Py_ssize_t ix;
Expand Down Expand Up @@ -3434,7 +3434,7 @@ dict_ass_sub(PyObject *mp, PyObject *v, PyObject *w)

static PyMappingMethods dict_as_mapping = {
dict_length, /*mp_length*/
dict_subscript, /*mp_subscript*/
_PyDict_Subscript, /*mp_subscript*/
dict_ass_sub, /*mp_ass_subscript*/
};

Expand Down Expand Up @@ -4712,7 +4712,7 @@ In either case, this is followed by: for k in F: D[k] = F[k]");

static PyMethodDef mapp_methods[] = {
DICT___CONTAINS___METHODDEF
{"__getitem__", dict_subscript, METH_O | METH_COEXIST,
{"__getitem__", _PyDict_Subscript, METH_O | METH_COEXIST,
getitem__doc__},
DICT___SIZEOF___METHODDEF
DICT_GET_METHODDEF
Expand Down
10 changes: 4 additions & 6 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,9 @@ dummy_func(

op(_GUARD_NOS_DICT_NOT_EXACT, (nos, unused -- nos, unused)) {
PyObject *o = PyStackRef_AsPyObjectBorrow(nos);
EXIT_IF(!PyDict_Check(o));
DEOPT_IF(!Py_TYPE(o)->tp_as_mapping);
DEOPT_IF(Py_TYPE(o)->tp_as_mapping->mp_subscript !=
PyDict_Type.tp_as_mapping->mp_subscript);
}

op(_GUARD_TOS_DICT, (tos -- tos)) {
Expand All @@ -976,12 +978,8 @@ dummy_func(
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
PyObject *dict = PyStackRef_AsPyObjectBorrow(dict_st);

assert(PyDict_Check(dict));
DEOPT_IF(!Py_TYPE(dict)->tp_as_mapping);
DEOPT_IF(Py_TYPE(dict)->tp_as_mapping->mp_subscript !=
PyDict_Type.tp_as_mapping->mp_subscript);
STAT_INC(BINARY_OP, hit);
PyObject *res_o = PyDict_Type.tp_as_mapping->mp_subscript(dict, sub);
PyObject *res_o = _PyDict_Subscript(dict, sub);
DECREF_INPUTS();
ERROR_IF(res_o == NULL, error); // not found or error
res = PyStackRef_FromPyObjectSteal(res_o);
Expand Down
19 changes: 7 additions & 12 deletions Python/executor_cases.c.h

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

22 changes: 8 additions & 14 deletions Python/generated_cases.c.h

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

Loading
0