Skip to content

Commit

Permalink
Use a bigger buffer when computing checksums
Browse files Browse the repository at this point in the history
This yields ~5x speedup
  • Loading branch information
moio committed Oct 20, 2017
1 parent 50d7589 commit 9594fe5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions get/filestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (

// FileStorage allows to store data in a local directory
type FileStorage struct {
directory string
directory string
checksumBuffer []byte
}

// NewFileStorage returns a new Storage given a local directory
func NewFileStorage(directory string) Storage {
return &FileStorage{directory}
return &FileStorage{directory, make([]byte, 4*1024*1024)}
}

// Checksum returns the checksum value of a file in the permanent location, according to the checksumType algorithm
Expand All @@ -41,13 +42,13 @@ func (s *FileStorage) Checksum(filename string, checksumType ChecksumType) (chec
switch checksumType {
case SHA1:
h := sha1.New()
if _, err = io.Copy(h, f); err != nil {
if _, err = io.CopyBuffer(h, f, s.checksumBuffer); err != nil {
log.Fatal(err)
}
checksum = hex.EncodeToString(h.Sum(nil))
case SHA256:
h := sha256.New()
if _, err = io.Copy(h, f); err != nil {
if _, err = io.CopyBuffer(h, f, s.checksumBuffer); err != nil {
log.Fatal(err)
}
checksum = hex.EncodeToString(h.Sum(nil))
Expand Down
4 changes: 3 additions & 1 deletion util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ func (t *TeeReadCloser) Read(p []byte) (n int, err error) {
return t.teeReader.Read(p)
}

var discardBuffer = make([]byte, 4*1024*1024)

// Close closes the internal reader and writer
func (t *TeeReadCloser) Close() (err error) {
// read any remaining bytes from the teeReader (discarding them)
io.Copy(ioutil.Discard, t.teeReader)
io.CopyBuffer(ioutil.Discard, t.teeReader, discardBuffer)
err = t.reader.Close()
if err != nil {
return
Expand Down

0 comments on commit 9594fe5

Please sign in to comment.