https://github.com/python/cpython/commit/1639d934b9180c278ac9c02be43cbb1beada494a
commit: 1639d934b9180c278ac9c02be43cbb1beada494a
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2024-10-10T12:11:06+02:00
summary:

gh-125196: Add a free list to PyUnicodeWriter (#125227)

files:
M Include/internal/pycore_freelist_state.h
M Objects/object.c
M Objects/unicodeobject.c

diff --git a/Include/internal/pycore_freelist_state.h 
b/Include/internal/pycore_freelist_state.h
index 762c583ce94e9a..4e04cf431e0b31 100644
--- a/Include/internal/pycore_freelist_state.h
+++ b/Include/internal/pycore_freelist_state.h
@@ -20,6 +20,7 @@ extern "C" {
 #  define Py_async_gen_asends_MAXFREELIST 80
 #  define Py_futureiters_MAXFREELIST 255
 #  define Py_object_stack_chunks_MAXFREELIST 4
+#  define Py_unicode_writers_MAXFREELIST 1
 
 // A generic freelist of either PyObjects or other data structures.
 struct _Py_freelist {
@@ -44,6 +45,7 @@ struct _Py_freelists {
     struct _Py_freelist async_gen_asends;
     struct _Py_freelist futureiters;
     struct _Py_freelist object_stack_chunks;
+    struct _Py_freelist unicode_writers;
 };
 
 #ifdef __cplusplus
diff --git a/Objects/object.c b/Objects/object.c
index 8d809158a6c1da..a97a900890320d 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -862,6 +862,7 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, 
int is_finalization)
         // stacks during GC, so emptying the free-list is counterproductive.
         clear_freelist(&freelists->object_stack_chunks, 1, PyMem_RawFree);
     }
+    clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free);
 }
 
 /*
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 93c1025b6a3cae..b94a74c2c688a9 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -46,6 +46,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
SOFTWARE.
 #include "pycore_codecs.h"        // _PyCodec_Lookup()
 #include "pycore_critical_section.h" // Py_*_CRITICAL_SECTION_SEQUENCE_FAST
 #include "pycore_format.h"        // F_LJUST
+#include "pycore_freelist.h"      // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
 #include "pycore_initconfig.h"    // _PyStatus_OK()
 #include "pycore_interp.h"        // PyInterpreterState.fs_codec
 #include "pycore_long.h"          // _PyLong_FormatWriter()
@@ -13436,9 +13437,13 @@ PyUnicodeWriter_Create(Py_ssize_t length)
     }
 
     const size_t size = sizeof(_PyUnicodeWriter);
-    PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
+    PyUnicodeWriter *pub_writer;
+    pub_writer = _Py_FREELIST_POP_MEM(unicode_writers);
     if (pub_writer == NULL) {
-        return (PyUnicodeWriter *)PyErr_NoMemory();
+        pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
+        if (pub_writer == NULL) {
+            return (PyUnicodeWriter *)PyErr_NoMemory();
+        }
     }
     _PyUnicodeWriter *writer = (_PyUnicodeWriter *)pub_writer;
 
@@ -13459,7 +13464,7 @@ void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
         return;
     }
     _PyUnicodeWriter_Dealloc((_PyUnicodeWriter*)writer);
-    PyMem_Free(writer);
+    _Py_FREELIST_FREE(unicode_writers, writer, PyMem_Free);
 }
 
 
@@ -13881,7 +13886,7 @@ PyUnicodeWriter_Finish(PyUnicodeWriter *writer)
 {
     PyObject *str = _PyUnicodeWriter_Finish((_PyUnicodeWriter*)writer);
     assert(((_PyUnicodeWriter*)writer)->buffer == NULL);
-    PyMem_Free(writer);
+    _Py_FREELIST_FREE(unicode_writers, writer, PyMem_Free);
     return str;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to