Skip to content

Commit cfa4150

Browse files
committed
get_vim_item() 的lua 实现从 29ms → 18ms,172 个节点,主要是去掉不必要的 json_encode
1 parent 94e298d commit cfa4150

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

autoload/easycomplete/action/completion.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ function! s:GetLspCompletionResult(server_name, data, plugin_name, word) abort
7070
" 192 个元素
7171
" vim 用时 55 ms
7272
" lua 用时 29 ms
73+
let tt = reltime()
7374
if g:env_is_nvim
7475
let l:completion_result = s:util_toolkit.get_vim_complete_items(l:response, a:plugin_name, a:word)
7576
else
7677
let l:completion_result = easycomplete#util#GetVimCompletionItems(l:response, a:plugin_name, a:word)
7778
endif
79+
" call s:console(reltimestr(reltime(tt)), len(l:completion_result['items']))
7880
return {'matches': l:completion_result['items'], 'incomplete': l:completion_result['incomplete'] }
7981
endfunction
8082

lua/easycomplete.lua

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -338,29 +338,25 @@ function EasyComplete.combine_list(buf_list, dict_list)
338338
local combine_all = EasyComplete.distinct(combined_list)
339339
local ret_list = {}
340340
for _, v in ipairs(combine_all) do
341+
local kind_str = ""
342+
local menu_str = ""
341343
if vim.tbl_contains(buf_list, v) then
342-
table.insert(ret_list, {
343-
word = v,
344-
dup = 1,
345-
icase = 1,
346-
equal = 1,
347-
info = "",
348-
abbr = v,
349-
kind = vim.g.easycomplete_kindflag_buf,
350-
menu = vim.g.easycomplete_menuflag_buf,
351-
})
344+
kind_str = vim.g.easycomplete_kindflag_buf
345+
menu_str = vim.g.easycomplete_menuflag_buf
352346
else
353-
table.insert(ret_list, {
354-
word = v,
355-
dup = 1,
356-
icase = 1,
357-
equal = 1,
358-
info = "",
359-
abbr = v,
360-
kind = vim.g.easycomplete_kindflag_dict,
361-
menu = vim.g.easycomplete_menuflag_dict,
362-
})
347+
kind_str = vim.g.easycomplete_kindflag_dict
348+
menu_str = vim.g.easycomplete_menuflag_dict
363349
end
350+
table.insert(ret_list, {
351+
word = v,
352+
dup = 1,
353+
icase = 1,
354+
equal = 1,
355+
info = "",
356+
abbr = v,
357+
kind = kind_str,
358+
menu = menu_str,
359+
})
364360
end
365361
return ret_list
366362
end

lua/easycomplete/util.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ end
6666
function util.distinct(items)
6767
local unique_values = {}
6868
local result = {}
69-
table.sort(items)
7069
for _, value in ipairs(items) do
7170
if #value == 0 then
7271
-- 空字符串
@@ -82,6 +81,7 @@ function util.distinct(items)
8281
table.insert(result, value)
8382
end
8483
end
84+
table.sort(result)
8585
return result
8686
end
8787

@@ -243,6 +243,7 @@ function util.get_vim_complete_items(response, plugin_name, word)
243243
l_completion_item["label"] = l_vim_complete_item["word"]
244244
end
245245

246+
local l_item_user_data_json_cache = {}
246247
if l_expandable == true then
247248
local l_origin_word = l_vim_complete_item['word']
248249
local l_placeholder_regex = [[\$[0-9]\+\|\${\%(\\.\|[^}]\)\+}]]
@@ -257,14 +258,15 @@ function util.get_vim_complete_items(response, plugin_name, word)
257258
placeholder_position = l_placeholder_position,
258259
cursor_backing_steps = l_cursor_backing_steps
259260
}
260-
l_vim_complete_item['user_data'] = vim.fn.json_encode(l_user_data_json)
261+
-- l_vim_complete_item['user_data'] = vim.fn.json_encode(l_user_data_json)
261262
l_vim_complete_item['user_data_json'] = l_user_data_json
262263
end
263-
local l_user_data_json_l = vim.fn.extend(vim.fn["easycomplete#util#GetUserData"](l_vim_complete_item), {
264+
local l_user_data_json_l = vim.fn.extend(l_user_data_json, {
264265
expandable = 1
265266
})
266-
l_vim_complete_item['user_data'] = vim.fn.json_encode(l_user_data_json_l)
267+
-- l_vim_complete_item['user_data'] = vim.fn.json_encode(l_user_data_json_l)
267268
l_vim_complete_item['user_data_json'] = l_user_data_json_l
269+
l_item_user_data_json_cache = l_user_data_json_l
268270
elseif string.find(l_completion_item['label'], ".+%(.*%)") then
269271
l_vim_complete_item['abbr'] = l_completion_item['label']
270272
if vim.fn['easycomplete#SnipExpandSupport']() then
@@ -292,9 +294,11 @@ function util.get_vim_complete_items(response, plugin_name, word)
292294
else
293295
l_vim_complete_item['user_data_json']["custom_expand"] = 1
294296
end
295-
l_vim_complete_item["user_data"] = vim.fn.json_encode(l_vim_complete_item['user_data_json'])
297+
-- l_vim_complete_item["user_data"] = vim.fn.json_encode(l_vim_complete_item['user_data_json'])
298+
l_item_user_data_json_cache = l_vim_complete_item['user_data_json']
296299
else
297300
l_vim_complete_item['abbr'] = l_completion_item['label']
301+
l_item_user_data_json_cache = {plugin_name = plugin_name}
298302
end
299303
local l_t_info = {}
300304
if l_completion_item["documentation"] ~= nil then
@@ -309,9 +313,9 @@ function util.get_vim_complete_items(response, plugin_name, word)
309313
for _, v in ipairs(l_t_info) do table.insert(l_vim_complete_item['info'], v) end
310314
end
311315

312-
local sha256_str_o = vim.fn['easycomplete#util#Sha256'](l_vim_complete_item['word'] .. tostring(l_vim_complete_item['info']))
316+
local sha256_str_o = vim.fn.sha256(l_vim_complete_item['word'] .. tostring(l_vim_complete_item['info']))
313317
local sha256_str = string.sub(sha256_str_o, 1, 16)
314-
local user_data_json = vim.fn.extend(vim.fn['easycomplete#util#GetUserData'](l_vim_complete_item), {
318+
local user_data_json = vim.fn.extend(l_item_user_data_json_cache, {
315319
plugin_name = plugin_name,
316320
sha256 = sha256_str,
317321
lsp_item = l_completion_item

0 commit comments

Comments
 (0)