Skip to content

Commit

Permalink
tune slice append performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hjweddie committed Feb 25, 2024
1 parent 39976f7 commit f715f17
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
12 changes: 6 additions & 6 deletions canal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ type node struct {
func parseStmt(stmt ast.StmtNode) (ns []*node) {
switch t := stmt.(type) {
case *ast.RenameTableStmt:
for _, tableInfo := range t.TableToTables {
n := &node{
ns = make([]*node, len(t.TableToTables))
for i, tableInfo := range t.TableToTables {
ns[i] = &node{
db: tableInfo.OldTable.Schema.String(),
table: tableInfo.OldTable.Name.String(),
}
ns = append(ns, n)
}
case *ast.AlterTableStmt:
n := &node{
Expand All @@ -197,12 +197,12 @@ func parseStmt(stmt ast.StmtNode) (ns []*node) {
}
ns = []*node{n}
case *ast.DropTableStmt:
for _, table := range t.Tables {
n := &node{
ns = make([]*node, len(t.Tables))
for i, table := range t.Tables {
ns[i] = &node{
db: table.Schema.String(),
table: table.Name.String(),
}
ns = append(ns, n)
}
case *ast.CreateTableStmt:
n := &node{
Expand Down
4 changes: 2 additions & 2 deletions replication/binlogstreamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func (s *BinlogStreamer) GetEventWithStartTime(ctx context.Context, startTime ti
// DumpEvents dumps all left events
func (s *BinlogStreamer) DumpEvents() []*BinlogEvent {
count := len(s.ch)
events := make([]*BinlogEvent, 0, count)
events := make([]*BinlogEvent, count)
for i := 0; i < count; i++ {
events = append(events, <-s.ch)
events[i] = <-s.ch
}
return events
}
Expand Down
8 changes: 4 additions & 4 deletions replication/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,17 @@ type PreviousGTIDsEvent struct {
}

func (e *PreviousGTIDsEvent) Decode(data []byte) error {
var previousGTIDSets []string
pos := 0
uuidCount := binary.LittleEndian.Uint16(data[pos : pos+8])
pos += 8

previousGTIDSets := make([]string, uuidCount)
for i := uint16(0); i < uuidCount; i++ {
uuid := e.decodeUuid(data[pos : pos+16])
pos += 16
sliceCount := binary.LittleEndian.Uint16(data[pos : pos+8])
pos += 8
var intervals []string
intervals := make([]string, sliceCount)
for i := uint16(0); i < sliceCount; i++ {
start := e.decodeInterval(data[pos : pos+8])
pos += 8
Expand All @@ -247,9 +247,9 @@ func (e *PreviousGTIDsEvent) Decode(data []byte) error {
} else {
interval = fmt.Sprintf("%d-%d", start, stop-1)
}
intervals = append(intervals, interval)
intervals[i] = interval
}
previousGTIDSets = append(previousGTIDSets, fmt.Sprintf("%s:%s", uuid, strings.Join(intervals, ":")))
previousGTIDSets[i] = fmt.Sprintf("%s:%s", uuid, strings.Join(intervals, ":"))
}
e.GTIDSets = strings.Join(previousGTIDSets, ",")
return nil
Expand Down
3 changes: 1 addition & 2 deletions replication/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ func (p *BinlogParser) parseSingleEvent(r io.Reader, onEvent OnEventFunc) (bool,
return false, errors.Errorf("invalid raw data size in event %s, need %d but got %d", h.EventType, h.EventSize, buf.Len())
}

var rawData []byte
rawData = append(rawData, buf.Bytes()...)
rawData := buf.Bytes()
bodyLen := int(h.EventSize) - EventHeaderSize
body := rawData[EventHeaderSize:]
if len(body) != bodyLen {
Expand Down
24 changes: 9 additions & 15 deletions replication/row_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,9 @@ func (e *TableMapEvent) SetStrValueString() [][]string {
if len(e.SetStrValue) == 0 {
return nil
}
e.setStrValueString = make([][]string, 0, len(e.SetStrValue))
for _, vals := range e.SetStrValue {
e.setStrValueString = append(
e.setStrValueString,
e.bytesSlice2StrSlice(vals),
)
e.setStrValueString = make([][]string, len(e.SetStrValue))
for i, vals := range e.SetStrValue {
e.setStrValueString[i] = e.bytesSlice2StrSlice(vals)
}
}
return e.setStrValueString
Expand All @@ -588,12 +585,9 @@ func (e *TableMapEvent) EnumStrValueString() [][]string {
if len(e.EnumStrValue) == 0 {
return nil
}
e.enumStrValueString = make([][]string, 0, len(e.EnumStrValue))
for _, vals := range e.EnumStrValue {
e.enumStrValueString = append(
e.enumStrValueString,
e.bytesSlice2StrSlice(vals),
)
e.enumStrValueString = make([][]string, len(e.EnumStrValue))
for i, vals := range e.EnumStrValue {
e.enumStrValueString[i] = e.bytesSlice2StrSlice(vals)
}
}
return e.enumStrValueString
Expand All @@ -612,9 +606,9 @@ func (e *TableMapEvent) bytesSlice2StrSlice(src [][]byte) []string {
if src == nil {
return nil
}
ret := make([]string, 0, len(src))
for _, item := range src {
ret = append(ret, string(item))
ret := make([]string, len(src))
for i, item := range src {
ret[i] = string(item)
}
return ret
}
Expand Down

0 comments on commit f715f17

Please sign in to comment.