Skip to content

Commit c9abefd

Browse files
committed
Restore min_size optimization
1 parent 90981ee commit c9abefd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Objects/stringlib/codecs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
350350
break;
351351

352352
case _Py_ERROR_BACKSLASHREPLACE:
353+
/* subtract preallocated bytes */
354+
writer->size -= max_char_size * (endpos - startpos);
353355
p = backslashreplace(writer, p,
354356
unicode, startpos, endpos);
355357
if (p == NULL)
@@ -358,6 +360,8 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
358360
break;
359361

360362
case _Py_ERROR_XMLCHARREFREPLACE:
363+
/* subtract preallocated bytes */
364+
writer->size -= max_char_size * (endpos - startpos);
361365
p = xmlcharrefreplace(writer, p,
362366
unicode, startpos, endpos);
363367
if (p == NULL)
@@ -396,6 +400,8 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
396400
}
397401
}
398402
else {
403+
/* subtract preallocated bytes */
404+
writer->size -= max_char_size * (newpos - startpos);
399405
/* Only overallocate the buffer if it's not the last write */
400406
writer->overallocate = (newpos < size);
401407
}

Objects/unicodeobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7414,6 +7414,8 @@ unicode_encode_ucs1(PyObject *unicode,
74147414
break;
74157415

74167416
case _Py_ERROR_BACKSLASHREPLACE:
7417+
/* subtract preallocated bytes */
7418+
writer->size -= (collend - collstart);
74177419
str = backslashreplace(writer, str,
74187420
unicode, collstart, collend);
74197421
if (str == NULL)
@@ -7422,6 +7424,8 @@ unicode_encode_ucs1(PyObject *unicode,
74227424
break;
74237425

74247426
case _Py_ERROR_XMLCHARREFREPLACE:
7427+
/* subtract preallocated bytes */
7428+
writer->size -= (collend - collstart);
74257429
str = xmlcharrefreplace(writer, str,
74267430
unicode, collstart, collend);
74277431
if (str == NULL)
@@ -7462,6 +7466,8 @@ unicode_encode_ucs1(PyObject *unicode,
74627466
}
74637467
}
74647468
else {
7469+
/* subtract preallocated bytes */
7470+
writer->size -= newpos - collstart;
74657471
/* Only overallocate the buffer if it's not the last write */
74667472
writer->overallocate = (newpos < size);
74677473
}

0 commit comments

Comments
 (0)