Skip to content
Snippets Groups Projects
Commit 42ea5ff6 authored by David Fifield's avatar David Fifield
Browse files

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
parent 2bf2f0cb
Branches
Tags
No related merge requests found
......@@ -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
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment