1
0
Fork 0
forked from mirrors/tar-split

tar/storage: Replace TeeReader with MultiWriter

It uses slightly less memory and more understandable.
Benchmar results:

benchmark             old ns/op     new ns/op     delta
BenchmarkPutter-4     57272         52375         -8.55%

benchmark             old allocs     new allocs     delta
BenchmarkPutter-4     21             19             -9.52%
benchmark             old bytes     new bytes     delta
BenchmarkPutter-4     19416         13336         -31.31%

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov 2015-08-13 11:42:43 -07:00
parent ea73dc6f6f
commit 45399711c2

View file

@ -59,15 +59,15 @@ func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) {
} }
func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) (int64, []byte, error) { func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) (int64, []byte, error) {
c := crc64.New(CRCTable) crc := crc64.New(CRCTable)
tRdr := io.TeeReader(r, c) buf := bytes.NewBuffer(nil)
b := bytes.NewBuffer([]byte{}) cw := io.MultiWriter(crc, buf)
i, err := io.Copy(b, tRdr) i, err := io.Copy(cw, r)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
bfgp.files[name] = b.Bytes() bfgp.files[name] = buf.Bytes()
return i, c.Sum(nil), nil return i, crc.Sum(nil), nil
} }
type readCloserWrapper struct { type readCloserWrapper struct {