Merge pull request #7054 from paritytech/allocate_with_zeroes

Small performance gain in allocations
This commit is contained in:
Marek Kotewicz 2017-11-14 15:48:11 +01:00 committed by GitHub
commit e6048e4a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 14 deletions

View File

@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::iter::repeat;
use std::str; use std::str;
use ethkey::Secret; use ethkey::Secret;
use {json, Error, crypto}; use {json, Error, crypto};
@ -90,9 +89,7 @@ impl Crypto {
// preallocated (on-stack in case of `Secret`) buffer to hold cipher // preallocated (on-stack in case of `Secret`) buffer to hold cipher
// length = length(plain) as we are using CTR-approach // length = length(plain) as we are using CTR-approach
let plain_len = plain.len(); let plain_len = plain.len();
let mut ciphertext: SmallVec<[u8; 32]> = SmallVec::new(); let mut ciphertext: SmallVec<[u8; 32]> = SmallVec::from_vec(vec![0; plain_len]);
ciphertext.grow(plain_len);
ciphertext.extend(repeat(0).take(plain_len));
// aes-128-ctr with initial vector of iv // aes-128-ctr with initial vector of iv
crypto::aes::encrypt(&derived_left_bits, &iv, plain, &mut *ciphertext); crypto::aes::encrypt(&derived_left_bits, &iv, plain, &mut *ciphertext);
@ -143,9 +140,7 @@ impl Crypto {
return Err(Error::InvalidPassword); return Err(Error::InvalidPassword);
} }
let mut plain: SmallVec<[u8; 32]> = SmallVec::new(); let mut plain: SmallVec<[u8; 32]> = SmallVec::from_vec(vec![0; expected_len]);
plain.grow(expected_len);
plain.extend(repeat(0).take(expected_len));
match self.cipher { match self.cipher {
Cipher::Aes128Ctr(ref params) => { Cipher::Aes128Ctr(ref params) => {

View File

@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::iter::repeat;
use rand::{Rng, OsRng}; use rand::{Rng, OsRng};
use ethkey::{Public, Secret, math}; use ethkey::{Public, Secret, math};
use crypto; use crypto;
@ -32,10 +31,13 @@ pub fn encrypt_document(key: Bytes, document: Bytes) -> Result<Bytes, Error> {
// use symmetric encryption to encrypt document // use symmetric encryption to encrypt document
let iv = initialization_vector(); let iv = initialization_vector();
let mut encrypted_document = Vec::with_capacity(document.len() + iv.len()); let mut encrypted_document = vec![0; document.len() + iv.len()];
encrypted_document.extend(repeat(0).take(document.len())); {
crypto::aes::encrypt(&key, &iv, &document, &mut encrypted_document); let (mut encryption_buffer, iv_buffer) = encrypted_document.split_at_mut(document.len());
encrypted_document.extend_from_slice(&iv);
crypto::aes::encrypt(&key, &iv, &document, &mut encryption_buffer);
iv_buffer.copy_from_slice(&iv);
}
Ok(encrypted_document) Ok(encrypted_document)
} }
@ -53,8 +55,7 @@ pub fn decrypt_document(key: Bytes, mut encrypted_document: Bytes) -> Result<Byt
// use symmetric decryption to decrypt document // use symmetric decryption to decrypt document
let iv = encrypted_document.split_off(encrypted_document_len - INIT_VEC_LEN); let iv = encrypted_document.split_off(encrypted_document_len - INIT_VEC_LEN);
let mut document = Vec::with_capacity(encrypted_document_len - INIT_VEC_LEN); let mut document = vec![0; encrypted_document_len - INIT_VEC_LEN];
document.extend(repeat(0).take(encrypted_document_len - INIT_VEC_LEN));
crypto::aes::decrypt(&key, &iv, &encrypted_document, &mut document); crypto::aes::decrypt(&key, &iv, &encrypted_document, &mut document);
Ok(document) Ok(document)