Skip to content

Commit

Permalink
streaming perf
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Mulvey <kmulvey@linux.com>
  • Loading branch information
kmulvey committed Apr 21, 2023
1 parent 723bd6b commit e193de2
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func (c *ConcurrentHash) streamFile(filePath string, blocks chan<- block) error
}

var r = bufio.NewReader(file)
var buf = make([]byte, 0, c.BlockSize)
var index int
for {
n, err := r.Read(buf[:cap(buf)])
buf = buf[:n]
var data = make([]byte, c.BlockSize)
n, err := io.ReadFull(r, data)
data = data[:n]
if n == 0 {
if err == nil {
continue
Expand All @@ -36,16 +36,21 @@ func (c *ConcurrentHash) streamFile(filePath string, blocks chan<- block) error
}
return err
}
if err != nil && err != io.EOF {
return err
}

// Write must not modify the slice data, even temporarily. Implementations must not retain p
// https://pkg.go.dev/io#Writer
var transportArr = make([]byte, len(buf))
copy(transportArr, buf)
blocks <- block{Index: index, Data: transportArr}
blocks <- block{Index: index, Data: data}
index++

if err != nil {
if err == io.EOF {
break
}
// assuming it is not an error
// if the last file block is short
if err == io.ErrUnexpectedEOF {
break
}
return err
}
}

return file.Close()
Expand Down

0 comments on commit e193de2

Please sign in to comment.