From 9594fe5dd378f5f210793f84ebc2e7a346e61466 Mon Sep 17 00:00:00 2001 From: Silvio Moioli Date: Thu, 19 Oct 2017 09:25:27 +0200 Subject: [PATCH] Use a bigger buffer when computing checksums This yields ~5x speedup --- get/filestorage.go | 9 +++++---- util/io.go | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/get/filestorage.go b/get/filestorage.go index 5c565f9..ac88aaf 100644 --- a/get/filestorage.go +++ b/get/filestorage.go @@ -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 @@ -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)) diff --git a/util/io.go b/util/io.go index 018f2b7..29c0875 100644 --- a/util/io.go +++ b/util/io.go @@ -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