Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ struct _ts {
_PyStackChunk *datastack_chunk;
PyObject **datastack_top;
PyObject **datastack_limit;
_PyStackChunk *datastack_cached_chunk;
/* XXX signal handlers should also be here */

/* The following fields are here to avoid allocation during init.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid a pathological case where repeated calls at a specific stack depth could be significantly slower.
30 changes: 26 additions & 4 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,7 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->datastack_chunk = NULL;
tstate->datastack_top = NULL;
tstate->datastack_limit = NULL;
tstate->datastack_cached_chunk = NULL;
tstate->what_event = -1;
tstate->current_executor = NULL;
tstate->jit_exit = NULL;
Expand Down Expand Up @@ -1714,6 +1715,11 @@ clear_datastack(PyThreadState *tstate)
_PyObject_VirtualFree(chunk, chunk->size);
chunk = prev;
}
if (tstate->datastack_cached_chunk != NULL) {
_PyObject_VirtualFree(tstate->datastack_cached_chunk,
tstate->datastack_cached_chunk->size);
tstate->datastack_cached_chunk = NULL;
}
}

void
Expand Down Expand Up @@ -3045,9 +3051,20 @@ push_chunk(PyThreadState *tstate, int size)
while (allocate_size < (int)sizeof(PyObject*)*(size + MINIMUM_OVERHEAD)) {
allocate_size *= 2;
}
_PyStackChunk *new = allocate_chunk(allocate_size, tstate->datastack_chunk);
if (new == NULL) {
return NULL;
_PyStackChunk *new;
if (tstate->datastack_cached_chunk != NULL
&& (size_t)allocate_size <= tstate->datastack_cached_chunk->size)
{
new = tstate->datastack_cached_chunk;
tstate->datastack_cached_chunk = NULL;
new->previous = tstate->datastack_chunk;
new->top = 0;
}
else {
new = allocate_chunk(allocate_size, tstate->datastack_chunk);
if (new == NULL) {
return NULL;
}
}
if (tstate->datastack_chunk) {
tstate->datastack_chunk->top = tstate->datastack_top -
Expand Down Expand Up @@ -3083,12 +3100,17 @@ _PyThreadState_PopFrame(PyThreadState *tstate, _PyInterpreterFrame * frame)
if (base == &tstate->datastack_chunk->data[0]) {
_PyStackChunk *chunk = tstate->datastack_chunk;
_PyStackChunk *previous = chunk->previous;
_PyStackChunk *cached = tstate->datastack_cached_chunk;
// push_chunk ensures that the root chunk is never popped:
assert(previous);
tstate->datastack_top = &previous->data[previous->top];
tstate->datastack_chunk = previous;
_PyObject_VirtualFree(chunk, chunk->size);
tstate->datastack_limit = (PyObject **)(((char *)previous) + previous->size);
chunk->previous = NULL;
if (cached != NULL) {
_PyObject_VirtualFree(cached, cached->size);
}
tstate->datastack_cached_chunk = chunk;
}
else {
assert(tstate->datastack_top);
Expand Down
Loading