Skip to content

Commit 835fc48

Browse files
committed
crypto/tls: reduce the number of allocations made for Conn.Read calls
Conversion of atLeastReader to a field for Conn structure prevents atLeastReader from being allocated on each call of Conn.Read
1 parent a34f420 commit 835fc48

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/crypto/tls/conn.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ type Conn struct {
115115
// in Conn.Write.
116116
activeCall int32
117117

118+
// tmpReader avoids allocations of atLeastReader for Conn.Read
119+
tmpReader atLeastReader
120+
118121
tmp [16]byte
119122
}
120123

@@ -793,6 +796,11 @@ func (r *atLeastReader) Read(p []byte) (int, error) {
793796
return n, err
794797
}
795798

799+
// release an underlying reader to be GCed
800+
func (r *atLeastReader) release() {
801+
r.R = nil
802+
}
803+
796804
// readFromUntil reads from r into c.rawInput until c.rawInput contains
797805
// at least n bytes or else returns an error.
798806
func (c *Conn) readFromUntil(r io.Reader, n int) error {
@@ -804,7 +812,11 @@ func (c *Conn) readFromUntil(r io.Reader, n int) error {
804812
// attempt to fetch it so that it can be used in (*Conn).Read to
805813
// "predict" closeNotify alerts.
806814
c.rawInput.Grow(needs + bytes.MinRead)
807-
_, err := c.rawInput.ReadFrom(&atLeastReader{r, int64(needs)})
815+
816+
c.tmpReader = atLeastReader{r, int64(needs)}
817+
defer c.tmpReader.release()
818+
819+
_, err := c.rawInput.ReadFrom(&c.tmpReader)
808820
return err
809821
}
810822

0 commit comments

Comments
 (0)