Browse Source

Reuse buffer when scanning

master
Trevor Slocum 2 years ago
parent
commit
d8fe69deff
  1. 4
      entry.go
  2. 29
      entry_test.go
  3. 5
      scan.go

4
entry.go

@ -91,7 +91,7 @@ func unquoteExec(ex string) string {
return ex
}
func Parse(content io.Reader) (*Entry, error) {
func Parse(content io.Reader, buf []byte) (*Entry, error) {
var (
scanner = bufio.NewScanner(content)
scannedBytes []byte
@ -101,6 +101,8 @@ func Parse(content io.Reader) (*Entry, error) {
foundHeader bool
)
scanner.Buffer(buf, len(buf))
for scanner.Scan() {
scannedBytes = bytes.TrimSpace(scanner.Bytes())
scannedBytesLen = len(scannedBytes)

29
entry_test.go

@ -20,13 +20,19 @@ var testCases = []*testData{
}
func TestParse(t *testing.T) {
var (
buf = make([]byte, bufferSize)
f *os.File
entry *Entry
err error
)
for _, c := range testCases {
f, err := os.OpenFile("test/"+c.Filename, os.O_RDONLY, 0644)
f, err = os.OpenFile("test/"+c.Filename, os.O_RDONLY, 0644)
if err != nil {
t.Fatal(err)
}
entry, err := Parse(f)
entry, err = Parse(f, buf)
f.Close()
if err != nil {
t.Fatal(err)
@ -39,33 +45,42 @@ func TestParse(t *testing.T) {
}
func BenchmarkParse(b *testing.B) {
var files []*os.File
var (
buf = make([]byte, bufferSize)
files = make([]*os.File, len(testCases))
entry *Entry
err error
)
defer func() {
for _, f := range files {
f.Close()
}
}()
for _, c := range testCases {
for i, c := range testCases {
f, err := os.OpenFile("test/"+c.Filename, os.O_RDONLY, 0644)
if err != nil {
b.Fatal(err)
}
files = append(files, f)
files[i] = f
}
b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, f := range files {
for ci, f := range files {
b.StartTimer()
_, err := Parse(f)
entry, err = Parse(f, buf)
if err != nil {
b.Fatal(err)
}
b.StopTimer()
if !reflect.DeepEqual(entry, testCases[ci].Entry) {
b.Fatalf("%s: entry incorrect: got %#v, want %#v", f.Name(), entry, testCases[ci].Entry)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
b.Fatal(err)

5
scan.go

@ -8,6 +8,8 @@ import (
"sync"
)
const bufferSize = 32 * 1024
type scan struct {
e map[int][]*Entry
errs chan error
@ -53,13 +55,14 @@ func Scan(dirs []string) (map[int][]*Entry, error) {
func scanner(s *scan) {
var (
buf = make([]byte, bufferSize)
scanEntry *scanEntry
entry *Entry
err error
)
for scanEntry = range s.in {
entry, err = Parse(scanEntry.f)
entry, err = Parse(scanEntry.f, buf)
scanEntry.f.Close()
if err != nil {
s.errs <- err

Loading…
Cancel
Save