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

Commit

Permalink
mydump: reduce parser memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng committed Dec 21, 2018
1 parent 7cfaddb commit 5074af5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lightning/mydump/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mydump

import (
"bytes"
"io"

"github.com/pkg/errors"
Expand All @@ -24,6 +25,10 @@ type ChunkParser struct {
// The list of columns in the form `(a, b, c)` in the last INSERT statement.
// Assumed to be constant throughout the entire file.
Columns []byte

// cache
remainBuf *bytes.Buffer
appendBuf *bytes.Buffer
}

// Chunk represents a portion of the data file.
Expand All @@ -43,8 +48,10 @@ type Row struct {
// NewChunkParser creates a new parser which can read chunks out of a file.
func NewChunkParser(reader io.Reader) *ChunkParser {
return &ChunkParser{
reader: reader,
blockBuf: make([]byte, 8192),
reader: reader,
blockBuf: make([]byte, 8192),
remainBuf: &bytes.Buffer{},
appendBuf: &bytes.Buffer{},
}
}

Expand Down Expand Up @@ -91,7 +98,12 @@ func (parser *ChunkParser) readBlock() error {
parser.isLastChunk = true
fallthrough
case nil:
tryAppendTo(&parser.buf, parser.blockBuf[:n])
parser.remainBuf.Reset()
parser.remainBuf.Write(parser.buf)
parser.appendBuf.Reset()
parser.appendBuf.Write(parser.remainBuf.Bytes())
parser.appendBuf.Write(parser.blockBuf[:n])
parser.buf = parser.appendBuf.Bytes()
return nil
default:
return errors.Trace(err)
Expand Down

0 comments on commit 5074af5

Please sign in to comment.