change read buffer api from read_to_end() to read_exact()

read_to_end() "guesses" the total amount of data to read by doing
roughly 5 calls per couple hundred bytes: it starts by guessing
it's 32 bytes long, then another 32, then 64, then 128, and then
a final call to make sure there's nothing more.

The read() call is somewhat expensive in that it involves initializing
and sending page-sized memory chunks, and every time it also
descends the entire basis hierarchy, recomputing the overlay.

Probably, this can be improved with some sort of server-side caching
of read results -- if a file is read, it seems very likely that
the next few bytes will be read and a smarter caching implementation
could smooth over some of the expense.

However, a much easier fix for now is to just change the read
call to read exactly the length of the file being presented. Since
we already have the key handle, we can get the attributes and then
do a single read of the exact amount.

Doing this improves the speed of the UX substantially, about 40%
faster for the same operation (at least).
This commit is contained in:
bunnie 2022-08-02 01:38:46 +08:00
parent 3d992cedf4
commit e32ea1e32f

View file

@ -736,6 +736,7 @@ impl ActionManager {
il.clear();
match self.mode_cache {
VaultMode::Password => {
let start = self.tt.elapsed_ms();
let keylist = match self.pddb.borrow().list_keys(VAULT_PASSWORD_DICT, None) {
Ok(keylist) => keylist,
Err(e) => {
@ -751,6 +752,9 @@ impl ActionManager {
Vec::new()
}
};
log::info!("listing took {} ms", self.tt.elapsed_ms() - start);
let start = self.tt.elapsed_ms();
let klen = keylist.len();
for key in keylist {
match self.pddb.borrow().get(
VAULT_PASSWORD_DICT,
@ -760,8 +764,13 @@ impl ActionManager {
Some(crate::basis_change)
) {
Ok(mut record) => {
let mut data = Vec::<u8>::new();
match record.read_to_end(&mut data) {
// determine the exact length of the record and read it in one go.
// read_to_end() performs ~5x read calls to do the same thing, because it
// has to "guess" the total record length starting with a 32-byte increment
let len = record.attributes().unwrap().len;
let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => {
if let Some(pw) = deserialize_password(data) {
let extra = format!("{}; {}{}",
@ -794,6 +803,7 @@ impl ActionManager {
},
}
}
log::info!("readout took {} ms for {} elements", self.tt.elapsed_ms() - start, klen);
}
VaultMode::Fido => {
// first assemble U2F records
@ -820,8 +830,10 @@ impl ActionManager {
Some(crate::basis_change)
) {
Ok(mut record) => {
let mut data = Vec::<u8>::new();
match record.read_to_end(&mut data) {
let len = record.attributes().unwrap().len;
let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => {
if let Some(ai) = deserialize_app_info(data) {
let extra = format!("{}; {}{}",
@ -871,8 +883,10 @@ impl ActionManager {
Some(crate::basis_change)
) {
Ok(mut record) => {
let mut data = Vec::<u8>::new();
match record.read_to_end(&mut data) {
let len = record.attributes().unwrap().len;
let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => {
match crate::ctap::storage::deserialize_credential(&data) {
Some(result) => {
@ -923,8 +937,10 @@ impl ActionManager {
Some(crate::basis_change)
) {
Ok(mut record) => {
let mut data = Vec::<u8>::new();
match record.read_to_end(&mut data) {
let len = record.attributes().unwrap().len;
let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => {
if let Some(totp) = deserialize_totp(data) {
let alg: String = totp.algorithm.into();