Skip to content
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
Prev Previous commit
Next Next commit
moved things around a little
  • Loading branch information
iritkatriel committed Jan 31, 2023
commit cf99ee400307037bf7b6fa9267235c36a5c09927
27 changes: 16 additions & 11 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ struct _Py_long_state {


/* interpreter state */
#define WITH_FREELISTS 1

#define SMALL_OBJECT_FREELIST_SIZE 1024
#define INTERP_NUM_FREELISTS 30
Expand Down Expand Up @@ -238,26 +237,32 @@ PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);

#define SIZE_TO_FREELIST_INDEX(size) ((size-4)/2)
Copy link
Copy Markdown
Member

@markshannon markshannon Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why divide by 2 and not 2 * sizeof(void *), which is the quantum of allocation for malloc and ob_malloc?
(Assuming that the size is in bytes)
Could you use the term "size class" or equivalent, instead of "freelist index" The mapping here is from sizes to classes (not all size classes get free lists).

Any reason not to make this an inline function?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, put an assert in this function, and put a size check in the caller.

#define FREELIST_INDEX_TO_SIZE(idx) (2*(idx) + 4)

#if WITH_FREELISTS
static inline PyObject*
_PyInterpreterState_FreelistAlloc(PyInterpreterState *interp, int size) {
assert(size >= 4);
assert((size & 0x1) == 0);
int index = (size-4)/2;
_PyInterpreterState_FreelistAlloc(PyInterpreterState *interp, Py_ssize_t size) {
#if WITH_FREELISTS
int index = SIZE_TO_FREELIST_INDEX(size);
assert(index >= 0 && index < INTERP_NUM_FREELISTS);
return _PyFreeList_Alloc(&interp->freelists[index]);
#else
return PyObject_Malloc(size);
#endif
}

static inline void
_PyInterpreterState_FreelistFree(PyInterpreterState * interp, PyObject *op, int size) {
_PyInterpreterState_FreelistFree(PyInterpreterState * interp, PyObject *op, Py_ssize_t size) {
#if WITH_FREELISTS
/* todo: assert the size is correct? */
Comment thread
iritkatriel marked this conversation as resolved.
assert(size >= 4);
assert((size & 0x1) == 0);
int index = (size-4)/2;
int index = SIZE_TO_FREELIST_INDEX(size);
assert(index >= 0 && index < INTERP_NUM_FREELISTS);
_PyFreeList_Alloc(&interp->freelists[index]);
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
#else
Py_TYPE(op)->tp_free((PyObject *)op);
#endif
}

#endif /* WITH_FREELISTS */


#ifdef __cplusplus
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ PyAPI_FUNC(int) _PyMem_GetAllocatorName(
PYMEM_ALLOCATOR_NOT_SET does nothing. */
PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);

#define WITH_FREELISTS 1

#if WITH_FREELISTS
/* Free lists.
Expand Down
2 changes: 1 addition & 1 deletion Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,9 @@ _PyFreeList_HalfFillAndAllocate(_PyFreeList *list)
if (i == 0) {
return NULL;
}
list->space -= (i-1);
void *result = list->ptr;
list->ptr = *((void **)result);
list->space -= (i-1);
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,9 @@ PyInterpreterState_New(void)

#if WITH_FREELISTS
for (int i=0; i < INTERP_NUM_FREELISTS; i++) {
_PyFreeList_Init(&interp->freelists[i], 4 + 2*i, SMALL_OBJECT_FREELIST_SIZE);
_PyFreeList_Init(&interp->freelists[i],
FREELIST_INDEX_TO_SIZE(i),
SMALL_OBJECT_FREELIST_SIZE);
}
#endif
Comment thread
iritkatriel marked this conversation as resolved.
Outdated

Expand Down