Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fewer unsafe hacks for AppendVec #3801

Merged
merged 9 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions runtime/benches/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn append_vec_append(bencher: &mut Bencher) {
let path = get_append_vec_path("bench_append");
let vec = AppendVec::new(&path.path, true, 64 * 1024);
bencher.iter(|| {
let account = create_test_account(0);
if vec.append_account(&account).is_none() {
let (meta, account) = create_test_account(0);
if vec.append_account(meta, &account).is_none() {
vec.reset();
}
});
Expand All @@ -26,8 +26,8 @@ fn add_test_accounts(vec: &AppendVec, size: usize) -> Vec<(usize, usize)> {
(0..size)
.into_iter()
.filter_map(|sample| {
let account = create_test_account(sample);
vec.append_account(&account).map(|pos| (sample, pos))
let (meta, account) = create_test_account(sample);
vec.append_account(meta, &account).map(|pos| (sample, pos))
})
.collect()
}
Expand All @@ -40,9 +40,9 @@ fn append_vec_sequential_read(bencher: &mut Bencher) {
let mut indexes = add_test_accounts(&vec, size);
bencher.iter(|| {
let (sample, pos) = indexes.pop().unwrap();
let account = vec.get_account(pos);
let test = create_test_account(sample);
assert_eq!(*account, test);
let (account, _next) = vec.get_account(pos).unwrap();
let (_meta, test) = create_test_account(sample);
assert_eq!(account.data, test.data.as_slice());
indexes.push((sample, pos));
});
}
Expand All @@ -55,9 +55,9 @@ fn append_vec_random_read(bencher: &mut Bencher) {
bencher.iter(|| {
let random_index: usize = thread_rng().gen_range(0, indexes.len());
let (sample, pos) = &indexes[random_index];
let account = vec.get_account(*pos);
let test = create_test_account(*sample);
assert_eq!(*account, test);
let (account, _next) = vec.get_account(*pos).unwrap();
let (_meta, test) = create_test_account(*sample);
assert_eq!(account.data, test.data.as_slice());
});
}

Expand All @@ -70,8 +70,8 @@ fn append_vec_concurrent_append_read(bencher: &mut Bencher) {
let indexes1 = indexes.clone();
spawn(move || loop {
let sample = indexes1.lock().unwrap().len();
let account = create_test_account(sample);
if let Some(pos) = vec1.append_account(&account) {
let (meta, account) = create_test_account(sample);
if let Some(pos) = vec1.append_account(meta, &account) {
indexes1.lock().unwrap().push((sample, pos))
} else {
break;
Expand All @@ -84,9 +84,9 @@ fn append_vec_concurrent_append_read(bencher: &mut Bencher) {
let len = indexes.lock().unwrap().len();
let random_index: usize = thread_rng().gen_range(0, len);
let (sample, pos) = indexes.lock().unwrap().get(random_index).unwrap().clone();
let account = vec.get_account(pos);
let test = create_test_account(sample);
assert_eq!(*account, test);
let (account, _next) = vec.get_account(pos).unwrap();
let (_meta, test) = create_test_account(sample);
assert_eq!(account.data, test.data.as_slice());
});
}

Expand All @@ -106,14 +106,14 @@ fn append_vec_concurrent_read_append(bencher: &mut Bencher) {
.get(random_index % len)
.unwrap()
.clone();
let account = vec1.get_account(pos);
let test = create_test_account(sample);
assert_eq!(*account, test);
let (account, _next) = vec1.get_account(pos).unwrap();
let (_meta, test) = create_test_account(sample);
assert_eq!(account.data, test.data.as_slice());
});
bencher.iter(|| {
let sample: usize = thread_rng().gen_range(0, 256);
let account = create_test_account(sample);
if let Some(pos) = vec.append_account(&account) {
let (meta, account) = create_test_account(sample);
if let Some(pos) = vec.append_account(meta, &account) {
indexes.lock().unwrap().push((sample, pos))
}
});
Expand Down
51 changes: 30 additions & 21 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! commit for each fork entry would be indexed.

use crate::accounts_index::{AccountsIndex, Fork};
use crate::append_vec::{AppendVec, StoredAccount};
use crate::append_vec::{AppendVec, StorageMeta, StoredAccount};
use crate::message_processor::has_duplicates;
use bincode::serialize;
use hashbrown::{HashMap, HashSet};
Expand All @@ -29,7 +29,7 @@ use rayon::prelude::*;
use solana_metrics::counter::Counter;
use solana_sdk::account::Account;
use solana_sdk::fee_calculator::FeeCalculator;
use solana_sdk::hash::{hash, Hash, Hasher};
use solana_sdk::hash::{Hash, Hasher};
use solana_sdk::native_loader;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
Expand Down Expand Up @@ -278,14 +278,21 @@ impl AccountsDB {
.collect()
}

fn hash_account(stored_account: &StoredAccount) -> Hash {
let mut hasher = Hasher::default();
hasher.hash(&serialize(&stored_account.balance).unwrap());
hasher.hash(stored_account.data);
hasher.result()
}

pub fn hash_internal_state(&self, fork_id: Fork) -> Option<Hash> {
let accumulator: Vec<Vec<(Pubkey, u64, Hash)>> = self.scan_account_storage(
fork_id,
|stored_account: &StoredAccount, accum: &mut Vec<(Pubkey, u64, Hash)>| {
accum.push((
stored_account.pubkey,
stored_account.write_version,
hash(&serialize(&stored_account.account).unwrap()),
stored_account.meta.pubkey,
stored_account.meta.write_version,
Self::hash_account(stored_account),
));
},
);
Expand Down Expand Up @@ -313,7 +320,7 @@ impl AccountsDB {
//TODO: thread this as a ref
storage
.get(info.id)
.map(|store| store.accounts.get_account(info.offset).account.clone())
.and_then(|store| Some(store.accounts.get_account(info.offset)?.0.clone_account()))
}

fn load_tx_accounts(
Expand Down Expand Up @@ -530,13 +537,12 @@ impl AccountsDB {
{
let accounts = &self.storage.read().unwrap()[id];
let write_version = self.write_version.fetch_add(1, Ordering::Relaxed) as u64;
let stored_account = StoredAccount {
let meta = StorageMeta {
write_version,
pubkey: *pubkey,
//TODO: fix all this copy
account: account.clone(),
data_len: account.data.len() as u64,
};
result = accounts.accounts.append_account(&stored_account);
result = accounts.accounts.append_account(meta, account);
accounts.add_account();
}
if let Some(val) = result {
Expand Down Expand Up @@ -691,21 +697,24 @@ impl Accounts {
}

pub fn load_by_program(&self, fork: Fork, program_id: &Pubkey) -> Vec<(Pubkey, Account)> {
let accumulator: Vec<Vec<StoredAccount>> = self.accounts_db.scan_account_storage(
let accumulator: Vec<Vec<(Pubkey, u64, Account)>> = self.accounts_db.scan_account_storage(
fork,
|stored_account: &StoredAccount, accum: &mut Vec<StoredAccount>| {
if stored_account.account.owner == *program_id {
accum.push(stored_account.clone())
|stored_account: &StoredAccount, accum: &mut Vec<(Pubkey, u64, Account)>| {
if stored_account.balance.owner == *program_id {
let val = (
stored_account.meta.pubkey,
stored_account.meta.write_version,
stored_account.clone_account(),
);
accum.push(val)
}
},
);
let mut versions: Vec<StoredAccount> = accumulator.into_iter().flat_map(|x| x).collect();
versions.sort_by_key(|s| (s.pubkey, (s.write_version as i64).neg()));
versions.dedup_by_key(|s| s.pubkey);
versions
.into_iter()
.map(|s| (s.pubkey, s.account))
.collect()
let mut versions: Vec<(Pubkey, u64, Account)> =
accumulator.into_iter().flat_map(|x| x).collect();
versions.sort_by_key(|s| (s.0, (s.1 as i64).neg()));
versions.dedup_by_key(|s| s.0);
versions.into_iter().map(|s| (s.0, s.2)).collect()
}

/// Slow because lock is held for 1 operation instead of many
Expand Down
Loading