Skip to content

Commit 1aa9b24

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

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
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

+9-4
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"
@@ -754,7 +755,7 @@ func (e *TableMapEvent) VisibilityMap() map[int]bool {
754755
if len(e.VisibilityBitmap) == 0 {
755756
return nil
756757
}
757-
ret := make(map[int]bool)
758+
ret := make(map[int]bool, len(e.VisibilityBitmap)*8)
758759
i := 0
759760
for _, field := range e.VisibilityBitmap {
760761
for c := 0x80; c != 0; c >>= 1 {
@@ -1146,15 +1147,19 @@ 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) {
1153+
col := 0
1154+
for ; col+8 < int(e.ColumnCount); col += 8 {
1155+
count += bits.OnesCount8(bitmap[col>>3])
1156+
}
1157+
for ; col < int(e.ColumnCount); col++ {
1158+
if isBitSet(bitmap, col) {
11551159
count++
11561160
}
11571161
}
1162+
skips := make([]int, 0, int(e.ColumnCount)-count)
11581163
count = bitmapByteSize(count)
11591164

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

0 commit comments

Comments
 (0)