refactor: remove allocs from vault inner loop

`vault` is changed so that instead of clearing the list of
data each time the screen is updated, it tries to compare it
against the existing data and modifies the record if it needs to,
or leaves it untouched.

Furthermore, at least in the password path, temporary strings
are pre-allocated and re-used, instead of freed and re-allocated
every time through the main loop.

The combination of these optimzations reduces the time to re-parse
a couple hundred records from about 1000ms to 200ms, reducing the
overall time to retrieve the database from 1.8s to 1.0s. It also
makes the loop much more stable over time; previously, as the heap
fragmented, performance would slowly degrade. Now that most
records are static and re-used, the performance seems to stay
constant.

The framwork is also put in place to re-use TOTP/U2F records,
but the code still uses the legacy method of clearing the
entire stored list and rebuilding it. However, it wasn't deemed
a priority to fix this because generally I haven't seen use cases
where users have hundreds of tokens or TOTP's; so long as the
number is about 20 or so, the performance difference should
be imperceptible with or without optimization.
This commit is contained in:
bunnie 2023-01-04 03:03:19 +08:00
parent 10b921e4e8
commit c3ecaa00eb
4 changed files with 216 additions and 78 deletions

View file

@ -11,7 +11,7 @@ use std::io::{Write, Read};
use passwords::PasswordGenerator;
use std::cell::RefCell;
use crate::{ux::{ListItem, deserialize_app_info}, ctap::FIDO_CRED_DICT, storage};
use crate::{ux::{ListItem, deserialize_app_info}, ctap::FIDO_CRED_DICT, storage::{self, PasswordRecord, StorageContent}, ItemLists};
use crate::{VaultMode, SelectedEntry};
use crate::utc_now;
@ -53,12 +53,12 @@ pub(crate) enum ActionOp {
pub(crate) fn start_actions_thread(
main_conn: xous::CID,
sid: SID, mode: Arc::<Mutex::<VaultMode>>,
item_list: Arc::<Mutex::<Vec::<ListItem>>>,
item_lists: Arc::<Mutex::<ItemLists>>,
action_active: Arc::<AtomicBool>,
) {
let _ = thread::spawn({
move || {
let mut manager = ActionManager::new(main_conn, mode, item_list, action_active);
let mut manager = ActionManager::new(main_conn, mode, item_lists, action_active);
loop {
let msg = xous::receive_message(sid).unwrap();
let opcode: Option<ActionOp> = FromPrimitive::from_usize(msg.body.id());
@ -134,7 +134,7 @@ struct ActionManager<'a> {
trng: RefCell::<trng::Trng>,
mode: Arc::<Mutex::<VaultMode>>,
item_list: Arc::<Mutex::<Vec::<ListItem>>>,
item_lists: Arc::<Mutex::<ItemLists>>,
pddb: RefCell::<pddb::Pddb>,
tt: ticktimer_server::Ticktimer,
action_active: Arc::<AtomicBool>,
@ -153,7 +153,7 @@ impl<'a> ActionManager<'a> {
pub fn new(
main_conn: xous::CID,
mode: Arc::<Mutex::<VaultMode>>,
item_list: Arc::<Mutex::<Vec::<ListItem>>>,
item_lists: Arc::<Mutex::<ItemLists>>,
action_active: Arc::<AtomicBool>
) -> ActionManager<'a> {
let xns = xous_names::XousNames::new().unwrap();
@ -181,7 +181,7 @@ impl<'a> ActionManager<'a> {
mode_cache: mc,
mode,
item_list,
item_lists,
pddb: RefCell::new(pddb::Pddb::new()),
tt: ticktimer_server::Ticktimer::new().unwrap(),
action_active,
@ -716,6 +716,10 @@ impl<'a> ActionManager<'a> {
/// Populate the display list with data from the PDDB. Limited by total available RAM; probably
/// would stop working if you have over 500-1k records with the current heap limits.
///
/// This has been performance-optimized for our platform:
/// - `format!()` is very slow, so we use `push_str()` where possible
/// - allocations are slow, so we try to avoid them at all costs
pub(crate) fn retrieve_db(&mut self) {
#[cfg(feature="vaultperf")]
self.pm.stop_and_reset();
@ -727,11 +731,10 @@ impl<'a> ActionManager<'a> {
self.mode_cache = {
(*self.mode.lock().unwrap()).clone()
};
let il = &mut *self.item_list.lock().unwrap();
il.clear();
log::info!("heap usage A: {}", heap_usage());
log::debug!("heap usage A: {}", heap_usage());
match self.mode_cache {
VaultMode::Password => {
let il = &mut self.item_lists.lock().unwrap().pw;
let start = self.tt.elapsed_ms();
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 1, std::line!());
@ -741,39 +744,91 @@ impl<'a> ActionManager<'a> {
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 1, std::line!());
let mut oom_keys = 0;
il.reserve(keys.len());
// allocate a re-usable temporary buffers, to avoid triggering allocs
let mut pw_rec = PasswordRecord::alloc();
let mut extra = String::with_capacity(256);
let mut desc = String::with_capacity(256);
let mut lookup_key = String::with_capacity(384);
for key in keys {
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 2, std::line!());
if let Some(data) = key.data {
if let Some(pw) = storage::PasswordRecord::try_from(data).ok() { // this call is slow
let human_time = crate::ux::atime_to_str(pw.atime);
if pw_rec.from_vec(data).is_ok() {
// reset the re-usable structures
lookup_key.clear();
desc.clear();
extra.clear();
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 2, std::line!());
// avoid allocations
let mut extra = String::with_capacity(
human_time.len() +
t!("vault.u2f.appinfo.authcount", xous::LANG).len() +
11 // space for "count" and "; "
);
extra.push_str(&human_time);
extra.push_str("; ");
extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG));
extra.push_str(&pw.count.to_string());
// this region is slow
let mut desc = String::with_capacity(
pw.description.len() + 1 + pw.username.len()
);
desc.push_str(&pw.description);
// build the description string
desc.push_str(&pw_rec.description);
desc.push_str("/");
desc.push_str(&pw.username);
let li = ListItem {
name: desc,
extra,
dirty: true,
guid: key.name,
};
desc.push_str(&pw_rec.username);
// build the storage key in the list array
lookup_key.push_str(&desc);
lookup_key.push_str(&key.name);
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 2, std::line!());
if let Some(prev_entry) = il.get_mut(&lookup_key) {
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 3, std::line!());
prev_entry.dirty = true;
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!());
if prev_entry.atime != pw_rec.atime || prev_entry.count != pw_rec.count {
// this is expensive, so don't run it unless we have to
let human_time = crate::ux::atime_to_str(pw_rec.atime);
extra.push_str(&human_time);
extra.push_str("; ");
extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG));
extra.push_str(&pw_rec.count.to_string());
prev_entry.extra.clear();
prev_entry.extra.push_str(&extra);
}
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!());
if prev_entry.name != desc { // this check should be redundant, but, leave it in to be safe
prev_entry.name.clear();
prev_entry.name.push_str(&desc);
}
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!());
if prev_entry.guid != key.name {
prev_entry.guid.clear();
prev_entry.guid.push_str(&key.name);
}
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 3, std::line!());
} else {
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 4, std::line!());
let human_time = crate::ux::atime_to_str(pw_rec.atime);
extra.push_str(&human_time);
extra.push_str("; ");
extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG));
extra.push_str(&pw_rec.count.to_string());
let li = ListItem {
name: desc.to_string(), // these allocs will be slow, but we do it only once on boot
extra: extra.to_string(),
dirty: true,
guid: key.name,
atime: pw_rec.atime,
count: pw_rec.count,
};
il.insert(li.key(), li); // this push is very slow, but we only have to do it once on boot
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 4, std::line!());
}
klen += 1;
il.push(li); // this push is very slow
} else {
log::warn!("Couldn't interpret password record: {}", key.name);
}
} else {
oom_keys += 1;
@ -805,10 +860,12 @@ impl<'a> ActionManager<'a> {
VaultMode::Fido => {
// first assemble U2F records
log::debug!("listing in {}", U2F_APP_DICT);
let il = &mut self.item_lists.lock().unwrap().fido;
// regen from scratch every time, It's slow, but we're counting on <20 FIDO entries on average
il.clear();
match self.pddb.borrow().read_dict(U2F_APP_DICT, None, Some(256 * 1024)) {
Ok(keys) => {
let mut oom_keys = 0;
il.reserve(keys.len());
for key in keys {
if let Some(data) = key.data {
if let Some(ai) = deserialize_app_info(data) {
@ -823,8 +880,10 @@ impl<'a> ActionManager<'a> {
extra,
dirty: true,
guid: key.name,
count: ai.count,
atime: ai.atime,
};
il.push(li);
il.insert(li.key(), li);
} else {
let err = format!("{}:{}:{}: ({})[moved data]...",
key.basis, U2F_APP_DICT, key.name, key.len
@ -857,7 +916,6 @@ impl<'a> ActionManager<'a> {
match self.pddb.borrow().read_dict(FIDO_CRED_DICT, None, Some(256 * 1024)) {
Ok(keys) => {
let mut oom_keys = 0;
il.reserve(keys.len());
for key in keys {
if let Some(data) = key.data {
match crate::ctap::storage::deserialize_credential(&data) {
@ -874,8 +932,10 @@ impl<'a> ActionManager<'a> {
extra,
dirty: true,
guid: key.name,
count: 0,
atime: 0,
};
il.push(li);
il.insert(li.key(), li);
}
None => {
let err = format!("{}:{}:{}: ({}){:x?}...",
@ -907,10 +967,11 @@ impl<'a> ActionManager<'a> {
}
}
VaultMode::Totp => {
let il = &mut self.item_lists.lock().unwrap().totp;
il.clear();
match self.pddb.borrow().read_dict(VAULT_TOTP_DICT, None, Some(256 * 1024)) {
Ok(keys) => {
let mut oom_keys = 0;
il.reserve(keys.len());
for key in keys {
if let Some(data) = key.data {
if let Some(totp) = storage::TotpRecord::try_from(data).ok() {
@ -927,8 +988,10 @@ impl<'a> ActionManager<'a> {
extra,
dirty: true,
guid: key.name,
count: 0,
atime: 0,
};
il.push(li);
il.insert(li.key(), li);
} else {
let err = format!("{}:{}:{}: ({})[moved data]...",
key.basis, VAULT_TOTP_DICT, key.name, key.len
@ -958,8 +1021,7 @@ impl<'a> ActionManager<'a> {
}
}
}
log::info!("heap usage B: {}", heap_usage());
il.sort();
log::debug!("heap usage B: {}", heap_usage());
#[cfg(feature="vaultperf")]
{
self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 0, std::line!());

View file

@ -31,6 +31,7 @@ use actions::{ActionOp, start_actions_thread};
use crate::prereqs::ntp_updater;
use crate::vendor_commands::VendorSession;
use std::collections::BTreeMap;
// CTAP2 testing notes:
// run our branch and use this to forward the prompts on to the device:
@ -159,6 +160,17 @@ struct SelectedEntry {
mode: VaultMode,
}
struct ItemLists {
pub fido: BTreeMap::<String, ListItem>,
pub totp: BTreeMap::<String, ListItem>,
pub pw: BTreeMap::<String, ListItem>,
}
impl ItemLists {
pub fn new() -> Self {
ItemLists { fido: BTreeMap::new(), totp: BTreeMap::new(), pw: BTreeMap::new() }
}
}
static SELF_CONN: AtomicU32 = AtomicU32::new(0);
const ERR_TIMEOUT_MS: usize = 5000;
@ -177,7 +189,7 @@ fn main() -> ! {
// global shared state between threads.
let mode = Arc::new(Mutex::new(VaultMode::Fido));
let item_list = Arc::new(Mutex::new(Vec::<ListItem>::new()));
let item_lists = Arc::new(Mutex::new(ItemLists::new()));
let action_active = Arc::new(AtomicBool::new(false));
let allow_host = Arc::new(AtomicBool::new(false));
@ -186,7 +198,13 @@ fn main() -> ! {
// redraws of the background list to block/fail.
let actions_sid = xous::create_server().unwrap();
SELF_CONN.store(conn, Ordering::SeqCst);
start_actions_thread(conn, actions_sid, mode.clone(), item_list.clone(), action_active.clone());
start_actions_thread(
conn,
actions_sid,
mode.clone(),
item_lists.clone(),
action_active.clone()
);
let actions_conn = xous::connect(actions_sid).unwrap();
@ -307,7 +325,16 @@ fn main() -> ! {
// this will block all initialization until the prereqs are met
let (token, mut allow_redraw) = prereqs::prereqs(sid, time_conn);
let mut vaultux = VaultUx::new(token, &xns, sid, menu_mgr, actions_conn, mode.clone(), item_list, action_active.clone());
let mut vaultux = VaultUx::new(
token,
&xns,
sid,
menu_mgr,
actions_conn,
mode.clone(),
item_lists,
action_active.clone()
);
// Trigger the mode update in the actions
send_message(actions_conn,
Message::new_blocking_scalar(ActionOp::UpdateMode.to_usize().unwrap(), 0, 0, 0, 0)

View file

@ -349,7 +349,7 @@ impl StorageContent for TotpRecord {
}
fn from_vec(&mut self, data: Vec<u8>) -> Result<(), Error> {
let desc_str = String::from_utf8(data).or(Err(TOTPSerializationError::MalformedInput))?;
let desc_str = std::str::from_utf8(&data).or(Err(TOTPSerializationError::MalformedInput))?;
let mut pr = TotpRecord::default();
@ -462,7 +462,7 @@ impl TryFrom<Vec<u8>> for TotpRecord {
type Error = TOTPSerializationError;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let desc_str = String::from_utf8(value).or(Err(TOTPSerializationError::MalformedInput))?;
let desc_str = std::str::from_utf8(&value).or(Err(TOTPSerializationError::MalformedInput))?;
let mut pr = TotpRecord {
version: VAULT_TOTP_REC_VERSION,
@ -595,6 +595,33 @@ pub struct PasswordRecord {
pub atime: u64,
pub count: u64,
}
impl PasswordRecord {
pub fn alloc() -> Self {
// The intent is only one of these is allocated, and it is re-used.
// Sizes picked to be big enough to probably avoid re-allocs,
// yet small enough to not be unreasonable for a temporary buffer.
PasswordRecord {
version: 0,
description: String::with_capacity(256),
username: String::with_capacity(256),
password: String::with_capacity(256),
notes: String::with_capacity(1024),
ctime: 0,
atime: 0,
count: 0,
}
}
pub fn clear(&mut self) {
self.description.clear();
self.username.clear();
self.password.clear();
self.notes.clear();
self.version = 0;
self.ctime = 0;
self.atime = 0;
self.count = 0;
}
}
impl StorageContent for PasswordRecord {
fn settings(&self) -> ContentPDDBSettings {
@ -609,10 +636,10 @@ impl StorageContent for PasswordRecord {
}
fn from_vec(&mut self, data: Vec<u8>) -> Result<(), Error> {
self.clear();
// use `std::str` so we're allocating this temporary on the stack
let desc_str =
String::from_utf8(data).or(Err(PasswordSerializationError::MalformedInput))?;
let mut pr = PasswordRecord::default();
std::str::from_utf8(&data).or(Err(PasswordSerializationError::MalformedInput))?;
let lines = desc_str.split('\n');
for line in lines {
@ -620,19 +647,19 @@ impl StorageContent for PasswordRecord {
match tag {
"version" => {
if let Ok(ver) = u32::from_str_radix(data, 10) {
pr.version = ver
self.version = ver
} else {
log::warn!("ver error");
return Err(PasswordSerializationError::BadVersion)?;
}
}
"description" => pr.description.push_str(data),
"username" => pr.username.push_str(data),
"password" => pr.password.push_str(data),
"notes" => pr.notes.push_str(data),
"description" => self.description.push_str(data),
"username" => self.username.push_str(data),
"password" => self.password.push_str(data),
"notes" => self.notes.push_str(data),
"ctime" => {
if let Ok(ctime) = u64::from_str_radix(data, 10) {
pr.ctime = ctime;
self.ctime = ctime;
} else {
log::warn!("ctime error");
return Err(PasswordSerializationError::BadCtime)?;
@ -640,7 +667,7 @@ impl StorageContent for PasswordRecord {
}
"atime" => {
if let Ok(atime) = u64::from_str_radix(data, 10) {
pr.atime = atime;
self.atime = atime;
} else {
log::warn!("atime error");
return Err(PasswordSerializationError::BadAtime)?;
@ -648,7 +675,7 @@ impl StorageContent for PasswordRecord {
}
"count" => {
if let Ok(count) = u64::from_str_radix(data, 10) {
pr.count = count;
self.count = count;
} else {
log::warn!("count error");
return Err(PasswordSerializationError::BadCount)?;
@ -665,8 +692,6 @@ impl StorageContent for PasswordRecord {
log::trace!("invalid line skipped: {:?}", line);
}
}
*self = pr;
Ok(())
}
fn to_vec(&self) -> Vec<u8> {

View file

@ -20,15 +20,34 @@ pub(crate) struct ListItem {
pub(crate) dirty: bool,
/// this is the name of the key used to refer to the item
pub(crate) guid: String,
/// stash a copy so we can compare to the DB record and avoid re-generating the atime/count string if it hasn't changed.
pub(crate) atime: u64,
pub(crate) count: u64,
}
impl ListItem {
pub fn clone(&self) -> ListItem {
ListItem { name: self.name.to_string(), extra: self.extra.to_string(), dirty: self.dirty, guid: self.guid.to_string() }
ListItem {
name: self.name.to_string(),
extra: self.extra.to_string(),
dirty: self.dirty,
guid: self.guid.to_string(),
atime: self.atime,
count: self.count,
}
}
pub fn key(&self) -> String {
self.name.to_string() + &self.guid.to_string()
}
}
impl Ord for ListItem {
fn cmp(&self, other: &Self) -> Ordering {
self.name.cmp(&other.name)
match self.name.cmp(&other.name) {
Ordering::Equal => {
self.guid.cmp(&other.guid)
},
Ordering::Greater => Ordering::Greater,
Ordering::Less => Ordering::Less,
}
}
}
impl PartialOrd for ListItem {
@ -38,7 +57,7 @@ impl PartialOrd for ListItem {
}
impl PartialEq for ListItem {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
self.name == other.name && self.guid == other.guid
}
}
impl Eq for ListItem {}
@ -69,7 +88,7 @@ pub(crate) struct VaultUx {
action_active: Arc::<AtomicBool>,
/// list of all items to be displayed
item_list: Arc::<Mutex::<Vec::<ListItem>>>,
item_lists: Arc::<Mutex::<ItemLists>>,
/// list of items displayable after filtering
filtered_list: Vec::<ListItem>,
/// the index into the item_list that is selected
@ -139,7 +158,7 @@ impl VaultUx {
menu_mgr: MenuMatic,
actions_conn: xous::CID,
mode: Arc::<Mutex::<VaultMode>>,
item_list: Arc::<Mutex::<Vec::<ListItem>>>,
item_lists: Arc::<Mutex::<ItemLists>>,
action_active: Arc::<AtomicBool>,
) -> Self {
let gam = gam::Gam::new(xns).expect("can't connect to GAM");
@ -166,7 +185,7 @@ impl VaultUx {
token,
mode,
title_dirty: true,
item_list,
item_lists,
selection_index: 0,
filtered_list: Vec::new(),
pddb: RefCell::new(pddb),
@ -305,16 +324,15 @@ impl VaultUx {
item.dirty = true;
}
}
fn backpropagate_item(&mut self, updated_item: ListItem) -> bool {
for item in self.item_list.lock().unwrap().iter_mut() {
if item.guid == updated_item.guid {
item.name = updated_item.name.to_string();
item.extra = updated_item.extra.to_string();
item.dirty = true;
return true;
}
}
false
fn backpropagate_item(&mut self, mut updated_item: ListItem) -> bool {
let il = &mut self.item_lists.lock().unwrap();
let item_list = match self.mode.lock().unwrap().clone() {
VaultMode::Fido => &mut il.fido,
VaultMode::Totp => &mut il.totp,
VaultMode::Password => &mut il.pw,
};
updated_item.dirty = true;
item_list.insert(updated_item.key(), updated_item).is_some()
}
pub(crate) fn nav(&mut self, dir: NavDir) {
match dir {
@ -648,7 +666,13 @@ impl VaultUx {
pub(crate) fn filter(&mut self, criteria: &str) {
self.filtered_list.clear();
for item in self.item_list.lock().unwrap().iter() {
let il = &self.item_lists.lock().unwrap();
let item_list = match self.mode.lock().unwrap().clone() {
VaultMode::Fido => &il.fido,
VaultMode::Totp => &il.totp,
VaultMode::Password => &il.pw,
};
for item in item_list.values() {
if item.name.starts_with(criteria) {
let mut staged_item = item.clone();
staged_item.dirty = true;