Skip to content

Commit ecdece1

Browse files
committed
Restore code to disable overallocation
1 parent 8dcf030 commit ecdece1

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Objects/stringlib/codecs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
326326
while ((endpos < size) && Py_UNICODE_IS_SURROGATE(data[endpos]))
327327
endpos++;
328328

329+
/* Only overallocate the buffer if it's not the last write */
330+
writer->overallocate = (endpos < size);
331+
329332
switch (error_handler)
330333
{
331334
case _Py_ERROR_REPLACE:
@@ -391,6 +394,10 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
391394
goto error;
392395
}
393396
}
397+
else {
398+
/* Only overallocate the buffer if it's not the last write */
399+
writer->overallocate = (newpos < size);
400+
}
394401

395402
char *rep_str;
396403
Py_ssize_t rep_len;
@@ -421,6 +428,10 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
421428

422429
i = newpos;
423430
}
431+
432+
/* If overallocation was disabled, ensure that it was the last
433+
write. Otherwise, we missed an optimization */
434+
assert(writer->overallocate || i == size);
424435
}
425436
else
426437
#if STRINGLIB_SIZEOF_CHAR > 2

Objects/unicodeobject.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7393,6 +7393,9 @@ unicode_encode_ucs1(PyObject *unicode,
73937393
while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit))
73947394
++collend;
73957395

7396+
/* Only overallocate the buffer if it's not the last write */
7397+
writer->overallocate = (collend < size);
7398+
73967399
/* cache callback name lookup (if not done yet, i.e. it's the first error) */
73977400
if (error_handler == _Py_ERROR_UNKNOWN)
73987401
error_handler = _Py_GetErrorHandler(errors);
@@ -7457,6 +7460,10 @@ unicode_encode_ucs1(PyObject *unicode,
74577460
goto onError;
74587461
}
74597462
}
7463+
else {
7464+
/* Only overallocate the buffer if it's not the last write */
7465+
writer->overallocate = (newpos < size);
7466+
}
74607467

74617468
char *rep_str;
74627469
Py_ssize_t rep_len;
@@ -7492,6 +7499,10 @@ unicode_encode_ucs1(PyObject *unicode,
74927499
pos = newpos;
74937500
Py_CLEAR(rep);
74947501
}
7502+
7503+
/* If overallocation was disabled, ensure that it was the last
7504+
write. Otherwise, we missed an optimization */
7505+
assert(writer->overallocate || pos == size);
74957506
}
74967507
}
74977508

0 commit comments

Comments
 (0)