contentenc: move parallel encryption into encryptBlocksParallel

Make the logic self-contained in the new helper function.
This commit is contained in:
Jakob Unterwurzacher 2020-02-15 15:49:12 +01:00
parent a92db18fe7
commit 9aeb2a3df6

View File

@ -204,13 +204,9 @@ func (be *ContentEnc) DecryptBlock(ciphertext []byte, blockNo uint64, fileID []b
// 2 seems to work ok for now. // 2 seems to work ok for now.
const encryptMaxSplit = 2 const encryptMaxSplit = 2
// EncryptBlocks is like EncryptBlock but takes multiple plaintext blocks. // encryptBlocksParallel splits the plaintext into parts and encrypts them
// Returns a byte slice from CReqPool - so don't forget to return it // in parallel.
// to the pool. func (be *ContentEnc) encryptBlocksParallel(plaintextBlocks [][]byte, ciphertextBlocks [][]byte, firstBlockNo uint64, fileID []byte) {
func (be *ContentEnc) EncryptBlocks(plaintextBlocks [][]byte, firstBlockNo uint64, fileID []byte) []byte {
ciphertextBlocks := make([][]byte, len(plaintextBlocks))
// For large writes, we parallelize encryption.
if len(plaintextBlocks) >= 32 {
ncpu := runtime.NumCPU() ncpu := runtime.NumCPU()
if ncpu > encryptMaxSplit { if ncpu > encryptMaxSplit {
ncpu = encryptMaxSplit ncpu = encryptMaxSplit
@ -231,6 +227,16 @@ func (be *ContentEnc) EncryptBlocks(plaintextBlocks [][]byte, firstBlockNo uint6
}(i) }(i)
} }
wg.Wait() wg.Wait()
}
// EncryptBlocks is like EncryptBlock but takes multiple plaintext blocks.
// Returns a byte slice from CReqPool - so don't forget to return it
// to the pool.
func (be *ContentEnc) EncryptBlocks(plaintextBlocks [][]byte, firstBlockNo uint64, fileID []byte) []byte {
ciphertextBlocks := make([][]byte, len(plaintextBlocks))
// For large writes, we parallelize encryption.
if len(plaintextBlocks) >= 32 && runtime.NumCPU() >= 2 {
be.encryptBlocksParallel(plaintextBlocks, ciphertextBlocks, firstBlockNo, fileID)
} else { } else {
be.doEncryptBlocks(plaintextBlocks, ciphertextBlocks, firstBlockNo, fileID) be.doEncryptBlocks(plaintextBlocks, ciphertextBlocks, firstBlockNo, fileID)
} }