Replace list_copy() + list_extend() (N+1 individual mallocs) with a single list_alloc_with_items(len1+len2) call. This reduces the number of memory allocations from O(N) to O(1) for the list '+' operator.
Before: 0.4190s
After: 0.2333s
let s:list_size = 100000 let s:iterations = 100 let s:l1 = range(s:list_size) let s:l2 = range(s:list_size) " Warm up for i in range(3) let _ = s:l1 + s:l2 endfor let s:start = reltime() for i in range(s:iterations) let _ = s:l1 + s:l2 endfor let s:elapsed = reltimefloat(reltime(s:start)) echo printf('list len: %d + %d, iterations: %d, total: %.4f s, per-op: %.6f s', \ len(s:l1), len(s:l2), s:iterations, s:elapsed, s:elapsed / s:iterations)
https://github.com/vim/vim/pull/19495
(1 file)
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@chrisbra commented on this pull request.
In src/list.c:
> l = list_alloc(); - else - l = list_copy(l1, FALSE, TRUE, 0); + if (l == NULL) + return FAIL; + ++l->lv_refcount; + tv->v_type = VAR_LIST; + tv->v_lock = 0; + tv->vval.v_list = l; + return OK; + } + + // allocate all items at once for efficiency + l = list_alloc_with_items(len1 + len2);
len1 + len2 can potentially overflow here
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@mattn pushed 1 commit.
—
View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@mattn commented on this pull request.
In src/list.c:
> l = list_alloc(); - else - l = list_copy(l1, FALSE, TRUE, 0); + if (l == NULL) + return FAIL; + ++l->lv_refcount; + tv->v_type = VAR_LIST; + tv->v_lock = 0; + tv->vval.v_list = l; + return OK; + } + + // allocate all items at once for efficiency + l = list_alloc_with_items(len1 + len2);
Thank you. Fixed.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()