Skip to content

tune slice append performance #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions canal/sync.go
Original file line number Diff line number Diff line change
@@ -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{
@@ -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{
6 changes: 3 additions & 3 deletions replication/binlogstreamer.go
Original file line number Diff line number Diff line change
@@ -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)
for i := 0; i < count; i++ {
events = append(events, <-s.ch)
events := make([]*BinlogEvent, count)
for i := range events {
events[i] = <-s.ch
}
return events
}
12 changes: 6 additions & 6 deletions replication/event.go
Original file line number Diff line number Diff line change
@@ -225,18 +225,18 @@ 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

for i := uint16(0); i < uuidCount; i++ {
previousGTIDSets := make([]string, uuidCount)
for i := range previousGTIDSets {
uuid := e.decodeUuid(data[pos : pos+16])
pos += 16
sliceCount := binary.LittleEndian.Uint16(data[pos : pos+8])
pos += 8
var intervals []string
for i := uint16(0); i < sliceCount; i++ {
intervals := make([]string, sliceCount)
for i := range intervals {
start := e.decodeInterval(data[pos : pos+8])
pos += 8
stop := e.decodeInterval(data[pos : pos+8])
@@ -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
24 changes: 9 additions & 15 deletions replication/row_event.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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
}