From 9ccf721435215e240c80b9b52d3723014600587f Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Fri, 18 Jul 2025 19:35:51 -0700 Subject: [PATCH] refactor(sidebar): avoid temporary table in Sidebar:update_content() Under the hood vim.list_extend() does the same loop over the source table and uses table.insert() to append the values to the destination table, so using a temporary table makes little sense. Switch to directly inserting (appending) values into history_lines table. --- lua/avante/sidebar.lua | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 016274c..b5e5561 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1579,18 +1579,11 @@ function Sidebar:update_content(content, opts) history_lines = vim.deepcopy(self._cached_history_lines) end - -- 批量处理内容行,减少表操作 if content ~= nil and content ~= "" then - local content_lines = vim.split(content, "\n") - local new_lines = { Line:new({ { "" } }) } - - -- 预分配表大小,提升性能 - for i = 1, #content_lines do - new_lines[i + 1] = Line:new({ { content_lines[i] } }) + table.insert(history_lines, Line:new({ { "" } })) + for _, line in ipairs(vim.split(content, "\n")) do + table.insert(history_lines, Line:new({ { line } })) end - - -- 一次性扩展,而不是逐个插入 - vim.list_extend(history_lines, new_lines) end -- 使用 vim.schedule 而不是 vim.defer_fn(0),性能更好