Skip to content

Commit 0d91171

Browse files
committed
Use math/bits OnesCount to preallocate slices
1 parent cf91430 commit 0d91171

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

client/conn.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"crypto/tls"
77
"fmt"
8+
"math/bits"
89
"net"
910
"runtime"
1011
"runtime/debug"
@@ -557,8 +558,8 @@ func (c *Conn) execSend(query string) error {
557558
// separated by "|". Examples of capability names are CLIENT_DEPRECATE_EOF and CLIENT_PROTOCOL_41.
558559
// These are defined as constants in the mysql package.
559560
func (c *Conn) CapabilityString() string {
560-
var caps []string
561561
capability := c.capability
562+
caps := make([]string, 0, bits.OnesCount32(capability))
562563
for i := 0; capability != 0; i++ {
563564
field := uint32(1 << i)
564565
if capability&field == 0 {
@@ -642,8 +643,8 @@ func (c *Conn) CapabilityString() string {
642643
// StatusString returns a "|" separated list of status fields. Example status values are SERVER_QUERY_WAS_SLOW and SERVER_STATUS_AUTOCOMMIT.
643644
// These are defined as constants in the mysql package.
644645
func (c *Conn) StatusString() string {
645-
var stats []string
646646
status := c.status
647+
stats := make([]string, 0, bits.OnesCount16(status))
647648
for i := 0; status != 0; i++ {
648649
field := uint16(1 << i)
649650
if status&field == 0 {

replication/row_event.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"fmt"
77
"io"
8+
"math/bits"
89
"strconv"
910
"strings"
1011
"time"
@@ -1146,15 +1147,13 @@ func (e *RowsEvent) decodeImage(data []byte, bitmap []byte, rowImageType EnumRow
11461147
}
11471148

11481149
row := make([]interface{}, e.ColumnCount)
1149-
skips := make([]int, 0)
11501150

11511151
// refer: https://github.com/alibaba/canal/blob/c3e38e50e269adafdd38a48c63a1740cde304c67/dbsync/src/main/java/com/taobao/tddl/dbsync/binlog/event/RowsLogBuffer.java#L63
11521152
count := 0
1153-
for i := 0; i < int(e.ColumnCount); i++ {
1154-
if isBitSet(bitmap, i) {
1155-
count++
1156-
}
1153+
for _, b := range bitmap {
1154+
count += bits.OnesCount8(b)
11571155
}
1156+
skips := make([]int, 0, e.ColumnCount-uint64(count))
11581157
count = bitmapByteSize(count)
11591158

11601159
nullBitmap := data[pos : pos+count]

0 commit comments

Comments
 (0)