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

feat: rc-zip-tokio: Re-use cursor if it's at the right offset already #71

Merged
merged 2 commits into from
Mar 16, 2024
Merged
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
31 changes: 29 additions & 2 deletions rc-zip-sync/src/read_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,40 @@ where
type File = F;

fn read_zip_with_size(&self, size: u64) -> Result<ArchiveHandle<'_, F>, Error> {
trace!(%size, "read_zip_with_size");
struct CursorState<'a, F: HasCursor + 'a> {
cursor: <F as HasCursor>::Cursor<'a>,
offset: u64,
}
let mut cstate: Option<CursorState<'_, F>> = None;

let mut fsm = ArchiveFsm::new(size);
loop {
if let Some(offset) = fsm.wants_read() {
trace!(%offset, "read_zip_with_size: wants_read, space len = {}", fsm.space().len());
match self.cursor_at(offset).read(fsm.space()) {

let mut cstate_next = match cstate.take() {
Some(cstate) => {
if cstate.offset == offset {
// all good, re-using
cstate
} else {
CursorState {
cursor: self.cursor_at(offset),
offset,
}
}
}
None => CursorState {
cursor: self.cursor_at(offset),
offset,
},
};

match cstate_next.cursor.read(fsm.space()) {
Ok(read_bytes) => {
cstate_next.offset += read_bytes as u64;
cstate = Some(cstate_next);

trace!(%read_bytes, "read_zip_with_size: read");
if read_bytes == 0 {
return Err(Error::IO(std::io::ErrorKind::UnexpectedEof.into()));
Expand Down
32 changes: 31 additions & 1 deletion rc-zip-tokio/src/read_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,41 @@ where
type File = F;

async fn read_zip_with_size(&self, size: u64) -> Result<ArchiveHandle<'_, F>, Error> {
struct CursorState<'a, F: HasCursor + 'a> {
cursor: <F as HasCursor>::Cursor<'a>,
offset: u64,
}
let mut cstate: Option<CursorState<'_, F>> = None;

let mut fsm = ArchiveFsm::new(size);
loop {
if let Some(offset) = fsm.wants_read() {
match self.cursor_at(offset).read(fsm.space()).await {
trace!(%offset, "read_zip_with_size: wants_read, space len = {}", fsm.space().len());

let mut cstate_next = match cstate.take() {
Some(cstate) => {
if cstate.offset == offset {
// all good, re-using
cstate
} else {
CursorState {
cursor: self.cursor_at(offset),
offset,
}
}
}
None => CursorState {
cursor: self.cursor_at(offset),
offset,
},
};

match cstate_next.cursor.read(fsm.space()).await {
Ok(read_bytes) => {
cstate_next.offset += read_bytes as u64;
cstate = Some(cstate_next);

trace!(%read_bytes, "read_zip_with_size: read");
if read_bytes == 0 {
return Err(Error::IO(io::ErrorKind::UnexpectedEof.into()));
}
Expand Down