Skip to content

Commit db7269f

Browse files
committed
Buffer IO
Reduce latency introduced by ***lsp4clj*** by using buffered input and output streams. In theory, it's faster to read data off the underlying stream in chunks, even though sometimes we need to process them one byte at a time. In practice the difference is probably minimal, though it doesn't hurt. This uses `clojure.java.io/input-stream` and `output-stream` to create the buffered streams, which has the side-benefit of cleaning up the `socket-server` code slightly.
1 parent 8af1d52 commit db7269f

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Buffer I/O, slightly reducing latency.
6+
57
## v1.5.0
68

79
- Let language servers abort running requests that a client has cancelled.
@@ -66,7 +68,7 @@ see https://github.com/clojure-lsp/clojure-lsp/pull/1117
6668

6769
## v0.2.0
6870

69-
- Support LSP 3.16 file operations: `workspace/willRenameFiles`, `workspace/didRenameFiles`,
71+
- Support LSP 3.16 file operations: `workspace/willRenameFiles`, `workspace/didRenameFiles`,
7072
`workspace/willCreateFiles`, `workspace/didCreateFiles`, `workspace/willDeleteFiles`, `workspace/didDeleteFiles`.
7173

7274
## v0.0.1

src/lsp4clj/io_chan.clj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[camel-snake-kebab.extras :as cske]
55
[cheshire.core :as json]
66
[clojure.core.async :as async]
7+
[clojure.java.io :as io]
78
[clojure.string :as string])
89
(:import
910
(java.io EOFException InputStream OutputStream)))
@@ -88,9 +89,10 @@
8889
8990
Reads in a thread to avoid blocking a go block thread."
9091
([input] (input-stream->input-chan input {}))
91-
([^InputStream input {:keys [close? keyword-function]
92-
:or {close? true, keyword-function csk/->kebab-case-keyword}}]
93-
(let [messages (async/chan 1)]
92+
([input {:keys [close? keyword-function]
93+
:or {close? true, keyword-function csk/->kebab-case-keyword}}]
94+
(let [input (io/input-stream input)
95+
messages (async/chan 1)]
9496
(async/thread
9597
(loop [headers {}]
9698
(let [line (read-header-line input)]
@@ -112,8 +114,9 @@
112114
channel is closed, closes the output.
113115
114116
Writes in a thread to avoid blocking a go block thread."
115-
[^OutputStream output]
116-
(let [messages (async/chan 1)]
117+
[output]
118+
(let [output (io/output-stream output)
119+
messages (async/chan 1)]
117120
(async/thread
118121
(with-open [writer output] ;; close output when channel closes
119122
(loop []

src/lsp4clj/socket_server.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
(.close conn)
4343
(.close socket))]
4444
(io-server/server (assoc opts
45-
:in (.getInputStream conn)
46-
:out (.getOutputStream conn)
45+
:in conn
46+
:out conn
4747
:on-close on-close)))))

test/lsp4clj/io_chan_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
(string/join "\r\n" arr))
1313

1414
(defn mock-input-stream [^String input]
15-
(java.io.ByteArrayInputStream. (.getBytes input "utf-8")))
15+
(.getBytes input "utf-8"))
1616

1717
(deftest output-stream-should-camel-case-output
1818
(let [output-stream (java.io.ByteArrayOutputStream.)

test/lsp4clj/socket_server_test.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
server* (future (socket-server/server {} socket-data))]
3030
(try
3131
(with-open [client (Socket. (.getInetAddress socket) (.getLocalPort socket))]
32-
(let [client-input-ch (io-chan/input-stream->input-chan (.getInputStream client))
33-
client-output-ch (io-chan/output-stream->output-chan (.getOutputStream client))
32+
(let [client-input-ch (io-chan/input-stream->input-chan client)
33+
client-output-ch (io-chan/output-stream->output-chan client)
3434
server @server*
3535
join (server/start server nil)]
3636
(try

0 commit comments

Comments
 (0)