feat(tvix/castore/blob/chunked_reader): only reassemble on real seek
If the resulting offset equals to our current position, there's no need to recreate a reader. Change-Id: I855f0c79c514c16ca48a78e12978af2835fbbd6a Reviewed-on: https://cl.tvl.fyi/c/depot/+/11441 Tested-by: BuildkiteCI Reviewed-by: raitobezarius <tvl@lahfa.xyz>
This commit is contained in:
parent
bccde31e77
commit
4d802fa0ae
1 changed files with 16 additions and 16 deletions
|
@ -76,12 +76,9 @@ where
|
||||||
#[instrument(skip(self), err(Debug))]
|
#[instrument(skip(self), err(Debug))]
|
||||||
fn start_seek(self: Pin<&mut Self>, position: std::io::SeekFrom) -> std::io::Result<()> {
|
fn start_seek(self: Pin<&mut Self>, position: std::io::SeekFrom) -> std::io::Result<()> {
|
||||||
let total_len = self.chunked_blob.blob_length();
|
let total_len = self.chunked_blob.blob_length();
|
||||||
let current_pos = self.pos;
|
let mut this = self.project();
|
||||||
let this = self.project();
|
|
||||||
let pos: &mut u64 = this.pos;
|
|
||||||
let mut r: Pin<&mut Box<dyn AsyncRead + Send + Unpin>> = this.r;
|
|
||||||
|
|
||||||
let new_position: u64 = match position {
|
let absolute_offset: u64 = match position {
|
||||||
std::io::SeekFrom::Start(from_start) => from_start,
|
std::io::SeekFrom::Start(from_start) => from_start,
|
||||||
std::io::SeekFrom::End(from_end) => {
|
std::io::SeekFrom::End(from_end) => {
|
||||||
// note from_end is i64, not u64, so this is usually negative.
|
// note from_end is i64, not u64, so this is usually negative.
|
||||||
|
@ -94,7 +91,7 @@ where
|
||||||
}
|
}
|
||||||
std::io::SeekFrom::Current(from_current) => {
|
std::io::SeekFrom::Current(from_current) => {
|
||||||
// note from_end is i64, not u64, so this can be positive or negative.
|
// note from_end is i64, not u64, so this can be positive or negative.
|
||||||
current_pos
|
(*this.pos)
|
||||||
.checked_add_signed(from_current)
|
.checked_add_signed(from_current)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
std::io::Error::new(
|
std::io::Error::new(
|
||||||
|
@ -105,17 +102,20 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// ensure the new position still is inside the file.
|
// check if the position actually did change.
|
||||||
if new_position > total_len {
|
if absolute_offset != *this.pos {
|
||||||
Err(std::io::Error::new(
|
// ensure the new position still is inside the file.
|
||||||
std::io::ErrorKind::InvalidInput,
|
if absolute_offset > total_len {
|
||||||
"seeked beyond EOF",
|
Err(std::io::Error::new(
|
||||||
))?
|
std::io::ErrorKind::InvalidInput,
|
||||||
}
|
"seeked beyond EOF",
|
||||||
|
))?
|
||||||
|
}
|
||||||
|
|
||||||
// Update the position and the internal reader.
|
// Update the position and the internal reader.
|
||||||
*pos = new_position;
|
*this.pos = absolute_offset;
|
||||||
*r = this.chunked_blob.reader_skipped_offset(new_position);
|
*this.r = this.chunked_blob.reader_skipped_offset(absolute_offset);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue