Mercurial > vim
changeset 30425:6c2bbd7d9217 v9.0.0548
patch 9.0.0548: reduce() with a compiled lambda could be faster
Commit: https://github.com/vim/vim/commit/f1c60d4bf10794265b828afd9c5f7eddacada10b
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Sep 22 17:07:00 2022 +0100
patch 9.0.0548: reduce() with a compiled lambda could be faster
Problem: reduce() with a compiled lambda could be faster.
Solution: Call eval_expr_typval() instead of call_func() directly.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 22 Sep 2022 18:15:03 +0200 |
parents | 1a518a4bf1fd |
children | d083c8d6f256 |
files | src/blob.c src/list.c src/proto/blob.pro src/proto/strings.pro src/strings.c src/testdir/test_listdict.vim src/version.c |
diffstat | 7 files changed, 30 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/src/blob.c +++ b/src/blob.c @@ -638,15 +638,14 @@ blob_insert_func(typval_T *argvars, typv } /* - * 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; @@ -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;
--- a/src/list.c +++ b/src/list.c @@ -2999,15 +2999,14 @@ f_reverse(typval_T *argvars, typval_T *r } /* - * 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; @@ -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; @@ -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) @@ -3084,10 +3083,7 @@ f_reduce(typval_T *argvars, typval_T *re 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) @@ -3096,16 +3092,12 @@ f_reduce(typval_T *argvars, typval_T *re 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)
--- a/src/proto/blob.pro +++ b/src/proto/blob.pro @@ -22,7 +22,7 @@ void blob_add(typval_T *argvars, typval_ 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);
--- a/src/proto/strings.pro +++ b/src/proto/strings.pro @@ -23,7 +23,7 @@ char_u *concat_str(char_u *str1, char_u 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);
--- a/src/strings.c +++ b/src/strings.c @@ -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]); @@ -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)
--- a/src/testdir/test_listdict.vim +++ b/src/testdir/test_listdict.vim @@ -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:')