Skip to content

Commit 3214f44

Browse files
committed
optimize readPacket (go-sql-driver#1705)
Avoid unnecessary allocation. (cherry picked from commit 0fd55eb)
1 parent eb79f6b commit 3214f44

File tree

6 files changed

+25
-50
lines changed

6 files changed

+25
-50
lines changed

compress_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ func uncompressHelper(t *testing.T, mc *mysqlConn, compressedPacket []byte) []by
4040
conn := new(mockConn)
4141
conn.data = compressedPacket
4242
mc.netConn = conn
43-
mc.readNextFunc = mc.compIO.readNext
44-
mc.readFunc = conn.Read
4543

4644
uncompressedPacket, err := mc.readPacket()
4745
if err != nil {

connection.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ type mysqlConn struct {
4040
compressSequence uint8
4141
parseTime bool
4242
compress bool
43-
readFunc func([]byte) (int, error)
44-
readNextFunc func(int, readerFunc) ([]byte, error)
4543

4644
// for context support (Go 1.8+)
4745
watching bool
@@ -67,6 +65,16 @@ func (mc *mysqlConn) log(v ...any) {
6765
mc.cfg.Logger.Print(v...)
6866
}
6967

68+
func (mc *mysqlConn) readWithTimeout(b []byte) (int, error) {
69+
to := mc.cfg.ReadTimeout
70+
if to > 0 {
71+
if err := mc.netConn.SetReadDeadline(time.Now().Add(to)); err != nil {
72+
return 0, err
73+
}
74+
}
75+
return mc.netConn.Read(b)
76+
}
77+
7078
func (mc *mysqlConn) writeWithTimeout(b []byte) (int, error) {
7179
to := mc.cfg.WriteTimeout
7280
if to > 0 {

connection_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ func TestInterpolateParams(t *testing.T) {
2727
cfg: &Config{
2828
InterpolateParams: true,
2929
},
30-
readNextFunc: buf.readNext,
31-
readFunc: nc.Read,
3230
}
3331

3432
q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42), "gopher"})

connector.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"os"
1717
"strconv"
1818
"strings"
19-
"time"
2019
)
2120

2221
type connector struct {
@@ -131,22 +130,6 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
131130

132131
mc.buf = newBuffer()
133132

134-
// setting readNext/read functions
135-
mc.readNextFunc = mc.buf.readNext
136-
137-
// Initialize read function based on configuration
138-
if mc.cfg.ReadTimeout > 0 {
139-
mc.readFunc = func(b []byte) (int, error) {
140-
deadline := time.Now().Add(mc.cfg.ReadTimeout)
141-
if err := mc.netConn.SetReadDeadline(deadline); err != nil {
142-
return 0, err
143-
}
144-
return mc.netConn.Read(b)
145-
}
146-
} else {
147-
mc.readFunc = mc.netConn.Read
148-
}
149-
150133
// Reading Handshake Initialization Packet
151134
authData, serverCapabilities, serverExtendedCapabilities, plugin, err := mc.readHandshakePacket()
152135
if err != nil {
@@ -187,7 +170,6 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
187170
if mc.cfg.compress && mc.clientCapabilities&clientCompress > 0 {
188171
mc.compress = true
189172
mc.compIO = newCompIO(mc)
190-
mc.readNextFunc = mc.compIO.readNext
191173
}
192174
if mc.cfg.MaxAllowedPacket > 0 {
193175
mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket

packets.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, serverCapabil
401401
return err
402402
}
403403
mc.netConn = tlsConn
404-
mc.readFunc = mc.netConn.Read
405404
}
406405

407406
// User [null terminated string]

packets_test.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ func newRWMockConn(sequence uint8) (*mockConn, *mysqlConn) {
106106
closech: make(chan struct{}),
107107
maxAllowedPacket: defaultMaxAllowedPacket,
108108
sequence: sequence,
109-
readNextFunc: buf.readNext,
110-
readFunc: conn.Read,
111109
}
112110
return conn, mc
113111
}
@@ -116,11 +114,9 @@ func TestReadPacketSingleByte(t *testing.T) {
116114
conn := new(mockConn)
117115
buf := newBuffer()
118116
mc := &mysqlConn{
119-
netConn: conn,
120-
buf: buf,
121-
cfg: NewConfig(),
122-
readNextFunc: buf.readNext,
123-
readFunc: conn.Read,
117+
netConn: conn,
118+
buf: buf,
119+
cfg: NewConfig(),
124120
}
125121

126122
conn.data = []byte{0x01, 0x00, 0x00, 0x00, 0xff}
@@ -173,11 +169,9 @@ func TestReadPacketSplit(t *testing.T) {
173169
conn := new(mockConn)
174170
buf := newBuffer()
175171
mc := &mysqlConn{
176-
netConn: conn,
177-
buf: buf,
178-
cfg: NewConfig(),
179-
readNextFunc: buf.readNext,
180-
readFunc: conn.Read,
172+
netConn: conn,
173+
buf: buf,
174+
cfg: NewConfig(),
181175
}
182176

183177
data := make([]byte, maxPacketSize*2+4*3)
@@ -283,12 +277,10 @@ func TestReadPacketFail(t *testing.T) {
283277
conn := new(mockConn)
284278
buf := newBuffer()
285279
mc := &mysqlConn{
286-
netConn: conn,
287-
buf: buf,
288-
closech: make(chan struct{}),
289-
cfg: NewConfig(),
290-
readNextFunc: buf.readNext,
291-
readFunc: conn.Read,
280+
netConn: conn,
281+
buf: buf,
282+
closech: make(chan struct{}),
283+
cfg: NewConfig(),
292284
}
293285

294286
// illegal empty (stand-alone) packet
@@ -331,13 +323,11 @@ func TestRegression801(t *testing.T) {
331323
conn := new(mockConn)
332324
buf := newBuffer()
333325
mc := &mysqlConn{
334-
netConn: conn,
335-
buf: buf,
336-
cfg: new(Config),
337-
sequence: 42,
338-
closech: make(chan struct{}),
339-
readNextFunc: buf.readNext,
340-
readFunc: conn.Read,
326+
netConn: conn,
327+
buf: buf,
328+
cfg: new(Config),
329+
sequence: 42,
330+
closech: make(chan struct{}),
341331
}
342332

343333
conn.data = []byte{72, 0, 0, 42, 10, 53, 46, 53, 46, 56, 0, 165, 0, 0, 0,

0 commit comments

Comments
 (0)