Skip to content

Commit

Permalink
patch 9.0.0548: reduce() with a compiled lambda could be faster
Browse files Browse the repository at this point in the history
Problem:    reduce() with a compiled lambda could be faster.
Solution:   Call eval_expr_typval() instead of call_func() directly.
  • Loading branch information
brammool committed Sep 22, 2022
1 parent 6d313be commit f1c60d4
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 34 deletions.
13 changes: 7 additions & 6 deletions src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,15 +638,14 @@ blob_insert_func(typval_T *argvars, typval_T *rettv)
}

/*
* reduce() Blob argvars[0] using the function 'funcname' with arguments in
* 'funcexe' starting with the initial value argvars[2] and return the result
* in 'rettv'.
* Implementaion of reduce() for Blob "argvars[0]" using the function "expr"
* starting with the optional initial value "argvars[2]" and return the result
* in "rettv".
*/
void
blob_reduce(
typval_T *argvars,
char_u *func_name,
funcexe_T *funcexe,
typval_T *expr,
typval_T *rettv)
{
blob_T *b = argvars[0].vval.v_blob;
Expand Down Expand Up @@ -684,7 +683,9 @@ blob_reduce(
argv[0] = *rettv;
argv[1].v_type = VAR_NUMBER;
argv[1].vval.v_number = blob_get(b, i);
r = call_func(func_name, -1, rettv, 2, argv, funcexe);

r = eval_expr_typval(expr, argv, 2, rettv);

clear_tv(&argv[0]);
if (r == FAIL || called_emsg != called_emsg_start)
return;
Expand Down
30 changes: 11 additions & 19 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -2999,15 +2999,14 @@ f_reverse(typval_T *argvars, typval_T *rettv)
}

/*
* reduce() List argvars[0] using the function 'funcname' with arguments in
* 'funcexe' starting with the initial value argvars[2] and return the result
* in 'rettv'.
* Implementation of reduce() for list "argvars[0]", using the function "expr"
* starting with the optional initial value argvars[2] and return the result in
* "rettv".
*/
static void
list_reduce(
typval_T *argvars,
char_u *func_name,
funcexe_T *funcexe,
typval_T *expr,
typval_T *rettv)
{
list_T *l = argvars[0].vval.v_list;
Expand Down Expand Up @@ -3049,7 +3048,9 @@ list_reduce(
argv[0] = *rettv;
argv[1] = li->li_tv;
rettv->v_type = VAR_UNKNOWN;
r = call_func(func_name, -1, rettv, 2, argv, funcexe);

r = eval_expr_typval(expr, argv, 2, rettv);

clear_tv(&argv[0]);
if (r == FAIL || called_emsg != called_emsg_start)
break;
Expand All @@ -3066,8 +3067,6 @@ list_reduce(
f_reduce(typval_T *argvars, typval_T *rettv)
{
char_u *func_name;
partial_T *partial = NULL;
funcexe_T funcexe;

if (in_vim9script()
&& check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL)
Expand All @@ -3084,10 +3083,7 @@ f_reduce(typval_T *argvars, typval_T *rettv)
if (argvars[1].v_type == VAR_FUNC)
func_name = argvars[1].vval.v_string;
else if (argvars[1].v_type == VAR_PARTIAL)
{
partial = argvars[1].vval.v_partial;
func_name = partial_name(partial);
}
func_name = partial_name(argvars[1].vval.v_partial);
else
func_name = tv_get_string(&argvars[1]);
if (func_name == NULL || *func_name == NUL)
Expand All @@ -3096,16 +3092,12 @@ f_reduce(typval_T *argvars, typval_T *rettv)
return;
}

CLEAR_FIELD(funcexe);
funcexe.fe_evaluate = TRUE;
funcexe.fe_partial = partial;

if (argvars[0].v_type == VAR_LIST)
list_reduce(argvars, func_name, &funcexe, rettv);
list_reduce(argvars, &argvars[1], rettv);
else if (argvars[0].v_type == VAR_STRING)
string_reduce(argvars, func_name, &funcexe, rettv);
string_reduce(argvars, &argvars[1], rettv);
else
blob_reduce(argvars, func_name, &funcexe, rettv);
blob_reduce(argvars, &argvars[1], rettv);
}

#endif // defined(FEAT_EVAL)
2 changes: 1 addition & 1 deletion src/proto/blob.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void blob_add(typval_T *argvars, typval_T *rettv);
void blob_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg);
void blob_filter_map(blob_T *blob_arg, filtermap_T filtermap, typval_T *expr, typval_T *rettv);
void blob_insert_func(typval_T *argvars, typval_T *rettv);
void blob_reduce(typval_T *argvars, char_u *func_name, funcexe_T *funcexe, typval_T *rettv);
void blob_reduce(typval_T *argvars, typval_T *expr, typval_T *rettv);
void blob_reverse(blob_T *b, typval_T *rettv);
void f_blob2list(typval_T *argvars, typval_T *rettv);
void f_list2blob(typval_T *argvars, typval_T *rettv);
Expand Down
2 changes: 1 addition & 1 deletion src/proto/strings.pro
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ char_u *concat_str(char_u *str1, char_u *str2);
char_u *string_quote(char_u *str, int function);
long string_count(char_u *haystack, char_u *needle, int ic);
void string_filter_map(char_u *str, filtermap_T filtermap, typval_T *expr, typval_T *rettv);
void string_reduce(typval_T *argvars, char_u *func_name, funcexe_T *funcexe, typval_T *rettv);
void string_reduce(typval_T *argvars, typval_T *expr, typval_T *rettv);
void f_byteidx(typval_T *argvars, typval_T *rettv);
void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
void f_charidx(typval_T *argvars, typval_T *rettv);
Expand Down
13 changes: 7 additions & 6 deletions src/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -932,15 +932,14 @@ string_filter_map(
}

/*
* reduce() String argvars[0] using the function 'funcname' with arguments in
* 'funcexe' starting with the initial value argvars[2] and return the result
* in 'rettv'.
* Implementation of reduce() for String "argvars[0]" using the function "expr"
* starting with the optional initial value "argvars[2]" and return the result
* in "rettv".
*/
void
string_reduce(
typval_T *argvars,
char_u *func_name,
funcexe_T *funcexe,
typval_T *expr,
typval_T *rettv)
{
char_u *p = tv_get_string(&argvars[0]);
Expand Down Expand Up @@ -971,7 +970,9 @@ string_reduce(
if (copy_first_char_to_tv(p, &argv[1]) == FAIL)
break;
len = (int)STRLEN(argv[1].vval.v_string);
r = call_func(func_name, -1, rettv, 2, argv, funcexe);

r = eval_expr_typval(expr, argv, 2, rettv);

clear_tv(&argv[0]);
clear_tv(&argv[1]);
if (r == FAIL || called_emsg != called_emsg_start)
Expand Down
2 changes: 1 addition & 1 deletion src/testdir/test_listdict.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ func Test_reduce()

call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E1098:')
call assert_fails("call reduce(0, { acc, val -> acc + val }, 1)", 'E1098:')
call assert_fails("call reduce([1, 2], 'Xdoes_not_exist')", 'E117:')
call assert_fails("call reduce([1, 2], 'Xdoes_not_exist')", 'E121:')
call assert_fails("echo reduce(0z01, { acc, val -> 2 * acc + val }, '')", 'E1210:')

call assert_fails("vim9 reduce(0, (acc, val) => (acc .. val), '')", 'E1252:')
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
548,
/**/
547,
/**/
Expand Down

0 comments on commit f1c60d4

Please sign in to comment.