Skip to content

Commit f1d60a5

Browse files
author
Fredrik Lundh
committed
needforspeed: speed up unicode repeat, unicode string copy
1 parent d82c310 commit f1d60a5

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

Include/unicodeobject.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,15 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
352352
Py_UNICODE_ISDIGIT(ch) || \
353353
Py_UNICODE_ISNUMERIC(ch))
354354

355-
#define Py_UNICODE_COPY(target, source, length)\
356-
(memcpy((target), (source), (length)*sizeof(Py_UNICODE)))
355+
#define Py_UNICODE_COPY(target, source, length) do\
356+
{int i; Py_UNICODE *t = (target); const Py_UNICODE *s = (source);\
357+
for (i = 0; i < (length); i++) t[i] = s[i];\
358+
} while (0)
357359

358360
#define Py_UNICODE_FILL(target, value, length) do\
359-
{int i; for (i = 0; i < (length); i++) (target)[i] = (value);}\
360-
while (0)
361+
{int i; Py_UNICODE *t = (target); Py_UNICODE v = (value);\
362+
for (i = 0; i < (length); i++) t[i] = v;\
363+
} while (0)
361364

362365
#define Py_UNICODE_MATCH(string, offset, substring)\
363366
((*((string)->str + (offset)) == *((substring)->str)) &&\

Objects/unicodeobject.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5898,10 +5898,13 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
58985898

58995899
p = u->str;
59005900

5901-
while (len-- > 0) {
5902-
Py_UNICODE_COPY(p, str->str, str->length);
5903-
p += str->length;
5904-
}
5901+
if (str->length == 1 && len > 0) {
5902+
Py_UNICODE_FILL(p, str->str[0], len);
5903+
} else
5904+
while (len-- > 0) {
5905+
Py_UNICODE_COPY(p, str->str, str->length);
5906+
p += str->length;
5907+
}
59055908

59065909
return (PyObject*) u;
59075910
}

0 commit comments

Comments
 (0)