Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
restore: reuse bytes.Buffer (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng authored and kennytm committed Dec 21, 2018
1 parent 9a9c299 commit c807b62
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@ func (cr *chunkRestore) restore(
}
}()

var buffer bytes.Buffer
for {
select {
case <-ctx.Done():
Expand All @@ -1345,52 +1346,52 @@ func (cr *chunkRestore) restore(
break
}

buffer.Reset()
start := time.Now()

var sqls strings.Builder
sqls.WriteString("INSERT INTO ")
sqls.WriteString(t.tableName)
sqls.Write(cr.chunk.Columns)
sqls.WriteString(" VALUES")
buffer.WriteString("INSERT INTO ")
buffer.WriteString(t.tableName)
buffer.Write(cr.chunk.Columns)
buffer.WriteString(" VALUES")
var sep byte = ' '
readLoop:
for cr.parser.Pos() < endOffset {
err := cr.parser.ReadRow()
switch errors.Cause(err) {
case nil:
sqls.WriteByte(sep)
buffer.WriteByte(sep)
sep = ','
lastRow := cr.parser.LastRow()
if cr.chunk.ShouldIncludeRowID {
sqls.Write(lastRow.Row[:len(lastRow.Row)-1])
fmt.Fprintf(&sqls, ",%d)", lastRow.RowID)
buffer.Write(lastRow.Row[:len(lastRow.Row)-1])
fmt.Fprintf(&buffer, ",%d)", lastRow.RowID)
} else {
sqls.Write(lastRow.Row)
buffer.Write(lastRow.Row)
}
case io.EOF:
break readLoop
default:
return errors.Trace(err)
}
}
if sep != ',' { // quick and dirty way to check if `sqls` actually contained any values
if sep != ',' { // quick and dirty way to check if `buffer` actually contained any values
continue
}
sqls.WriteByte(';')
buffer.WriteByte(';')

readDur := time.Since(start)
readTotalDur += readDur
metric.BlockReadSecondsHistogram.Observe(readDur.Seconds())
metric.BlockReadBytesHistogram.Observe(float64(sqls.Len()))
metric.BlockReadBytesHistogram.Observe(float64(buffer.Len()))

// sql -> kv
start = time.Now()
kvs, _, err := kvEncoder.SQL2KV(sqls.String())
kvs, _, err := kvEncoder.SQL2KV(buffer.String())
encodeDur := time.Since(start)
encodeTotalDur += encodeDur
metric.BlockEncodeSecondsHistogram.Observe(encodeDur.Seconds())

common.AppLogger.Debugf("len(kvs) %d, len(sql) %d", len(kvs), sqls.Len())
common.AppLogger.Debugf("len(kvs) %d, len(sql) %d", len(kvs), buffer.Len())
if err != nil {
common.AppLogger.Errorf("kv encode failed = %s\n", err.Error())
return errors.Trace(err)
Expand Down

0 comments on commit c807b62

Please sign in to comment.