Commit: patch 9.2.1137: blob2list() can be improved

2 views
Skip to first unread message

Christian Brabandt

unread,
Sep 28, 2026, 3:00:24 PM (6 days ago) Sep 28
to vim...@googlegroups.com
patch 9.2.1137: blob2list() can be improved

Commit: https://github.com/vim/vim/commit/0a2f4ba24bf411332559e64eaea69521b3b70886
Author: Yasuhiro Matsumoto <matt...@gmail.com>
Date: Mon Sep 28 18:53:31 2026 +0000

patch 9.2.1137: blob2list() can be improved

Problem: blob2list() appends list items one by one.
Solution: Preallocate the list items and set them directly
(Yasuhiro Matsumoto).

Add coverage for editing and freeing the converted list. In a local
five-run benchmark, conversion of 4096 and 65536 bytes takes about 40%
less time. The blob tests and focused Vim9 conversion tests pass.

closes: #21386

Signed-off-by: Yasuhiro Matsumoto <matt...@gmail.com>
Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/src/blob.c b/src/blob.c
index 1f9d470e0..a85df43d8 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -975,18 +975,29 @@ f_blob2list(typval_T *argvars, typval_T *rettv)
{
blob_T *blob;
list_T *l;
- long i;
-
- if (rettv_list_alloc(rettv) == FAIL)
- return;
+ int i, len;
+ typval_T tv;

if (check_for_blob_arg(argvars, 0) == FAIL)
+ {
+ (void)rettv_list_alloc(rettv);
return;
+ }

blob = argvars->vval.v_blob;
- l = rettv->vval.v_list;
- for (i = 0; i < (long)blob_len(blob); i++)
- list_append_number(l, blob_get(blob, (int)i));
+ len = blob_len(blob);
+ l = list_alloc_with_items(len);
+ if (l == NULL)
+ return;
+ rettv_list_set(rettv, l);
+
+ tv.v_type = VAR_NUMBER;
+ tv.v_lock = 0;
+ for (i = 0; i < len; i++)
+ {
+ tv.vval.v_number = blob_get(blob, i);
+ list_set_item(l, i, &tv);
+ }
}

/*
diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim
index c9917e53b..9c773f344 100644
--- a/src/testdir/test_blob.vim
+++ b/src/testdir/test_blob.vim
@@ -752,6 +752,20 @@ func Test_blob2list()
call assert_equal([], blob2list(test_null_blob()))
endfunc

+func Test_blob2list_edit_result()
+ let b = list2blob(range(256))
+ let l = blob2list(b)
+ call assert_equal(range(256), l)
+ call assert_equal(range(1, 254), remove(l, 1, 254))
+ call insert(l, 42, 1)
+ call add(l, 99)
+ call reverse(l)
+ call assert_equal([99, 255, 42, 0], l)
+ let l[1] = 'changed'
+ call assert_equal([99, 'changed', 42, 0], l)
+ call assert_equal(list2blob(range(256)), b)
+endfunc
+
" Tests for the list2blob() function
func Test_list2blob()
call assert_fails('let b = list2blob(0z10)', 'E1211: List required for argument 1')
diff --git a/src/version.c b/src/version.c
index 0e806fe05..0d18d8a7a 100644
--- a/src/version.c
+++ b/src/version.c
@@ -763,6 +763,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1137,
/**/
1136,
/**/
Reply all
Reply to author
Forward
0 new messages