Skip to content

Commit 1639d93

Browse files
authored
gh-125196: Add a free list to PyUnicodeWriter (#125227)
1 parent 7a10cde commit 1639d93

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Include/internal/pycore_freelist_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern "C" {
2020
# define Py_async_gen_asends_MAXFREELIST 80
2121
# define Py_futureiters_MAXFREELIST 255
2222
# define Py_object_stack_chunks_MAXFREELIST 4
23+
# define Py_unicode_writers_MAXFREELIST 1
2324

2425
// A generic freelist of either PyObjects or other data structures.
2526
struct _Py_freelist {
@@ -44,6 +45,7 @@ struct _Py_freelists {
4445
struct _Py_freelist async_gen_asends;
4546
struct _Py_freelist futureiters;
4647
struct _Py_freelist object_stack_chunks;
48+
struct _Py_freelist unicode_writers;
4749
};
4850

4951
#ifdef __cplusplus

Objects/object.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization)
862862
// stacks during GC, so emptying the free-list is counterproductive.
863863
clear_freelist(&freelists->object_stack_chunks, 1, PyMem_RawFree);
864864
}
865+
clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free);
865866
}
866867

867868
/*

Objects/unicodeobject.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4646
#include "pycore_codecs.h" // _PyCodec_Lookup()
4747
#include "pycore_critical_section.h" // Py_*_CRITICAL_SECTION_SEQUENCE_FAST
4848
#include "pycore_format.h" // F_LJUST
49+
#include "pycore_freelist.h" // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
4950
#include "pycore_initconfig.h" // _PyStatus_OK()
5051
#include "pycore_interp.h" // PyInterpreterState.fs_codec
5152
#include "pycore_long.h" // _PyLong_FormatWriter()
@@ -13436,9 +13437,13 @@ PyUnicodeWriter_Create(Py_ssize_t length)
1343613437
}
1343713438

1343813439
const size_t size = sizeof(_PyUnicodeWriter);
13439-
PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
13440+
PyUnicodeWriter *pub_writer;
13441+
pub_writer = _Py_FREELIST_POP_MEM(unicode_writers);
1344013442
if (pub_writer == NULL) {
13441-
return (PyUnicodeWriter *)PyErr_NoMemory();
13443+
pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
13444+
if (pub_writer == NULL) {
13445+
return (PyUnicodeWriter *)PyErr_NoMemory();
13446+
}
1344213447
}
1344313448
_PyUnicodeWriter *writer = (_PyUnicodeWriter *)pub_writer;
1344413449

@@ -13459,7 +13464,7 @@ void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
1345913464
return;
1346013465
}
1346113466
_PyUnicodeWriter_Dealloc((_PyUnicodeWriter*)writer);
13462-
PyMem_Free(writer);
13467+
_Py_FREELIST_FREE(unicode_writers, writer, PyMem_Free);
1346313468
}
1346413469

1346513470

@@ -13881,7 +13886,7 @@ PyUnicodeWriter_Finish(PyUnicodeWriter *writer)
1388113886
{
1388213887
PyObject *str = _PyUnicodeWriter_Finish((_PyUnicodeWriter*)writer);
1388313888
assert(((_PyUnicodeWriter*)writer)->buffer == NULL);
13884-
PyMem_Free(writer);
13889+
_Py_FREELIST_FREE(unicode_writers, writer, PyMem_Free);
1388513890
return str;
1388613891
}
1388713892

0 commit comments

Comments
 (0)