Skip to content

Commit 08740af

Browse files
Update _PyType_InitSlotDefs and add comment for wrapperbase.name_count
1 parent 1459c16 commit 08740af

3 files changed

Lines changed: 18 additions & 21 deletions

File tree

Include/cpython/descrobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ struct wrapperbase {
1616
const char *doc;
1717
int flags;
1818
PyObject *name_strobj;
19+
20+
/* Stores the number of times where slotdefs has elements with this name.
21+
This counter precalculated by _PyType_InitSlotDefs when main
22+
interprepter starts.
23+
*/
1924
uint8_t name_count;
2025
};
2126

Include/internal/pycore_typeobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ typedef int (*_py_validate_type)(PyTypeObject *);
149149
extern int _PyType_Validate(PyTypeObject *ty, _py_validate_type validate, unsigned int *tp_version);
150150
extern int _PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t tp_version);
151151

152-
// Precalculates count of non-unique slots
152+
// Precalculates count of non-unique slots and fills wrapperbase::name_count.
153153
extern int _PyType_InitSlotDefs(PyInterpreterState *interp);
154154

155155
#ifdef __cplusplus

Objects/typeobject.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11253,7 +11253,7 @@ _PyType_InitSlotDefs(PyInterpreterState *interp)
1125311253
if (interp != interp->runtime->interpreters.main) {
1125411254
return 0;
1125511255
}
11256-
PyObject *bytes = NULL;
11256+
PyObject *bytearray = NULL;
1125711257
PyObject *cache = PyDict_New();
1125811258
if (!cache) {
1125911259
return -1;
@@ -11262,51 +11262,43 @@ _PyType_InitSlotDefs(PyInterpreterState *interp)
1126211262
pytype_slotdef *p;
1126311263
Py_ssize_t idx = 0;
1126411264
for (p = slotdefs; p->name_strobj; p++, idx++) {
11265-
Py_hash_t hash = _PyObject_HashFast(p->name_strobj);
11266-
if (hash == -1) {
11267-
goto error;
11268-
}
1126911265
assert (idx < 255);
1127011266

11271-
if (_PyDict_GetItemRef_KnownHash_LockHeld((PyDictObject *)cache,
11272-
p->name_strobj, hash,
11273-
&bytes) < 0) {
11267+
if (PyDict_GetItemRef(cache, p->name_strobj, &bytearray) < 0) {
1127411268
goto error;
1127511269
}
1127611270

11277-
if (!bytes) {
11271+
if (!bytearray) {
1127811272
Py_ssize_t size = sizeof(uint8_t) * (1 + MAX_EQUIV);
11279-
bytes = PyBytes_FromStringAndSize(NULL, size);
11280-
if (!bytes) {
11273+
bytearray = PyByteArray_FromStringAndSize(NULL, size);
11274+
if (!bytearray) {
1128111275
goto error;
1128211276
}
1128311277

11284-
uint8_t *data = (uint8_t *)PyBytes_AS_STRING(bytes);
11278+
uint8_t *data = (uint8_t *)PyByteArray_AS_STRING(bytearray);
1128511279
data[0] = 0;
1128611280

11287-
if (_PyDict_SetItem_KnownHash_LockHeld((PyDictObject *)cache,
11288-
p->name_strobj,
11289-
bytes, hash) < 0) {
11281+
if (PyDict_SetItem(cache, p->name_strobj, bytearray) < 0) {
1129011282
goto error;
1129111283
}
1129211284
}
1129311285

11294-
assert (PyBytes_CheckExact(bytes));
11295-
uint8_t *data = (uint8_t *)PyBytes_AS_STRING(bytes);
11286+
assert (PyByteArray_CheckExact(bytearray));
11287+
uint8_t *data = (uint8_t *)PyByteArray_AS_STRING(bytearray);
1129611288

1129711289
data[0] += 1;
1129811290
assert (data[0] < MAX_EQUIV);
1129911291

1130011292
data[data[0]] = (uint8_t)idx;
1130111293

11302-
Py_CLEAR(bytes);
11294+
Py_CLEAR(bytearray);
1130311295
}
1130411296

1130511297
Py_ssize_t pos=0;
1130611298
PyObject *key=NULL;
1130711299
PyObject *value=NULL;
1130811300
while (PyDict_Next(cache, &pos, &key, &value)) {
11309-
uint8_t *data = (uint8_t *)PyBytes_AS_STRING(value);
11301+
uint8_t *data = (uint8_t *)PyByteArray_AS_STRING(value);
1131011302
uint8_t n = data[0];
1131111303
uint8_t i = 0;
1131211304
for(; i < n; i++) {
@@ -11319,7 +11311,7 @@ _PyType_InitSlotDefs(PyInterpreterState *interp)
1131911311
return 0;
1132011312

1132111313
error:
11322-
Py_XDECREF(bytes);
11314+
Py_XDECREF(bytearray);
1132311315
Py_DECREF(cache);
1132411316
return -1;
1132511317
}

0 commit comments

Comments
 (0)