mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
avoid use of format!() to improve performance (#260)
This commit is contained in:
parent
e77588805c
commit
9699cb761a
2 changed files with 34 additions and 29 deletions
|
|
@ -13,6 +13,7 @@ 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};
|
||||||
use crate::{VaultMode, SelectedEntry};
|
use crate::{VaultMode, SelectedEntry};
|
||||||
|
use crate::utc_now;
|
||||||
|
|
||||||
use crate::fido::U2F_APP_DICT;
|
use crate::fido::U2F_APP_DICT;
|
||||||
use crate::totp::TotpAlgorithm;
|
use crate::totp::TotpAlgorithm;
|
||||||
|
|
@ -640,6 +641,9 @@ impl<'a> ActionManager<'a> {
|
||||||
pw.username = edit_data.content()[1].content.as_str().unwrap().to_string();
|
pw.username = edit_data.content()[1].content.as_str().unwrap().to_string();
|
||||||
pw.password = edit_data.content()[2].content.as_str().unwrap().to_string();
|
pw.password = edit_data.content()[2].content.as_str().unwrap().to_string();
|
||||||
pw.notes = edit_data.content()[3].content.as_str().unwrap().to_string();
|
pw.notes = edit_data.content()[3].content.as_str().unwrap().to_string();
|
||||||
|
// note the edit access, this counts as an access since the password was revealed
|
||||||
|
pw.count += 1;
|
||||||
|
pw.atime = utc_now().timestamp() as u64;
|
||||||
storage.update(&choice, key_name, &mut pw)
|
storage.update(&choice, key_name, &mut pw)
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
@ -709,11 +713,16 @@ impl<'a> ActionManager<'a> {
|
||||||
self.perfentry(&self.pm, PERFMETA_NONE, 1, std::line!());
|
self.perfentry(&self.pm, PERFMETA_NONE, 1, std::line!());
|
||||||
if let Some(data) = key.data {
|
if let Some(data) = key.data {
|
||||||
if let Some(pw) = storage::PasswordRecord::try_from(data).ok() {
|
if let Some(pw) = storage::PasswordRecord::try_from(data).ok() {
|
||||||
let extra = format!("{}; {}{}",
|
let human_time = crate::ux::atime_to_str(pw.atime);
|
||||||
crate::ux::atime_to_str(pw.atime),
|
// avoid allocations
|
||||||
t!("vault.u2f.appinfo.authcount", xous::LANG),
|
let mut extra = String::with_capacity(
|
||||||
pw.count,
|
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(&pw.count.to_string());
|
||||||
let desc = format!("{}/{}", pw.description, pw.username);
|
let desc = format!("{}/{}", pw.description, pw.username);
|
||||||
let li = ListItem {
|
let li = ListItem {
|
||||||
name: desc,
|
name: desc,
|
||||||
|
|
|
||||||
|
|
@ -10,42 +10,38 @@ use chrono::{Utc, DateTime, NaiveDateTime};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
pub(crate) fn atime_to_str(req_atime: u64) -> String {
|
pub(crate) fn atime_to_str(req_atime: u64) -> String {
|
||||||
let mut request_str = String::new();
|
let mut request_str = String::with_capacity(
|
||||||
|
// avoid allocations to speed up this routine, it is in the inner loop of rendering lists of passwords
|
||||||
|
t!("vault.u2f.appinfo.last_authtime", xous::LANG).len() +
|
||||||
|
t!("vault.u2f.appinfo.seconds_ago", xous::LANG).len() +
|
||||||
|
16 // space for the actual duration + some slop for translation
|
||||||
|
);
|
||||||
if req_atime == 0 {
|
if req_atime == 0 {
|
||||||
request_str.push_str(&format!("{}{}",
|
request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.last_authtime", xous::LANG),
|
request_str.push_str(t!("vault.u2f.appinfo.never", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.never", xous::LANG)
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
let now = utc_now();
|
let now = utc_now();
|
||||||
let atime = DateTime::<Utc>::from_utc(
|
let atime = DateTime::<Utc>::from_utc(
|
||||||
NaiveDateTime::from_timestamp(req_atime as i64, 0),
|
NaiveDateTime::from_timestamp(req_atime as i64, 0),
|
||||||
Utc
|
Utc
|
||||||
);
|
);
|
||||||
|
// avoid format! macro, it is too slow.
|
||||||
if now.signed_duration_since(atime).num_days() > 1 {
|
if now.signed_duration_since(atime).num_days() > 1 {
|
||||||
request_str.push_str(&format!("{}{}{}",
|
request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.last_authtime", xous::LANG),
|
request_str.push_str(&now.signed_duration_since(atime).num_days().to_string());
|
||||||
now.signed_duration_since(atime).num_days(),
|
request_str.push_str(t!("vault.u2f.appinfo.days_ago", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.days_ago", xous::LANG),
|
|
||||||
));
|
|
||||||
} else if now.signed_duration_since(atime).num_hours() > 1 {
|
} else if now.signed_duration_since(atime).num_hours() > 1 {
|
||||||
request_str.push_str(&format!("{}{}{}",
|
request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.last_authtime", xous::LANG),
|
request_str.push_str(&now.signed_duration_since(atime).num_hours().to_string());
|
||||||
now.signed_duration_since(atime).num_hours(),
|
request_str.push_str(t!("vault.u2f.appinfo.hours_ago", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.hours_ago", xous::LANG),
|
|
||||||
));
|
|
||||||
} else if now.signed_duration_since(atime).num_minutes() > 1 {
|
} else if now.signed_duration_since(atime).num_minutes() > 1 {
|
||||||
request_str.push_str(&format!("{}{}{}",
|
request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.last_authtime", xous::LANG),
|
request_str.push_str(&now.signed_duration_since(atime).num_minutes().to_string());
|
||||||
now.signed_duration_since(atime).num_minutes(),
|
request_str.push_str(t!("vault.u2f.appinfo.minutes_ago", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.minutes_ago", xous::LANG),
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
request_str.push_str(&format!("{}{}{}",
|
request_str.push_str(t!("vault.u2f.appinfo.last_authtime", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.last_authtime", xous::LANG),
|
request_str.push_str(&now.signed_duration_since(atime).num_seconds().to_string());
|
||||||
now.signed_duration_since(atime).num_seconds(),
|
request_str.push_str(t!("vault.u2f.appinfo.seconds_ago", xous::LANG));
|
||||||
t!("vault.u2f.appinfo.seconds_ago", xous::LANG),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
request_str
|
request_str
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue