Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6989ba0

Browse files
committed
Issue #19581: Change the overallocation factor of _PyUnicodeWriter on Windows
On Windows, a factor of 50% gives best performances.
1 parent f47981f commit 6989ba0

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

Objects/unicodeobject.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13106,6 +13106,13 @@ int
1310613106
_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1310713107
Py_ssize_t length, Py_UCS4 maxchar)
1310813108
{
13109+
#ifdef MS_WINDOWS
13110+
/* On Windows, overallocate by 50% is the best factor */
13111+
# define OVERALLOCATE_FACTOR 2
13112+
#else
13113+
/* On Linux, overallocate by 25% is the best factor */
13114+
# define OVERALLOCATE_FACTOR 4
13115+
#endif
1310913116
Py_ssize_t newlen;
1311013117
PyObject *newbuffer;
1311113118

@@ -13121,9 +13128,10 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1312113128

1312213129
if (writer->buffer == NULL) {
1312313130
assert(!writer->readonly);
13124-
if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) {
13125-
/* overallocate 25% to limit the number of resize */
13126-
newlen += newlen / 4;
13131+
if (writer->overallocate
13132+
&& newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) {
13133+
/* overallocate to limit the number of realloc() */
13134+
newlen += newlen / OVERALLOCATE_FACTOR;
1312713135
}
1312813136
if (newlen < writer->min_length)
1312913137
newlen = writer->min_length;
@@ -13133,9 +13141,10 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1313313141
return -1;
1313413142
}
1313513143
else if (newlen > writer->size) {
13136-
if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) {
13137-
/* overallocate 25% to limit the number of resize */
13138-
newlen += newlen / 4;
13144+
if (writer->overallocate
13145+
&& newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) {
13146+
/* overallocate to limit the number of realloc() */
13147+
newlen += newlen / OVERALLOCATE_FACTOR;
1313913148
}
1314013149
if (newlen < writer->min_length)
1314113150
newlen = writer->min_length;
@@ -13169,6 +13178,8 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1316913178
}
1317013179
_PyUnicodeWriter_Update(writer);
1317113180
return 0;
13181+
13182+
#undef OVERALLOCATE_FACTOR
1317213183
}
1317313184

1317413185
Py_LOCAL_INLINE(int)

0 commit comments

Comments
 (0)