Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7254e5a

Browse files
committed
_PyObject_GetDictPtr():
+ Use the _PyObject_VAR_SIZE macro to compute object size. + Break the computation into lines convenient for debugger inspection. + Speed the round-up-to-pointer-size computation.
1 parent 8c18f25 commit 7254e5a

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

Objects/object.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,15 +1157,19 @@ _PyObject_GetDictPtr(PyObject *obj)
11571157
if (dictoffset == 0)
11581158
return NULL;
11591159
if (dictoffset < 0) {
1160-
dictoffset += tp->tp_basicsize;
1161-
dictoffset += tp->tp_itemsize * ((PyVarObject *)obj)->ob_size;
1160+
/* dictoffset is positive by the time we're ready to round
1161+
it, and compilers can generate faster rounding code if
1162+
they know that. */
1163+
unsigned long udo; /* unsigned dictoffset */
1164+
const long nitems = ((PyVarObject *)obj)->ob_size;
1165+
const long size = _PyObject_VAR_SIZE(tp, nitems);
1166+
1167+
dictoffset += size;
11621168
assert(dictoffset > 0); /* Sanity check */
1163-
/* Round up, if necessary */
1164-
if (dictoffset % PTRSIZE != 0) {
1165-
dictoffset /= PTRSIZE;
1166-
dictoffset += 1;
1167-
dictoffset *= PTRSIZE;
1168-
}
1169+
/* Round up to multiple of PTRSIZE. */
1170+
udo = (unsigned long)dictoffset;
1171+
udo = ((udo + PTRSIZE-1) / PTRSIZE) * PTRSIZE;
1172+
dictoffset = (long)udo;
11691173
}
11701174
return (PyObject **) ((char *)obj + dictoffset);
11711175
}

0 commit comments

Comments
 (0)