From 6420615e3f703870ed898083f84d6e3515a8c279 Mon Sep 17 00:00:00 2001 From: Cédric Barreteau <> Date: Tue, 30 Jun 2020 18:39:54 +0200 Subject: vim-patch:8.2.0935: flattening a list with existing code is slow Problem: Flattening a list with existing code is slow. Solution: Add flatten(). (Mopp, closes vim/vim#3676) https://github.com/vim/vim/commit/077a1e670ad69ef4cefc22103ca6635bd269e764 --- src/nvim/eval/typval.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/nvim/eval/typval.c') diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 89ca2db59b..576948f052 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -640,6 +640,53 @@ tv_list_copy_error: return NULL; } +/// Flatten "list" in place to depth "maxdepth". +/// Does nothing if "maxdepth" is 0. +/// +/// @param[in] list List to flatten +/// @param[in] maxdepth Maximum depth that will be flattened +/// +/// @return OK or FAIL +int tv_list_flatten(list_T *list, long maxdepth) + FUNC_ATTR_WARN_UNUSED_RESULT +{ + listitem_T *item; + int n; + if (maxdepth == 0) { + return OK; + } + + n = 0; + item = list->lv_first; + while (item != NULL) { + fast_breakcheck(); + if (got_int) { + return FAIL; + } + if (item->li_tv.v_type == VAR_LIST) { + listitem_T *next = item->li_next; + + tv_list_drop_items(list, item, item); + tv_list_extend(list, item->li_tv.vval.v_list, next); + + if (item->li_prev == NULL) { + item = list->lv_first; + } else { + item = item->li_prev->li_next; + } + + if (++n >= maxdepth) { + n = 0; + item = next; + } + } else { + n = 0; + item = item->li_next; + } + } + return OK; +} + /// Extend first list with the second /// /// @param[out] l1 List to extend. -- cgit From fd57d69970e7947da0d69d744c13121668f91bd8 Mon Sep 17 00:00:00 2001 From: Cédric Barreteau <> Date: Tue, 30 Jun 2020 18:45:58 +0200 Subject: vim-patch:8.2.0937: asan failure in the flatten() test Problem: Asan failure in the flatten() test. Solution: Free the flattened list. https://github.com/vim/vim/commit/dcf59c37d0e1517439c4c0c4a6a5ca09c90157ad --- src/nvim/eval/typval.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/nvim/eval/typval.c') diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 576948f052..d5db15afcc 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -651,6 +651,7 @@ int tv_list_flatten(list_T *list, long maxdepth) FUNC_ATTR_WARN_UNUSED_RESULT { listitem_T *item; + listitem_T *to_free; int n; if (maxdepth == 0) { return OK; @@ -668,12 +669,15 @@ int tv_list_flatten(list_T *list, long maxdepth) tv_list_drop_items(list, item, item); tv_list_extend(list, item->li_tv.vval.v_list, next); + tv_clear(&item->li_tv); + to_free = item; if (item->li_prev == NULL) { item = list->lv_first; } else { item = item->li_prev->li_next; } + xfree(to_free); if (++n >= maxdepth) { n = 0; -- cgit From 9f7f42ea0a4ba85aa27f9d8dc3833e854ce54703 Mon Sep 17 00:00:00 2001 From: Cédric Barreteau <> Date: Tue, 7 Jul 2020 21:02:22 +0200 Subject: Prevent `flatten` from taking a null list --- src/nvim/eval/typval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/eval/typval.c') diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index d5db15afcc..e47403c656 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -648,7 +648,7 @@ tv_list_copy_error: /// /// @return OK or FAIL int tv_list_flatten(list_T *list, long maxdepth) - FUNC_ATTR_WARN_UNUSED_RESULT + FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT { listitem_T *item; listitem_T *to_free; -- cgit From 9464399c8c8af44eea9c723f090e782af0346a1a Mon Sep 17 00:00:00 2001 From: Cédric Barreteau <> Date: Mon, 20 Jul 2020 20:21:49 +0200 Subject: Fix documentation The list parameter is an an [in,out] rather than just an [in]. --- src/nvim/eval/typval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/eval/typval.c') diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index e47403c656..dcfd456ce3 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -643,7 +643,7 @@ tv_list_copy_error: /// Flatten "list" in place to depth "maxdepth". /// Does nothing if "maxdepth" is 0. /// -/// @param[in] list List to flatten +/// @param[in,out] list List to flatten /// @param[in] maxdepth Maximum depth that will be flattened /// /// @return OK or FAIL -- cgit