File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -326,6 +326,9 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
326
326
while ((endpos < size ) && Py_UNICODE_IS_SURROGATE (data [endpos ]))
327
327
endpos ++ ;
328
328
329
+ /* Only overallocate the buffer if it's not the last write */
330
+ writer -> overallocate = (endpos < size );
331
+
329
332
switch (error_handler )
330
333
{
331
334
case _Py_ERROR_REPLACE :
@@ -391,6 +394,10 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
391
394
goto error ;
392
395
}
393
396
}
397
+ else {
398
+ /* Only overallocate the buffer if it's not the last write */
399
+ writer -> overallocate = (newpos < size );
400
+ }
394
401
395
402
char * rep_str ;
396
403
Py_ssize_t rep_len ;
@@ -421,6 +428,10 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
421
428
422
429
i = newpos ;
423
430
}
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 );
424
435
}
425
436
else
426
437
#if STRINGLIB_SIZEOF_CHAR > 2
Original file line number Diff line number Diff line change @@ -7393,6 +7393,9 @@ unicode_encode_ucs1(PyObject *unicode,
7393
7393
while ((collend < size ) && (PyUnicode_READ (kind , data , collend ) >= limit ))
7394
7394
++ collend ;
7395
7395
7396
+ /* Only overallocate the buffer if it's not the last write */
7397
+ writer -> overallocate = (collend < size );
7398
+
7396
7399
/* cache callback name lookup (if not done yet, i.e. it's the first error) */
7397
7400
if (error_handler == _Py_ERROR_UNKNOWN )
7398
7401
error_handler = _Py_GetErrorHandler (errors );
@@ -7457,6 +7460,10 @@ unicode_encode_ucs1(PyObject *unicode,
7457
7460
goto onError ;
7458
7461
}
7459
7462
}
7463
+ else {
7464
+ /* Only overallocate the buffer if it's not the last write */
7465
+ writer -> overallocate = (newpos < size );
7466
+ }
7460
7467
7461
7468
char * rep_str ;
7462
7469
Py_ssize_t rep_len ;
@@ -7492,6 +7499,10 @@ unicode_encode_ucs1(PyObject *unicode,
7492
7499
pos = newpos ;
7493
7500
Py_CLEAR (rep );
7494
7501
}
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 );
7495
7506
}
7496
7507
}
7497
7508
You can’t perform that action at this time.
0 commit comments