]> granicus.if.org Git - python/commitdiff
needforspeed: speed up unicode repeat, unicode string copy
authorFredrik Lundh <fredrik@pythonware.com>
Mon, 22 May 2006 16:29:30 +0000 (16:29 +0000)
committerFredrik Lundh <fredrik@pythonware.com>
Mon, 22 May 2006 16:29:30 +0000 (16:29 +0000)
Include/unicodeobject.h
Objects/unicodeobject.c

index 9012257fdd8af7f069680b96d659732e2d5c970a..7917c689afe606cb8a6e24167bd466ef35a56559 100644 (file)
@@ -352,12 +352,15 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
         Py_UNICODE_ISDIGIT(ch) || \
         Py_UNICODE_ISNUMERIC(ch))
 
-#define Py_UNICODE_COPY(target, source, length)\
-    (memcpy((target), (source), (length)*sizeof(Py_UNICODE)))
+#define Py_UNICODE_COPY(target, source, length) do\
+    {int i; Py_UNICODE *t = (target); const Py_UNICODE *s = (source);\
+        for (i = 0; i < (length); i++) t[i] = s[i];\
+    } while (0)
 
 #define Py_UNICODE_FILL(target, value, length) do\
-    {int i; for (i = 0; i < (length); i++) (target)[i] = (value);}\
-    while (0)
+    {int i; Py_UNICODE *t = (target); Py_UNICODE v = (value);\
+        for (i = 0; i < (length); i++) t[i] = v;\
+    } while (0)
 
 #define Py_UNICODE_MATCH(string, offset, substring)\
     ((*((string)->str + (offset)) == *((substring)->str)) &&\
index 7d11f7de34013f4c9620d59057c8e4c5dfebf05d..6f04a6db39697673c659ddb2a01fc3b59c2b33f2 100644 (file)
@@ -5898,10 +5898,13 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
 
     p = u->str;
 
-    while (len-- > 0) {
-        Py_UNICODE_COPY(p, str->str, str->length);
-        p += str->length;
-    }
+    if (str->length == 1 && len > 0) {
+        Py_UNICODE_FILL(p, str->str[0], len);
+    } else
+        while (len-- > 0) {
+            Py_UNICODE_COPY(p, str->str, str->length);
+            p += str->length;
+        }
 
     return (PyObject*) u;
 }