Skip to content

Commit 2e54db5

Browse files
Yhg1smeta-codesync[bot]
authored andcommitted
Cache one datachunk per tstate to prevent alloc/dealloc thrashing
Summary: Cache one datachunk per tstate to prevent alloc/dealloc thrashing when repeatedly hitting the same call depth at exactly the wrong boundary. Upstream issue: python/cpython#142183 Upstream PR: python/cpython#145789 Reviewed By: itamaro Differential Revision: D96052502 fbshipit-source-id: 883f6148568b6cca5a71dd7d754eb206fbb56f90
1 parent 42de8cf commit 2e54db5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

cinderx/UpstreamBorrow/borrowed-3.14.free-threading.gen_cached.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7698,9 +7698,20 @@ push_chunk(PyThreadState *tstate, int size)
76987698
while (allocate_size < (int)sizeof(PyObject*)*(size + MINIMUM_OVERHEAD)) {
76997699
allocate_size *= 2;
77007700
}
7701-
_PyStackChunk *new = allocate_chunk(allocate_size, tstate->datastack_chunk);
7702-
if (new == NULL) {
7703-
return NULL;
7701+
_PyStackChunk *new;
7702+
if (tstate->datastack_cached_chunk != NULL
7703+
&& (size_t)allocate_size <= tstate->datastack_cached_chunk->size)
7704+
{
7705+
new = tstate->datastack_cached_chunk;
7706+
tstate->datastack_cached_chunk = NULL;
7707+
new->previous = tstate->datastack_chunk;
7708+
new->top = 0;
7709+
}
7710+
else {
7711+
new = allocate_chunk(allocate_size, tstate->datastack_chunk);
7712+
if (new == NULL) {
7713+
return NULL;
7714+
}
77047715
}
77057716
if (tstate->datastack_chunk) {
77067717
tstate->datastack_chunk->top = tstate->datastack_top -

0 commit comments

Comments
 (0)