]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
needforspeed: use memcpy for "long" strings; use a better algorithm
authorFredrik Lundh <fredrik@pythonware.com>
Mon, 22 May 2006 17:12:58 +0000 (17:12 +0000)
committerFredrik Lundh <fredrik@pythonware.com>
Mon, 22 May 2006 17:12:58 +0000 (17:12 +0000)
for long repeats.

Include/unicodeobject.h
Objects/unicodeobject.c

index 7917c689afe606cb8a6e24167bd466ef35a56559..c0036bf837eb8937c44ea408607374c7175fb8e6 100644 (file)
@@ -352,14 +352,19 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
         Py_UNICODE_ISDIGIT(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\
-    {int i; Py_UNICODE *t = (target); const Py_UNICODE *s = (source);\
-        for (i = 0; i < (length); i++) t[i] = s[i];\
+    {Py_ssize_t i_; Py_UNICODE *t_ = (target); const Py_UNICODE *s_ = (source);\
+      if (length > 16)\
+        memcpy(t_, s_, (length)*sizeof(Py_UNICODE));\
+      else\
+        for (i_ = 0; i_ < (length); i_++) t_[i_] = s_[i_];\
     } while (0)
 
 #define Py_UNICODE_FILL(target, value, length) do\
-    {int i; Py_UNICODE *t = (target); Py_UNICODE v = (value);\
-        for (i = 0; i < (length); i++) t[i] = v;\
+    {Py_ssize_t 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)\
index 6f04a6db39697673c659ddb2a01fc3b59c2b33f2..85cbed285d7b459711c51e67c5e0cc987d59aefc 100644 (file)
@@ -5900,11 +5900,18 @@ unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
 
     if (str->length == 1 && len > 0) {
         Py_UNICODE_FILL(p, str->str[0], len);
-    } else
-        while (len-- > 0) {
+    } else {
+       int done = 0; /* number of characters copied this far */
+       if (done < nchars) {
             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;
 }