diff options
author | David Fifield <david@bamsoftware.com> | 2022-09-22 15:49:14 -0600 |
---|---|---|
committer | David Fifield <david@bamsoftware.com> | 2022-11-16 13:48:34 -0700 |
commit | 264425a488edbbfad80deb59aefcb3c40ad9a72d (patch) | |
tree | 0aa0909e0c630acea394e35687909c8351075411 | |
parent | 3df514ae2961166088902dbd329b929983b6e002 (diff) | |
download | snowflake-264425a488edbbfad80deb59aefcb3c40ad9a72d.tar.gz snowflake-264425a488edbbfad80deb59aefcb3c40ad9a72d.zip |
Use io.CopyBuffer in websocketconn.readLoop.
This avoids io.Copy allocating a 32 KB buffer on every call.
https://cs.opensource.google/go/go/+/refs/tags/go1.19.1:src/io/io.go;l=416
$ go test -bench=BenchmarkReadWrite -benchmem -benchtime=5s
BenchmarkReadWrite/c←s_150-4 385740 15114 ns/op 9.92 MB/s 4104 B/op 3 allocs/op
BenchmarkReadWrite/s←c_150-4 347070 16824 ns/op 8.92 MB/s 4152 B/op 4 allocs/op
BenchmarkReadWrite/c←s_3000-4 190257 31581 ns/op 94.99 MB/s 8208 B/op 6 allocs/op
BenchmarkReadWrite/s←c_3000-4 163233 34821 ns/op 86.16 MB/s 8304 B/op 8 allocs/op
-rw-r--r-- | common/websocketconn/websocketconn.go | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/common/websocketconn/websocketconn.go b/common/websocketconn/websocketconn.go index 72ac463..3fdcb79 100644 --- a/common/websocketconn/websocketconn.go +++ b/common/websocketconn/websocketconn.go @@ -50,7 +50,8 @@ func readLoop(w io.Writer, ws *websocket.Conn) error { if messageType != websocket.BinaryMessage && messageType != websocket.TextMessage { continue } - _, err = io.Copy(w, r) + var buf [2048]byte + _, err = io.CopyBuffer(w, r, buf[:]) if err != nil { return err } |