patch 8.2.4823: concat more than 2 strings in :def function is inefficient
Problem: Concatenating more than 2 strings in a :def function is
inefficient.
Solution: Add a count to the CONCAT instruction. (closes #10276)
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 88a4d57..c1d5fb9 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -988,14 +988,12 @@
if (evalstr && (p = (char_u *)strstr((char *)str, "`=")) != NULL)
{
char_u *start = str;
+ int count = 0;
// Need to evaluate expressions of the form `=<expr>` in the string.
// Split the string into literal strings and Vim expressions and
// generate instructions to concatenate the literal strings and the
// result of evaluating the Vim expressions.
- val = vim_strsave((char_u *)"");
- generate_PUSHS(cctx, &val);
-
for (;;)
{
if (p > start)
@@ -1003,7 +1001,7 @@
// literal string before the expression
val = vim_strnsave(start, p - start);
generate_PUSHS(cctx, &val);
- generate_instr_drop(cctx, ISN_CONCAT, 1);
+ count++;
}
p += 2;
@@ -1011,7 +1009,7 @@
if (compile_expr0(&p, cctx) == FAIL)
return FAIL;
may_generate_2STRING(-1, TRUE, cctx);
- generate_instr_drop(cctx, ISN_CONCAT, 1);
+ count++;
p = skipwhite(p);
if (*p != '`')
@@ -1029,11 +1027,14 @@
{
val = vim_strsave(start);
generate_PUSHS(cctx, &val);
- generate_instr_drop(cctx, ISN_CONCAT, 1);
+ count++;
}
break;
}
}
+
+ if (count > 1)
+ generate_CONCAT(cctx, count);
}
else
{
@@ -2382,7 +2383,7 @@
if (*op == '.')
{
- if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
+ if (generate_CONCAT(cctx, 2) == FAIL)
goto theend;
}
else if (*op == '+')