]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0151: blob_from_string() is slow for long strings v9.2.0151
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Fri, 13 Mar 2026 17:36:34 +0000 (17:36 +0000)
committerChristian Brabandt <cb@256bit.org>
Fri, 13 Mar 2026 17:36:34 +0000 (17:36 +0000)
Problem:  blob_from_string() is slow for long strings
Solution: Use ga_grow() to allocate memory once, perform a bulk copy
          with mch_memmove() then translate NL to NUL in-place
          (Yasuhiro Matsumoto).

closes: #19665

Signed-off-by: Yasuhiro Matsumoto <mattn.jp@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/strings.c
src/version.c

index fb14e7de6a20fba3b2ab71acffebbaaca45aaa7e..c5e07590cdd2d5cdb1085540018a053c955a1cbf 100644 (file)
@@ -1232,13 +1232,20 @@ convert_string(string_T *str, char_u *from, char_u *to, string_T *ret)
     static void
 blob_from_string(char_u *str, blob_T *blob)
 {
-    char_u  *p;
+    int            len = (int)STRLEN(str);
+    char_u  *dest;
 
-    for (p = str; *p != NUL; ++p)
-    {
-       // Translate newlines in the string to NUL character
-       ga_append(&blob->bv_ga, (*p == NL) ? NUL : (int)*p);
-    }
+    if (len == 0)
+       return;
+    if (ga_grow(&blob->bv_ga, len) == FAIL)
+       return;
+    dest = (char_u *)blob->bv_ga.ga_data + blob->bv_ga.ga_len;
+    mch_memmove(dest, str, (size_t)len);
+    // Translate newlines in the string to NUL characters
+    for (int i = 0; i < len; ++i)
+       if (dest[i] == NL)
+           dest[i] = NUL;
+    blob->bv_ga.ga_len += len;
 }
 
 /*
index 37ef83b978878b50c7fc9808f2626498394e8a17..9969dc786cb163ab60e9de773c77af9f55db36c7 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    151,
 /**/
     150,
 /**/