From e32ea1e32fb21410440cf2e9493abf516f4acc9f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 01:38:46 +0800 Subject: [PATCH] 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). --- apps/vault/src/actions.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 1765ddae7..0540912a2 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -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::::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::::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::::new(); - match record.read_to_end(&mut data) { + let len = record.attributes().unwrap().len; + let mut data = Vec::::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::::new(); - match record.read_to_end(&mut data) { + let len = record.attributes().unwrap().len; + let mut data = Vec::::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::::new(); - match record.read_to_end(&mut data) { + let len = record.attributes().unwrap().len; + let mut data = Vec::::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();