Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iteration optimisation #107

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ test-attestation-cert-ids = []
[package.metadata.docs.rs]
features = ["serde-extensions", "virt"]
rustdoc-args = ["--cfg", "docsrs"]

[patch.crates-io]
littlefs2-sys = { git = "https://github.com/sosthene-nitrokey/littlefs2-sys.git", branch = "update" }
littlefs2 = { git = "https://github.com/sosthene-nitrokey/littlefs2.git", branch = "iter-optimization" }
19 changes: 19 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ generate_enums! {
Hash: 12
// TODO: add ReadDir{First,Next}, not loading data, if needed for efficiency
ReadDirFilesFirst: 13
ReadDirFilesNth: 63
ReadDirFilesNext: 14
ReadFile: 15
Metadata: 26
Expand All @@ -63,6 +64,7 @@ generate_enums! {

// // CreateDir, <-- implied by WriteFile
ReadDirFirst: 31 // <-- gets Option<FileType> to restrict to just dir/file DirEntries,
ReadDirNth: 64
ReadDirNext: 32 // <-- gets Option<FileType> to restrict to just dir/file DirEntries,
// // returns simplified Metadata
// // ReadDirFilesFirst: 23 // <-- returns contents
Expand Down Expand Up @@ -229,13 +231,24 @@ pub mod request {
- dir: PathBuf
- user_attribute: Option<UserAttribute>

ReadDirFilesNth:
- location: Location
- dir: PathBuf
- start_at: usize
- user_attribute: Option<UserAttribute>

ReadDirFilesNext:

ReadDirFirst:
- location: Location
- dir: PathBuf
- not_before_filename: Option<PathBuf>

ReadDirNth:
- location: Location
- dir: PathBuf
- start_at: usize

ReadDirNext:

ReadFile:
Expand Down Expand Up @@ -419,12 +432,18 @@ pub mod reply {
ReadDirFilesFirst:
- data: Option<Message>

ReadDirFilesNth:
- data: Option<Message>

ReadDirFilesNext:
- data: Option<Message>

ReadDirFirst:
- entry: Option<DirEntry>

ReadDirNth:
- entry: Option<DirEntry>

ReadDirNext:
- entry: Option<DirEntry>

Expand Down
6 changes: 4 additions & 2 deletions src/api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ macro_rules! generate_enums {
($($(#[$attr:meta])? $which:ident: $index:literal)*) => {

#[derive(Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
#[allow(clippy::large_enum_variant)]
#[non_exhaustive]
pub enum Request {
DummyRequest, // for testing
DummyRequest = 0, // for testing
$(
$(#[$attr])?
$which(request::$which),
$which(request::$which) = $index,
)*
}

#[derive(Clone, Eq, PartialEq, Debug)]
#[repr(u8)]
#[allow(clippy::large_enum_variant)]
#[non_exhaustive]
pub enum Reply {
Expand Down
28 changes: 28 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,19 @@ pub trait FilesystemClient: PollClient {
})
}

fn read_dir_nth(
&mut self,
location: Location,
dir: PathBuf,
start_at: usize,
) -> ClientResult<'_, reply::ReadDirNth, Self> {
self.request(request::ReadDirNth {
location,
dir,
start_at,
})
}

fn read_dir_next(&mut self) -> ClientResult<'_, reply::ReadDirNext, Self> {
self.request(request::ReadDirNext {})
}
Expand All @@ -586,6 +599,21 @@ pub trait FilesystemClient: PollClient {
})
}

fn read_dir_files_nth(
&mut self,
location: Location,
dir: PathBuf,
start_at: usize,
user_attribute: Option<UserAttribute>,
) -> ClientResult<'_, reply::ReadDirFilesNth, Self> {
self.request(request::ReadDirFilesNth {
dir,
location,
start_at,
user_attribute,
})
}

fn read_dir_files_next(&mut self) -> ClientResult<'_, reply::ReadDirFilesNext, Self> {
self.request(request::ReadDirFilesNext {})
}
Expand Down
28 changes: 28 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,20 @@ impl<P: Platform> ServiceResources<P> {
};
Ok(Reply::ReadDirFirst(reply::ReadDirFirst { entry: maybe_entry } ))
}
Request::ReadDirNth(request) => {
let maybe_entry = match filestore.read_dir_nth(&request.dir, request.location, request.start_at)? {
Some((entry, read_dir_state)) => {
ctx.read_dir_state = Some(read_dir_state);
Some(entry)
}
None => {
ctx.read_dir_state = None;
None

}
};
Ok(Reply::ReadDirNth(reply::ReadDirNth { entry: maybe_entry } ))
}

Request::ReadDirNext(_request) => {
// ensure next call has nothing to work with, unless we store state again
Expand Down Expand Up @@ -393,6 +407,20 @@ impl<P: Platform> ServiceResources<P> {
Ok(Reply::ReadDirFilesFirst(reply::ReadDirFilesFirst { data: maybe_data } ))
}

Request::ReadDirFilesNth(request) => {
let maybe_data = match filestore.read_dir_files_nth(&request.dir, request.location, request.start_at, request.user_attribute.clone())? {
Some((data, state)) => {
ctx.read_dir_files_state = Some(state);
data
}
None => {
ctx.read_dir_files_state = None;
None
}
};
Ok(Reply::ReadDirFilesNth(reply::ReadDirFilesNth { data: maybe_data } ))
}

Request::ReadDirFilesNext(_request) => {
let read_dir_files_state = ctx.read_dir_files_state.take();

Expand Down
5 changes: 1 addition & 4 deletions src/store/certstore.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use littlefs2::{
path,
path::{Path, PathBuf},
};
use littlefs2::{path, path::PathBuf};
use rand_chacha::ChaCha8Rng;

use crate::{
Expand Down
5 changes: 1 addition & 4 deletions src/store/counterstore.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use littlefs2::{
path,
path::{Path, PathBuf},
};
use littlefs2::{path, path::PathBuf};
use rand_chacha::ChaCha8Rng;

use crate::{
Expand Down
Loading