Skip to content

Commit e580c8b

Browse files
committed
Refactor lsp--create-filter-function for better performance
Signed-off-by: Eval EXEC <execvy@gmail.com>
1 parent dc75f2a commit e580c8b

File tree

1 file changed

+31
-62
lines changed

1 file changed

+31
-62
lines changed

lsp-mode.el

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7151,69 +7151,38 @@ server. WORKSPACE is the active workspace."
71517151
('request (lsp--on-request workspace json-data)))))))
71527152

71537153
(defun lsp--create-filter-function (workspace)
7154-
"Make filter for the workspace."
7155-
(let ((body-received 0)
7156-
leftovers body-length body chunk)
7154+
"Create a process filter function for WORKSPACE that decodes and processes LSP JSON messages.
7155+
The function accumulates incoming data in a buffer, parses headers to extract
7156+
Content-Length, and then reads and decodes complete JSON messages."
7157+
(let ((input-buffer (generate-new-buffer " *lsp-input*")))
71577158
(lambda (_proc input)
7158-
(setf chunk (if (s-blank? leftovers)
7159-
(encode-coding-string input 'utf-8-unix t)
7160-
(concat leftovers (encode-coding-string input 'utf-8-unix t))))
7161-
7162-
(let (messages)
7163-
(while (not (s-blank? chunk))
7164-
(if (not body-length)
7165-
;; Read headers
7166-
(if-let* ((body-sep-pos (string-match-p "\r\n\r\n" chunk)))
7167-
;; We've got all the headers, handle them all at once:
7168-
(setf body-length (lsp--get-body-length
7169-
(mapcar #'lsp--parse-header
7170-
(split-string
7171-
(substring-no-properties chunk
7172-
(or (string-match-p "Content-Length" chunk)
7173-
(error "Unable to find Content-Length header."))
7174-
body-sep-pos)
7175-
"\r\n")))
7176-
body-received 0
7177-
leftovers nil
7178-
chunk (substring-no-properties chunk (+ body-sep-pos 4)))
7179-
7180-
;; Haven't found the end of the headers yet. Save everything
7181-
;; for when the next chunk arrives and await further input.
7182-
(setf leftovers chunk
7183-
chunk nil))
7184-
(let* ((chunk-length (string-bytes chunk))
7185-
(left-to-receive (- body-length body-received))
7186-
(this-body (if (< left-to-receive chunk-length)
7187-
(prog1 (substring-no-properties chunk 0 left-to-receive)
7188-
(setf chunk (substring-no-properties chunk left-to-receive)))
7189-
(prog1 chunk
7190-
(setf chunk nil))))
7191-
(body-bytes (string-bytes this-body)))
7192-
(push this-body body)
7193-
(setf body-received (+ body-received body-bytes))
7194-
(when (>= chunk-length left-to-receive)
7195-
(condition-case err
7196-
(with-temp-buffer
7197-
(apply #'insert
7198-
(nreverse
7199-
(prog1 body
7200-
(setf leftovers nil
7201-
body-length nil
7202-
body-received nil
7203-
body nil))))
7204-
(decode-coding-region (point-min)
7205-
(point-max)
7206-
'utf-8)
7207-
(goto-char (point-min))
7208-
(push (lsp-json-read-buffer) messages))
7209-
7210-
(error
7211-
(lsp-warn "Failed to parse the following chunk:\n'''\n%s\n'''\nwith message %s"
7212-
(concat leftovers input)
7213-
err)))))))
7214-
(mapc (lambda (msg)
7215-
(lsp--parser-on-message msg workspace))
7216-
(nreverse messages))))))
7159+
(with-current-buffer input-buffer
7160+
;; Insert raw input at the end as UTF-8 (no decode yet)
7161+
(goto-char (point-max))
7162+
(insert (encode-coding-string input 'utf-8-unix t))
7163+
(goto-char (point-min))
7164+
(cl-loop
7165+
;; Try to parse messages as long as possible
7166+
while (let ((header-end (search-forward "\r\n\r\n" nil t))
7167+
(header-start (search-backward "Content-Length:" nil t)))
7168+
(when header-end
7169+
(let* ((headers (buffer-substring-no-properties header-start (- header-end 4)))
7170+
(header-lines (split-string headers "\r\n" t))
7171+
(parsed-headers (mapcar #'lsp--parse-header header-lines))
7172+
(body-length (lsp--get-body-length parsed-headers))
7173+
(body-start header-end)
7174+
(body-end (+ header-end body-length)))
7175+
(when (<= body-end (point-max))
7176+
(decode-coding-region body-start body-end 'utf-8)
7177+
(goto-char body-start)
7178+
(condition-case err
7179+
(let ((msg (lsp-json-read-buffer)))
7180+
(lsp--parser-on-message msg workspace))
7181+
(error
7182+
(message "LSP JSON parse error: %S" err)))
7183+
(delete-region (point-min) body-end)
7184+
(goto-char (point-min))
7185+
t)))))))))
72177186

72187187
(defvar-local lsp--line-col-to-point-hash-table nil
72197188
"Hash table with keys (line . col) and values that are either point positions

0 commit comments

Comments
 (0)