From f4ae0304aff2f721ebe6088aa4f4c83f872f1ee9 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Tue, 8 Dec 2015 13:22:57 +0100 Subject: opensslGCM: preallocate buffer space, improves performance by 11% Results of cryptfs/openssl_benchmark.bash : Before: BenchmarkEnc_OpenSSL_4k_AES256_nonce96-2 50000 31802 ns/op 127.28 MB/s BenchmarkEnc_OpenSSL_4k_AES256_nonce128-2 50000 32110 ns/op 126.06 MB/s After: BenchmarkEnc_OpenSSL_4k_AES256_nonce96-2 50000 28612 ns/op 141.47 MB/s BenchmarkEnc_OpenSSL_4k_AES256_nonce128-2 50000 28613 ns/op 141.47 MB/s --- cryptfs/openssl_aead.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cryptfs/openssl_aead.go b/cryptfs/openssl_aead.go index 1ec7c48..c70bd1f 100644 --- a/cryptfs/openssl_aead.go +++ b/cryptfs/openssl_aead.go @@ -25,7 +25,11 @@ func (be opensslGCM) NonceSize() int { // time, for a given key. func (be opensslGCM) Seal(dst, nonce, plaintext, data []byte) []byte { - cipherBuf := bytes.NewBuffer(dst) + // Preallocate output buffer + var cipherBuf bytes.Buffer + cipherBuf.Grow(len(dst) + len(plaintext) + AUTH_TAG_LEN) + // Output will be appended to dst + cipherBuf.Write(dst) ectx, err := openssl.NewGCMEncryptionCipherCtx(KEY_LEN*8, nil, be.key, nonce) if err != nil { -- cgit v1.2.3