-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
GH-100240: Generic freelist, applied to ints #101453
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
Changes from 1 commit
e953c57
cf99ee4
8a6e6a3
fe65f49
a33061f
6b312e0
1d81198
5609e30
b8b1879
460d12c
2e2a861
42ee27f
a9e76ad
1134727
78592b9
044743e
827b81b
51c6c3c
cfb886d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#include "pycore_bitutils.h" // _Py_popcount32() | ||
#include "pycore_initconfig.h" // _PyStatus_OK() | ||
10000 | #include "pycore_long.h" // _Py_SmallInts | |
#include "pycore_pymem.h" // Free lists | ||
#include "pycore_object.h" // _PyObject_InitVar() | ||
#include "pycore_pystate.h" // _Py_IsMainInterpreter() | ||
#include "pycore_runtime.h" // _PY_NSMALLPOSINTS | ||
|
@@ -152,16 +153,26 @@ _PyLong_New(Py_ssize_t size) | |
"too many digits in integer"); | ||
return NULL; | ||
} | ||
/* Fast operations for single digit integers (including zero) | ||
* assume that there is always at least one digit present. */ | ||
Py_ssize_t ndigits = size ? size : 1; | ||
/* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + | ||
sizeof(digit)*size. Previous incarnations of this code used | ||
sizeof(PyVarObject) instead of the offsetof, but this risks being | ||
incorrect in the presence of padding between the PyVarObject header | ||
and the digits. */ | ||
result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) + | ||
ndigits*sizeof(digit)); | ||
assert(size >= 0); | ||
#if WITH_FREELISTS | ||
if (size <= 1) { | ||
PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
result = (PyLongObject *)_PyInterpreterState_FreelistAlloc(interp, sizeof(PyLongObject)); | ||
} | ||
#else | ||
if (size == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't change it for this PR, but this shouldn't be needed, as there should only be one zero. I think this indicates a bug in the caller. |
||
result = PyObject_Malloc(sizeof(PyLongObject)); | ||
} | ||
#endif | ||
else { | ||
/* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + | ||
sizeof(digit)*size. Previous incarnations of this code used | ||
sizeof(PyVarObject) instead of the offsetof, but this risks being | ||
incorrect in the presence of padding between the PyVarObject header | ||
and the digits. */ | ||
result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) + | ||
size*sizeof(digit)); | ||
} | ||
if (!result) { | ||
PyErr_NoMemory(); | ||
return NULL; | ||
|
@@ -202,10 +213,14 @@ _PyLong_FromMedium(sdigit x) | |
assert(!IS_SMALL_INT(x)); | ||
assert(is_medium_int(x)); | ||
/* We could use a freelist here */ | ||
#if WITH_FREELISTS | ||
PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
PyLongObject *v = (PyLongObject *)_PyInterpreterState_FreelistAlloc(interp, sizeof(PyLongObject)); | ||
#else | ||
PyLongObject *v = PyObject_Malloc(sizeof(PyLongObject)); | ||
#endif | ||
if (v == NULL) { | ||
PyErr_NoMemory(); | ||
return NULL; | ||
return PyErr_NoMemory(); | ||
} | ||
Py_ssize_t sign = x < 0 ? -1: 1; | ||
digit abs_x = x < 0 ? -x : x; | ||
|
@@ -267,6 +282,21 @@ _PyLong_FromSTwoDigits(stwodigits x) | |
return _PyLong_FromLarge(x); | ||
} | ||
|
||
static void | ||
int_dealloc(PyLongObject *op) | ||
{ | ||
#if WITH_FREELISTS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we move the Personally, I think we should just remove |
||
if (PyLong_CheckExact(op) && IS_MEDIUM_VALUE(op)) { | ||
PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
_PyInterpreterState_FreelistFree(interp, (PyObject*)op, sizeof(PyLongObject)); | ||
} | ||
else | ||
#endif | ||
{ | ||
Py_TYPE(op)->tp_free((PyObject *)op); | ||
} | ||
} | ||
|
||
int | ||
_PyLong_AssignValue(PyObject **target, Py_ssize_t value) | ||
{ | ||
|
@@ -6289,7 +6319,7 @@ PyTypeObject PyLong_Type = { | |
"int", /* tp_name */ | ||
offsetof(PyLongObject, long_value.ob_digit), /* tp_basicsize */ | ||
sizeof(digit), /* tp_itemsize */ | ||
0, /* tp_dealloc */ | ||
(destructor)int_dealloc, /* tp_dealloc */ | ||
0, /* tp_vectorcall_offset */ | ||
0, /* tp_getattr */ | ||
0, /* tp_setattr */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -717,6 +717,70 @@ PyObject_Free(void *ptr) | |
# define LIKELY(value) (value) | ||
#endif | ||
|
||
|
||
#if WITH_FREELISTS | ||
void * | ||
_PyFreeList_HalfFillAndAllocate(_PyFreeList *list) | ||
{ | ||
assert(list->ptr == NULL); | ||
if (list->capacity < 4) { | ||
return PyObject_Malloc(list->size); | ||
} | ||
uint32_t i = 0; | ||
for (; i < list->space>>1; i++) { | ||
void* ptr = PyObject_Malloc(list->size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for future PR: we should add the bulk allocate/free capability to the allocator. |
||
if (ptr == NULL) { | ||
break; | ||
} | ||
*((void**)ptr) = list->ptr; | ||
list->ptr = ptr; | ||
} | ||
if (i == 0) { | ||
return NULL; | ||
} | ||
list->space -= (i-1); | ||
iritkatriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void *result = list->ptr; | ||
list->ptr = *((void **)result); | ||
return result; | ||
} | ||
|
||
void | ||
_PyFreeList_Clear(_PyFreeList *list) | ||
{ | ||
int space = 0; | ||
void *head = list->ptr; | ||
while (head) { | ||
void *next = *((void**)head); | ||
PyObject_Free(head); | ||
head = next; | ||
space++; | ||
} | ||
list->ptr = NULL; | ||
list->space += space; | ||
} | ||
|
||
void | ||
_PyFreeList_FreeToFull(_PyFreeList *list, void *ptr) | ||
{ | ||
assert(list->space == 0); | ||
if (list->ptr == NULL) { | ||
PyObject_Free(ptr); | ||
return; | ||
} | ||
int space = 0; | ||
void *head = list->ptr; | ||
while (head) { | ||
void *next = *((void**)head); | ||
PyObject_Free(head); | ||
head = next; | ||
space++; | ||
} | ||
list->ptr = ptr; | ||
*((void **)ptr) = NULL; | ||
list->space = space-1; | ||
iritkatriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
#endif /* WITH_FREELISTS */ | ||
|
||
#ifdef WITH_PYMALLOC | ||
|
||
#ifdef WITH_VALGRIND | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
size
?. The assumption is that it is size in bytes, but I think you are using size in machine words.Also needs to be
Py_ssize_t
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's the results of sizeof(PyLongObject), that should be bytes no?