]> granicus.if.org Git - vim/commitdiff
patch 8.2.1973: finding a patch number can be a bit slow v8.2.1973
authorBram Moolenaar <Bram@vim.org>
Tue, 10 Nov 2020 19:54:29 +0000 (20:54 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 10 Nov 2020 19:54:29 +0000 (20:54 +0100)
Problem:    Finding a patch number can be a bit slow.
Solution:   Use binary search. (closes #7279)

src/version.c

index e2affa79ed463cc7f5c0b9be824b09745951d307..8535ac554de6445b0a32314a731431bbd4ef81cc 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1973,
 /**/
     1972,
 /**/
@@ -4725,11 +4727,21 @@ highest_patch(void)
     int
 has_patch(int n)
 {
-    int                i;
+    int                h, m, l;
 
-    for (i = 0; included_patches[i] != 0; ++i)
-       if (included_patches[i] == n)
+    // Perform a binary search.
+    l = 0;
+    h = (int)(sizeof(included_patches) / sizeof(included_patches[0])) - 1;
+    while (l < h)
+    {
+       m = (l + h) / 2;
+       if (included_patches[m] == n)
            return TRUE;
+       if (included_patches[m] < n)
+           h = m;
+       else
+           l = m + 1;
+    }
     return FALSE;
 }
 #endif
@@ -4941,9 +4953,7 @@ list_version(void)
     {
        msg_puts(_("\nIncluded patches: "));
        first = -1;
-       // find last one
-       for (i = 0; included_patches[i] != 0; ++i)
-           ;
+       i = (int)(sizeof(included_patches) / sizeof(included_patches[0])) - 1;
        while (--i >= 0)
        {
            if (first < 0)