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
|
if line == '' then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
local key, value = line:match('^%s*(%S+)%s*:%s*(.+)%s*$')
|
local key, value = line:match('^%s*(%S+)%s*:%s*(%d+)%s*$')
|
||||||
if key:lower() == 'content-length' then
|
if key and key:lower() == 'content-length' then
|
||||||
return tonumber(value)
|
return tonumber(value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -39,66 +39,96 @@ local header_start_pattern = ('content'):gsub('%w', function(c)
|
|||||||
return '[' .. c .. c:upper() .. ']'
|
return '[' .. c .. c:upper() .. ']'
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local has_strbuffer, strbuffer = pcall(require, 'string.buffer')
|
||||||
|
|
||||||
--- The actual workhorse.
|
--- The actual workhorse.
|
||||||
local function request_parser_loop()
|
---@type function
|
||||||
local buffer = '' -- only for header part
|
local request_parser_loop
|
||||||
while true do
|
|
||||||
-- A message can only be complete if it has a double CRLF and also the full
|
if has_strbuffer then
|
||||||
-- payload, so first let's check for the CRLFs
|
request_parser_loop = function()
|
||||||
local header_end, body_start = buffer:find('\r\n\r\n', 1, true)
|
local buf = strbuffer.new()
|
||||||
-- Start parsing the headers
|
while true do
|
||||||
if header_end then
|
local msg = buf:tostring()
|
||||||
-- This is a workaround for servers sending initial garbage before
|
local header_end = msg:find('\r\n\r\n', 1, true)
|
||||||
-- sending headers, such as if a bash script sends stdout. It assumes
|
if header_end then
|
||||||
-- that we know all of the headers ahead of time. At this moment, the
|
local header = buf:get(header_end + 1)
|
||||||
-- only valid headers start with "Content-*", so that's the thing we will
|
buf:skip(2) -- skip past header boundary
|
||||||
-- be searching for.
|
local content_length = get_content_length(header)
|
||||||
-- TODO(ashkan) I'd like to remove this, but it seems permanent :(
|
while #buf < content_length do
|
||||||
local buffer_start = buffer:find(header_start_pattern)
|
local chunk = coroutine.yield()
|
||||||
if not buffer_start then
|
buf:put(chunk)
|
||||||
error(
|
end
|
||||||
string.format(
|
local body = buf:get(content_length)
|
||||||
"Headers were expected, a different response was received. The server response was '%s'.",
|
local chunk = coroutine.yield(body)
|
||||||
buffer
|
buf:put(chunk)
|
||||||
)
|
else
|
||||||
)
|
|
||||||
end
|
|
||||||
local header = buffer:sub(buffer_start, header_end + 1)
|
|
||||||
local content_length = get_content_length(header)
|
|
||||||
-- Use table instead of just string to buffer the message. It prevents
|
|
||||||
-- a ton of strings allocating.
|
|
||||||
-- ref. http://www.lua.org/pil/11.6.html
|
|
||||||
---@type string[]
|
|
||||||
local body_chunks = { buffer:sub(body_start + 1) }
|
|
||||||
local body_length = #body_chunks[1]
|
|
||||||
-- Keep waiting for data until we have enough.
|
|
||||||
while body_length < content_length do
|
|
||||||
---@type string
|
|
||||||
local chunk = coroutine.yield()
|
local chunk = coroutine.yield()
|
||||||
or error('Expected more data for the body. The server may have died.') -- TODO hmm.
|
buf:put(chunk)
|
||||||
table.insert(body_chunks, chunk)
|
|
||||||
body_length = body_length + #chunk
|
|
||||||
end
|
end
|
||||||
local last_chunk = body_chunks[#body_chunks]
|
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
|
||||||
|
-- payload, so first let's check for the CRLFs
|
||||||
|
local header_end, body_start = buffer:find('\r\n\r\n', 1, true)
|
||||||
|
-- Start parsing the headers
|
||||||
|
if header_end then
|
||||||
|
-- This is a workaround for servers sending initial garbage before
|
||||||
|
-- sending headers, such as if a bash script sends stdout. It assumes
|
||||||
|
-- that we know all of the headers ahead of time. At this moment, the
|
||||||
|
-- only valid headers start with "Content-*", so that's the thing we will
|
||||||
|
-- be searching for.
|
||||||
|
-- TODO(ashkan) I'd like to remove this, but it seems permanent :(
|
||||||
|
local buffer_start = buffer:find(header_start_pattern)
|
||||||
|
if not buffer_start then
|
||||||
|
error(
|
||||||
|
string.format(
|
||||||
|
"Headers were expected, a different response was received. The server response was '%s'.",
|
||||||
|
buffer
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
local header = buffer:sub(buffer_start, header_end + 1)
|
||||||
|
local content_length = get_content_length(header)
|
||||||
|
-- Use table instead of just string to buffer the message. It prevents
|
||||||
|
-- a ton of strings allocating.
|
||||||
|
-- ref. http://www.lua.org/pil/11.6.html
|
||||||
|
---@type string[]
|
||||||
|
local body_chunks = { buffer:sub(body_start + 1) }
|
||||||
|
local body_length = #body_chunks[1]
|
||||||
|
-- Keep waiting for data until we have enough.
|
||||||
|
while body_length < content_length do
|
||||||
|
---@type string
|
||||||
|
local chunk = coroutine.yield()
|
||||||
|
or error('Expected more data for the body. The server may have died.') -- TODO hmm.
|
||||||
|
table.insert(body_chunks, chunk)
|
||||||
|
body_length = body_length + #chunk
|
||||||
|
end
|
||||||
|
local last_chunk = body_chunks[#body_chunks]
|
||||||
|
|
||||||
body_chunks[#body_chunks] = last_chunk:sub(1, content_length - body_length - 1)
|
body_chunks[#body_chunks] = last_chunk:sub(1, content_length - body_length - 1)
|
||||||
local rest = ''
|
local rest = ''
|
||||||
if body_length > content_length then
|
if body_length > content_length then
|
||||||
rest = last_chunk:sub(content_length - body_length)
|
rest = last_chunk:sub(content_length - body_length)
|
||||||
|
end
|
||||||
|
local body = table.concat(body_chunks)
|
||||||
|
-- Yield our data.
|
||||||
|
|
||||||
|
--- @type string
|
||||||
|
local data = coroutine.yield(body)
|
||||||
|
or error('Expected more data for the body. The server may have died.')
|
||||||
|
buffer = rest .. data
|
||||||
|
else
|
||||||
|
-- Get more data since we don't have enough.
|
||||||
|
--- @type string
|
||||||
|
local data = coroutine.yield()
|
||||||
|
or error('Expected more data for the header. The server may have died.')
|
||||||
|
buffer = buffer .. data
|
||||||
end
|
end
|
||||||
local body = table.concat(body_chunks)
|
|
||||||
-- Yield our data.
|
|
||||||
|
|
||||||
--- @type string
|
|
||||||
local data = coroutine.yield(body)
|
|
||||||
or error('Expected more data for the body. The server may have died.')
|
|
||||||
buffer = rest .. data
|
|
||||||
else
|
|
||||||
-- Get more data since we don't have enough.
|
|
||||||
--- @type string
|
|
||||||
local data = coroutine.yield()
|
|
||||||
or error('Expected more data for the header. The server may have died.')
|
|
||||||
buffer = buffer .. data
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user