Skip to content

Commit

Permalink
Only clone []byte when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
rsafonseca committed Jan 29, 2025
1 parent 1a643e7 commit 66d84c6
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ var wsGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
var compressFinalBlock = []byte{0x00, 0x00, 0xff, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff}

type websocketReader struct {
r io.Reader
pending [][]byte
ib []byte
ff bool
fc bool
nl bool
dc *wsDecompressor
nc *Conn
r io.Reader
pending [][]byte
compress bool
ib []byte
ff bool
fc bool
nl bool
dc *wsDecompressor
nc *Conn
}

type wsDecompressor struct {
Expand Down Expand Up @@ -312,11 +313,13 @@ func (r *websocketReader) Read(p []byte) (int, error) {
}
r.fc = false
}
} else if r.compress {
b = bytes.Clone(b)
}
// Add to the pending list if dealing with uncompressed frames or
// after we have received the full compressed message and decompressed it.
if addToPending {
r.pending = append(r.pending, bytes.Clone(b))
r.pending = append(r.pending, b)
}
}
// In case of compression, there may be nothing to drain
Expand Down Expand Up @@ -647,6 +650,7 @@ func (nc *Conn) wsInitHandshake(u *url.URL) error {

wsr := wsNewReader(nc.br.r)
wsr.nc = nc
wsr.compress = compress
// We have to slurp whatever is in the bufio reader and copy to br.r
if n := br.Buffered(); n != 0 {
wsr.ib, _ = br.Peek(n)
Expand Down

0 comments on commit 66d84c6

Please sign in to comment.