Original file line numberDiff line numberDiff line change@@ -836,6 +836,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
836836if (v==NULL)
837837 goto error;
838838839+if (fmtcnt==0) {
840+/* last write: disable writer overallocation */
841+writer->overallocate=0;
842+ }
843+839844sign=0;
840845fill=' ';
841846switch (c) {
@@ -1056,6 +1061,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
10561061assert((res-before) ==alloc);
10571062#endif
10581063 } /* '%' */
1064+1065+/* If overallocation was disabled, ensure that it was the last
1066+ write. Otherwise, we missed an optimization */
1067+assert(writer->overallocate||fmtcnt==0||use_bytearray);
10591068 } /* until end */
1060106910611070if (argidx<arglen&& !dict) {
@@ -3746,14 +3755,6 @@ _PyBytes_Repeat(char* dest, Py_ssize_t len_dest,
3746375537473756// --- PyBytesWriter API -----------------------------------------------------
374837573749-structPyBytesWriter {
3750-charsmall_buffer[256];
3751-PyObject*obj;
3752-Py_ssize_tsize;
3753-intuse_bytearray;
3754-};
3755-3756-37573758staticinlinechar*
37583759byteswriter_data(PyBytesWriter*writer)
37593760{
@@ -3802,7 +3803,8 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int overallocate)
38023803return0;
38033804 }
380438053805-if (overallocate&& !writer->use_bytearray) {
3806+overallocate &= writer->overallocate;
3807+if (overallocate) {
38063808if (size <= (PY_SSIZE_T_MAX-size / OVERALLOCATE_FACTOR)) {
38073809size+=size / OVERALLOCATE_FACTOR;
38083810 }
@@ -3867,6 +3869,7 @@ byteswriter_create(Py_ssize_t size, int use_bytearray)
38673869writer->obj=NULL;
38683870writer->size=0;
38693871writer->use_bytearray=use_bytearray;
3872+writer->overallocate= !use_bytearray;
3870387338713874if (size >= 1) {
38723875if (byteswriter_resize(writer, size, 0) <0) {