mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 03:18:16 +00:00
perf(lsp): use string.buffer for rpc loop
Avoids some table allocations. In a quick test over 50000 iterations it reduces the time from 130ms to 74 ms For the test setup details see: https://github.com/mfussenegger/nvim-dap/pull/1394#issue-2725352391
This commit is contained in:

committed by
Lewis Russell

parent
42657e70b8
commit
f517fcd148
@@ -25,8 +25,8 @@ local function get_content_length(header)
|
||||
if line == '' then
|
||||
break
|
||||
end
|
||||
local key, value = line:match('^%s*(%S+)%s*:%s*(.+)%s*$')
|
||||
if key:lower() == 'content-length' then
|
||||
local key, value = line:match('^%s*(%S+)%s*:%s*(%d+)%s*$')
|
||||
if key and key:lower() == 'content-length' then
|
||||
return tonumber(value)
|
||||
end
|
||||
end
|
||||
@@ -39,8 +39,37 @@ local header_start_pattern = ('content'):gsub('%w', function(c)
|
||||
return '[' .. c .. c:upper() .. ']'
|
||||
end)
|
||||
|
||||
local has_strbuffer, strbuffer = pcall(require, 'string.buffer')
|
||||
|
||||
--- The actual workhorse.
|
||||
local function request_parser_loop()
|
||||
---@type function
|
||||
local request_parser_loop
|
||||
|
||||
if has_strbuffer then
|
||||
request_parser_loop = function()
|
||||
local buf = strbuffer.new()
|
||||
while true do
|
||||
local msg = buf:tostring()
|
||||
local header_end = msg:find('\r\n\r\n', 1, true)
|
||||
if header_end then
|
||||
local header = buf:get(header_end + 1)
|
||||
buf:skip(2) -- skip past header boundary
|
||||
local content_length = get_content_length(header)
|
||||
while #buf < content_length do
|
||||
local chunk = coroutine.yield()
|
||||
buf:put(chunk)
|
||||
end
|
||||
local body = buf:get(content_length)
|
||||
local chunk = coroutine.yield(body)
|
||||
buf:put(chunk)
|
||||
else
|
||||
local chunk = coroutine.yield()
|
||||
buf:put(chunk)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
request_parser_loop = function()
|
||||
local buffer = '' -- only for header part
|
||||
while true do
|
||||
-- A message can only be complete if it has a double CRLF and also the full
|
||||
@@ -101,6 +130,7 @@ local function request_parser_loop()
|
||||
buffer = buffer .. data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local M = {}
|
||||
|
Reference in New Issue
Block a user