Reduce memory usage of decryptReader

benchmark                         old ns/op     new ns/op     delta
    BenchmarkChunkEncrypt             260007360     261144414     +0.44%
    BenchmarkChunkEncryptParallel     262839697     261201438     -0.62%
    BenchmarkArchiveDirectory         0.00          0.00          +0.00%
    BenchmarkEncryptWriter            86994839      88297245      +1.50%
    BenchmarkEncrypt                  87414849      87406446      -0.01%
    BenchmarkDecryptReader            90354651      89948630      -0.45%
    BenchmarkEncryptDecryptReader     184533845     178374144     -3.34%
    BenchmarkDecrypt                  88153894      88289705      +0.15%
    BenchmarkSaveJSON                 213906        213917        +0.01%
    BenchmarkSaveFrom                 75263853      74881361      -0.51%

    benchmark                         old MB/s     new MB/s     speedup
    BenchmarkChunkEncrypt             40.33        40.15        1.00x
    BenchmarkChunkEncryptParallel     39.89        40.14        1.01x
    BenchmarkEncryptWriter            96.43        95.00        0.99x
    BenchmarkEncrypt                  95.96        95.97        1.00x
    BenchmarkDecryptReader            92.84        93.26        1.00x
    BenchmarkEncryptDecryptReader     45.46        47.03        1.03x
    BenchmarkDecrypt                  95.16        95.01        1.00x
    BenchmarkSaveFrom                 55.73        56.01        1.01x

    benchmark                         old allocs     new allocs     delta
    BenchmarkChunkEncrypt             113            113            +0.00%
    BenchmarkChunkEncryptParallel     104            104            +0.00%
    BenchmarkArchiveDirectory         0              0              +0.00%
    BenchmarkEncryptWriter            20             20             +0.00%
    BenchmarkEncrypt                  14             14             +0.00%
    BenchmarkDecryptReader            18             18             +0.00%
    BenchmarkEncryptDecryptReader     55             40             -27.27%
    BenchmarkDecrypt                  17             17             +0.00%
    BenchmarkSaveJSON                 125            125            +0.00%
    BenchmarkSaveFrom                 119            116            -2.52%

    benchmark                         old bytes     new bytes     delta
    BenchmarkChunkEncrypt             8515750       8515750       +0.00%
    BenchmarkChunkEncryptParallel     8515766       8515766       +0.00%
    BenchmarkArchiveDirectory         0             0             +0.00%
    BenchmarkEncryptWriter            28927         28927         +0.00%
    BenchmarkEncrypt                  422313        422313        +0.00%
    BenchmarkDecryptReader            527827        527827        +0.00%
    BenchmarkEncryptDecryptReader     35814894      4100824       -88.55%
    BenchmarkDecrypt                  8391127       8391127       +0.00%
    BenchmarkSaveJSON                 9208          9208          +0.00%
    BenchmarkSaveFrom                 40541         39694         -2.09%
This commit is contained in:
Alexander Neumann 2015-02-17 18:14:39 +01:00
parent be03a31bdd
commit af57fb86d7
2 changed files with 15 additions and 4 deletions

16
key.go
View file

@ -529,13 +529,23 @@ func (d *decryptReader) Read(dst []byte) (int, error) {
return n, nil
}
func (d *decryptReader) Close() error {
if d.buf == nil {
return nil
}
FreeChunkBuf("decryptReader", d.buf)
d.buf = nil
return nil
}
// decryptFrom verifies and decrypts the ciphertext read from rd with ks and
// makes it available on the returned Reader. Ciphertext must be in the form IV
// || Ciphertext || HMAC. In order to correctly verify the ciphertext, rd is
// drained, locally buffered and made available on the returned Reader
// afterwards. If an HMAC verification failure is observed, it is returned
// immediately.
func (k *Key) decryptFrom(ks *keys, rd io.Reader) (io.Reader, error) {
func (k *Key) decryptFrom(ks *keys, rd io.Reader) (io.ReadCloser, error) {
ciphertext := GetChunkBuf("decryptReader")
ciphertext = ciphertext[0:cap(ciphertext)]
n, err := io.ReadFull(rd, ciphertext)
@ -600,7 +610,7 @@ func (k *Key) decryptFrom(ks *keys, rd io.Reader) (io.Reader, error) {
// drained, locally buffered and made available on the returned Reader
// afterwards. If an HMAC verification failure is observed, it is returned
// immediately.
func (k *Key) DecryptFrom(rd io.Reader) (io.Reader, error) {
func (k *Key) DecryptFrom(rd io.Reader) (io.ReadCloser, error) {
return k.decryptFrom(k.master, rd)
}
@ -610,7 +620,7 @@ func (k *Key) DecryptFrom(rd io.Reader) (io.Reader, error) {
// rd is drained, locally buffered and made available on the returned Reader
// afterwards. If an HMAC verification failure is observed, it is returned
// immediately.
func (k *Key) DecryptUserFrom(rd io.Reader) (io.Reader, error) {
func (k *Key) DecryptUserFrom(rd io.Reader) (io.ReadCloser, error) {
return k.decryptFrom(k.user, rd)
}

View file

@ -201,9 +201,10 @@ func BenchmarkEncryptDecryptReader(b *testing.B) {
b.ResetTimer()
b.SetBytes(int64(size))
buf := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
rd.Seek(0, 0)
buf := bytes.NewBuffer(nil)
buf.Reset()
wr := k.EncryptTo(buf)
_, err := io.Copy(wr, rd)
ok(b, err)