8000 bpo-32346: speed up slot lookup during class creation by pitrou · Pull Request #4902 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-32346: speed up slot lookup during class creation #4902

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

Closed
wants to merge 9 commits into from
Closed
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
Next Next commit
bpo-32346: speed up slot lookup during class creation
Instead of looking up every possible slot in every class along the MRO
(generating possibly hundreds of dict lookups), cache the known descriptors
of each type so that finding them again is simply a matter of walking
a tuple.
  • Loading branch information
pitrou committed Dec 16, 2017
commit 4b2e41c14789378ca2c02a46d579641dd8423a37
16 changes: 11 additions & 5 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ typedef struct _typeobject {

destructor tp_finalize;

PyObject *tp_defined_slots;

#ifdef COUNT_ALLOCS
/* these must be last and never explicitly initialized */
Py_ssize_t tp_allocs;
Expand Down Expand Up @@ -656,17 +658,21 @@ given type object has a specified feature.
#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
#define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)

#define Py_TPFLAGS_DEFAULT ( \
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
Py_TPFLAGS_HAVE_VERSION_TAG | \
0)

/* NOTE: The following flags reuse lower bits (removed as part of the
* Python 3.0 transition). */

/* Type structure has tp_finalize member (3.4) */
#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)

/* Type structure has tp_defined_slots member (3.7) */
#define Py_TPFLAGS_HAVE_DEFINED_SLOTS (1UL << 1)

#define Py_TPFLAGS_DEFAULT ( \
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
Py_TPFLAGS_HAVE_VERSION_TAG | \
Py_TPFLAGS_HAVE_DEFINED_SLOTS | \
0)

#ifdef Py_LIMITED_API
#define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0)
#else
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ def delx(self): del self.__x
check((1,2,3), vsize('') + 3*self.P)
# type
# static type: PyTypeObject
fmt = 'P2n15Pl4Pn9Pn11PIP'
fmt = 'P2n15Pl4Pn9Pn11PI2P'
if hasattr(sys, 'getcounts'):
fmt += '3n2P'
s = vsize(fmt)
Expand Down
Loading
0