Skip to content

Commit

Permalink
Remove serialize_bytes unneeded indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed May 18, 2020
1 parent bfcfbab commit 7bf89d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 77 deletions.
38 changes: 11 additions & 27 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::{
append_vec::{AppendVec, StoredAccount, StoredMeta},
bank::deserialize_from_snapshot,
};
use bincode::{deserialize_from, serialize_into};
use byteorder::{ByteOrder, LittleEndian};
use fs_extra::dir::CopyOptions;
use lazy_static::lazy_static;
Expand All @@ -46,7 +45,7 @@ use solana_sdk::{
use std::{
collections::{HashMap, HashSet},
fmt,
io::{BufReader, Cursor, Error as IOError, ErrorKind, Read, Result as IOResult},
io::{BufReader, Error as IOError, ErrorKind, Read, Result as IOResult},
ops::RangeBounds,
path::{Path, PathBuf},
sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
Expand Down Expand Up @@ -390,27 +389,19 @@ impl<'a, 'b> Serialize for AccountsDBSerialize<'a, 'b> {
where
S: serde::ser::Serializer,
{
use serde::ser::Error;
let mut wr = Cursor::new(vec![]);
let version = self.accounts_db.write_version.load(Ordering::Relaxed);
let account_storage_serialize = AccountStorageSerialize {
account_storage_entries: self.account_storage_entries,
};
serialize_into(&mut wr, &account_storage_serialize).map_err(Error::custom)?;
serialize_into(&mut wr, &version).map_err(Error::custom)?;
let bank_hashes = self.accounts_db.bank_hashes.read().unwrap();
serialize_into(
&mut wr,
&(
self.slot,
&*bank_hashes
.get(&self.slot)
.unwrap_or_else(|| panic!("No bank_hashes entry for slot {}", self.slot)),
),
)
.map_err(Error::custom)?;
let len = wr.position() as usize;
serializer.serialize_bytes(&wr.into_inner()[..len])
(
account_storage_serialize,
version,
self.slot,
&*bank_hashes
.get(&self.slot)
.unwrap_or_else(|| panic!("No bank_hashes entry for slot {}", self.slot)),
).serialize(serializer)
}
}

Expand Down Expand Up @@ -597,10 +588,8 @@ impl AccountsDB {
mut stream: &mut BufReader<R>,
stream_append_vecs_path: P,
) -> Result<(), IOError> {
let _len: usize =
deserialize_from(&mut stream).map_err(|e| AccountsDB::get_io_error(&e.to_string()))?;
let storage: AccountStorage = deserialize_from_snapshot(&mut stream)
.map_err(|e| AccountsDB::get_io_error(&e.to_string()))?;
let (storage, version, slot, bank_hash): (AccountStorage, u64, Slot, BankHashInfo) = deserialize_from_snapshot(&mut stream)
.map_err(|_| AccountsDB::get_io_error("bank hashes deserialize error"))?;

// Remap the deserialized AppendVec paths to point to correct local paths
let new_storage_map: Result<HashMap<Slot, SlotStores>, IOError> = storage
Expand Down Expand Up @@ -662,11 +651,6 @@ impl AccountsDB {
// but non-root stores should not be included in the snapshot
storage.0.retain(|_slot, stores| !stores.is_empty());

let version: u64 = deserialize_from(&mut stream)
.map_err(|_| AccountsDB::get_io_error("write version deserialize error"))?;

let (slot, bank_hash): (Slot, BankHashInfo) = deserialize_from(&mut stream)
.map_err(|_| AccountsDB::get_io_error("bank hashes deserialize error"))?;
self.bank_hashes.write().unwrap().insert(slot, bank_hash);

// Process deserialized data, set necessary fields in self
Expand Down
38 changes: 4 additions & 34 deletions runtime/src/append_vec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bincode::{deserialize_from, serialize_into};
use memmap::MmapMut;
use serde::{Deserialize, Serialize};
use solana_sdk::{
Expand All @@ -8,10 +7,9 @@ use solana_sdk::{
pubkey::Pubkey,
};
use std::{
fmt,
fs::{remove_file, OpenOptions},
io,
io::{Cursor, Seek, SeekFrom, Write},
io::{Seek, SeekFrom, Write},
mem,
path::{Path, PathBuf},
sync::atomic::{AtomicUsize, Ordering},
Expand Down Expand Up @@ -486,36 +484,7 @@ impl Serialize for AppendVec {
where
S: serde::ser::Serializer,
{
use serde::ser::Error;
let len = std::mem::size_of::<usize>();
let mut buf = vec![0u8; len];
let mut wr = Cursor::new(&mut buf[..]);
serialize_into(&mut wr, &(self.current_len.load(Ordering::Relaxed) as u64))
.map_err(Error::custom)?;
let len = wr.position() as usize;
serializer.serialize_bytes(&wr.into_inner()[..len])
}
}

struct AppendVecVisitor;

impl<'a> serde::de::Visitor<'a> for AppendVecVisitor {
type Value = AppendVec;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Expecting AppendVec")
}

fn visit_bytes<E>(self, data: &[u8]) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
use serde::de::Error;
let mut rd = Cursor::new(&data[..]);
let current_len: usize = deserialize_from(&mut rd).map_err(Error::custom)?;
// Note this does not initialize a valid Mmap in the AppendVec, needs to be done
// externally
Ok(AppendVec::new_empty_map(current_len))
self.current_len.load(Ordering::Relaxed).serialize(serializer)
}
}

Expand All @@ -524,7 +493,8 @@ impl<'de> Deserialize<'de> for AppendVec {
where
D: ::serde::Deserializer<'de>,
{
deserializer.deserialize_bytes(AppendVecVisitor)
let current_len: usize = <usize>::deserialize(deserializer)?;
Ok(AppendVec::new_empty_map(current_len))
}
}

Expand Down
19 changes: 3 additions & 16 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::{
transaction_batch::TransactionBatch,
transaction_utils::OrderedIterator,
};
use bincode::{deserialize_from, serialize_into};
use byteorder::{ByteOrder, LittleEndian};
use itertools::Itertools;
use log::*;
Expand Down Expand Up @@ -60,7 +59,7 @@ use solana_vote_program::vote_state::VoteState;
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
io::{BufReader, Cursor, Error as IOError, Read},
io::{BufReader, Error as IOError, Read},
mem,
ops::RangeInclusive,
path::{Path, PathBuf},
Expand Down Expand Up @@ -114,12 +113,9 @@ impl BankRc {
slot: Slot,
ancestors: &Ancestors,
frozen_account_pubkeys: &[Pubkey],
mut stream: &mut BufReader<R>,
stream: &mut BufReader<R>,
stream_append_vecs_path: P,
) -> std::result::Result<Self, IOError> {
let _len: usize =
deserialize_from(&mut stream).map_err(|e| BankRc::get_io_error(&e.to_string()))?;

let accounts = Accounts::from_stream(
account_paths,
ancestors,
Expand All @@ -138,11 +134,6 @@ impl BankRc {
pub fn get_snapshot_storages(&self, slot: Slot) -> SnapshotStorages {
self.accounts.accounts_db.get_snapshot_storages(slot)
}

fn get_io_error(error: &str) -> IOError {
warn!("BankRc error: {:?}", error);
std::io::Error::new(std::io::ErrorKind::Other, error)
}
}

pub struct BankRcSerialize<'a, 'b> {
Expand All @@ -155,16 +146,12 @@ impl<'a, 'b> Serialize for BankRcSerialize<'a, 'b> {
where
S: serde::ser::Serializer,
{
use serde::ser::Error;
let mut wr = Cursor::new(Vec::new());
let accounts_db_serialize = AccountsDBSerialize::new(
&*self.bank_rc.accounts.accounts_db,
self.bank_rc.slot,
self.snapshot_storages,
);
serialize_into(&mut wr, &accounts_db_serialize).map_err(Error::custom)?;
let len = wr.position() as usize;
serializer.serialize_bytes(&wr.into_inner()[..len])
accounts_db_serialize.serialize(serializer)
}
}

Expand Down

0 comments on commit 7bf89d9

Please sign in to comment.