Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] Optimization for mb_metastrlenend()



The missing files

-- 
  Sebastian Gniazdowski
  psprint@xxxxxxxxxxxx
diff --git a/Src/utils.c b/Src/utils.c
index db43529..5bc9ef4 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -5323,7 +5323,7 @@ mb_metastrlenend(char *ptr, int width, char *eptr)
     char inchar, *laststart;
     size_t ret;
     wchar_t wc;
-    int num, num_in_char;
+    int num, num_in_char, complete;
 
     if (!isset(MULTIBYTE))
 	return ztrlen(ptr);
@@ -5331,6 +5331,7 @@ mb_metastrlenend(char *ptr, int width, char *eptr)
     laststart = ptr;
     ret = MB_INVALID;
     num = num_in_char = 0;
+    complete = 1;
 
     memset(&mb_shiftstate, 0, sizeof(mb_shiftstate));
     while (*ptr && !(eptr && ptr >= eptr)) {
@@ -5339,6 +5340,14 @@ mb_metastrlenend(char *ptr, int width, char *eptr)
 	else
 	    inchar = *ptr;
 	ptr++;
+
+        if ( complete && ( inchar >= 0 && inchar <= 0x7f ) ) {
+            num ++;
+            laststart = ptr;
+            num_in_char = 0;
+            continue;
+        }
+
 	ret = mbrtowc(&wc, &inchar, 1, &mb_shiftstate);
 
 	if (ret == MB_INCOMPLETE) {
@@ -5358,6 +5367,7 @@ mb_metastrlenend(char *ptr, int width, char *eptr)
 	     * so we don't count characters twice.
 	     */
 	    num_in_char++;
+            complete = 0;
 	} else {
 	    if (ret == MB_INVALID) {
 		/* Reset, treat as single character */
@@ -5378,8 +5388,10 @@ mb_metastrlenend(char *ptr, int width, char *eptr)
 		}
 	    } else
 		num++;
+
 	    laststart = ptr;
 	    num_in_char = 0;
+            complete = 1;
 	}
     }
 
git clone https://github.com/psprint/history-search-multi-word.git
cd test; ./parse.zsh ./to-parse.zsh

823 lines parsed with modified, optimized zsh-syntax-highlighting code

After optimization, minimum obtainable time:

Running time: 2.0280520000
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1        2027,49  2027,49  100,00%   1898,46  1898,46   93,63%  -hsmw-highlight-process
 2)  754         109,36     0,15    5,39%    109,36     0,15    5,39%  -hsmw-highlight-main-type
 3)  395          11,57     0,03    0,57%     11,57     0,03    0,57%  -hsmw-highlight-check-path
 4)   22           5,78     0,26    0,28%      5,78     0,26    0,28%  -hsmw-highlight-string
 5)    6           2,33     0,39    0,11%      2,33     0,39    0,11%  -hsmw-highlight-dollar-string
 6)    1           0,07     0,07    0,00%      0,07     0,07    0,00%  -hsmw-highlight-fill-option-variables
 7)    1           0,01     0,01    0,00%      0,01     0,01    0,00%  -hsmw-highlight-init

Before optimization, minimum obtainable time:

Running time: 2.2383990000
num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1        2237,79  2237,79  100,00%   2104,55  2104,55   94,04%  -hsmw-highlight-process
 2)  754         113,73     0,15    5,08%    113,73     0,15    5,08%  -hsmw-highlight-main-type
 3)  395          11,24     0,03    0,50%     11,24     0,03    0,50%  -hsmw-highlight-check-path
 4)   22           6,02     0,27    0,27%      6,02     0,27    0,27%  -hsmw-highlight-string
 5)    6           2,26     0,38    0,10%      2,26     0,38    0,10%  -hsmw-highlight-dollar-string
 6)    1           0,07     0,07    0,00%      0,07     0,07    0,00%  -hsmw-highlight-fill-option-variables
 7)    1           0,01     0,01    0,00%      0,01     0,01    0,00%  -hsmw-highlight-init



Messages sorted by: Reverse Date, Date, Thread, Author