Skip to content

Commit

Permalink
server: optimize BCE on the packetio.writePacket (#34758)
Browse files Browse the repository at this point in the history
ref #34669
  • Loading branch information
hawkingrei authored May 18, 2022
1 parent 76cc528 commit d2c4339
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 2 additions & 4 deletions server/packetio.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,11 @@ func (p *packetIO) writePacket(data []byte) error {
writePacketBytes.Add(float64(len(data)))

for length >= mysql.MaxPayloadLen {
data[3] = p.sequence
data[0] = 0xff
data[1] = 0xff
data[2] = 0xff

data[3] = p.sequence

if n, err := p.bufWriter.Write(data[:4+mysql.MaxPayloadLen]); err != nil {
return errors.Trace(mysql.ErrBadConn)
} else if n != (4 + mysql.MaxPayloadLen) {
Expand All @@ -182,11 +181,10 @@ func (p *packetIO) writePacket(data []byte) error {
data = data[mysql.MaxPayloadLen:]
}
}

data[3] = p.sequence
data[0] = byte(length)
data[1] = byte(length >> 8)
data[2] = byte(length >> 16)
data[3] = p.sequence

if n, err := p.bufWriter.Write(data); err != nil {
terror.Log(errors.Trace(err))
Expand Down
9 changes: 9 additions & 0 deletions server/packetio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ import (
"github.com/stretchr/testify/require"
)

func BenchmarkPacketIOWrite(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
var outBuffer bytes.Buffer
pkt := &packetIO{bufWriter: bufio.NewWriter(&outBuffer)}
_ = pkt.writePacket([]byte{0x6d, 0x44, 0x42, 0x3a, 0x35, 0x36, 0x0, 0x0, 0x0, 0xfc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x68, 0x54, 0x49, 0x44, 0x3a, 0x31, 0x30, 0x38, 0x0, 0xfe})
}
}

func TestPacketIOWrite(t *testing.T) {
// Test write one packet
var outBuffer bytes.Buffer
Expand Down

0 comments on commit d2c4339

Please sign in to comment.