Commit 8a8e05a2 authored by Fredrik Lundh's avatar Fredrik Lundh

needforspeed: use memcpy for "long" strings; use a better algorithm

for long repeats.
parent f1d60a53
...@@ -352,14 +352,19 @@ typedef PY_UNICODE_TYPE Py_UNICODE; ...@@ -352,14 +352,19 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
Py_UNICODE_ISDIGIT(ch) || \ Py_UNICODE_ISDIGIT(ch) || \
Py_UNICODE_ISNUMERIC(ch)) Py_UNICODE_ISNUMERIC(ch))
/* memcpy has a considerable setup overhead on many platforms; use a
loop for short strings (the "16" below is pretty arbitary) */
#define Py_UNICODE_COPY(target, source, length) do\ #define Py_UNICODE_COPY(target, source, length) do\
{int i; Py_UNICODE *t = (target); const Py_UNICODE *s = (source);\ {Py_ssize_t i_; Py_UNICODE *t_ = (target); const Py_UNICODE *s_ = (source);\
for (i = 0; i < (length); i++) t[i] = s[i];\ if (length > 16)\
memcpy(t_, s_, (length)*sizeof(Py_UNICODE));\
else\
for (i_ = 0; i_ < (length); i_++) t_[i_] = s_[i_];\
} while (0) } while (0)
#define Py_UNICODE_FILL(target, value, length) do\ #define Py_UNICODE_FILL(target, value, length) do\
{int i; Py_UNICODE *t = (target); Py_UNICODE v = (value);\ {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\
for (i = 0; i < (length); i++) t[i] = v;\ for (i_ = 0; i_ < (length); i_++) t_[i_] = v_;\
} while (0) } while (0)
#define Py_UNICODE_MATCH(string, offset, substring)\ #define Py_UNICODE_MATCH(string, offset, substring)\
......
...@@ -5900,11 +5900,18 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) ...@@ -5900,11 +5900,18 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
if (str->length == 1 && len > 0) { if (str->length == 1 && len > 0) {
Py_UNICODE_FILL(p, str->str[0], len); Py_UNICODE_FILL(p, str->str[0], len);
} else } else {
while (len-- > 0) { int done = 0; /* number of characters copied this far */
if (done < nchars) {
Py_UNICODE_COPY(p, str->str, str->length); Py_UNICODE_COPY(p, str->str, str->length);
p += str->length; done = str->length;
} }
while (done < nchars) {
int n = (done <= nchars-done) ? done : nchars-done;
Py_UNICODE_COPY(p+done, p, n);
done += n;
}
}
return (PyObject*) u; return (PyObject*) u;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment