Skip to content

Commit 65f5385

Browse files
committed
skip reads if the data is already buffered
read_exact_at is often called with a bunch of offsets in the same general area. In a lot of cases, the data needed is still in the buffer from the last call. Let's make use of that by remembering the last sector read and skip reading the same sector again.
1 parent 0ab866d commit 65f5385

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

bios/stage-2/src/disk.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ pub struct DiskAccess {
55
pub disk_number: u16,
66
pub base_offset: u64,
77
pub current_offset: u64,
8+
9+
/// The offset of the sector read in the last call to read_exact_at.
10+
pub last_read_exact_at_offset: Option<u64>,
811
}
912

1013
impl Read for DiskAccess {
@@ -19,7 +22,15 @@ impl Read for DiskAccess {
1922
let buf = unsafe { &mut TMP_BUF };
2023
assert!(current_sector_offset + len <= buf.buffer.len());
2124

22-
self.read_exact_into(buf.buffer.len(), buf);
25+
// Don't read the sector if we already read the same sector in the last
26+
// call.
27+
if self
28+
.last_read_exact_at_offset
29+
.is_none_or(|offset| offset != self.current_offset)
30+
{
31+
self.last_read_exact_at_offset = Some(self.current_offset);
32+
self.read_exact_into(buf.buffer.len(), buf);
33+
}
2334

2435
&buf.buffer[current_sector_offset..][..len]
2536
}

bios/stage-2/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn start(disk_number: u16, partition_table_start: *const u8) -> ! {
8383
disk_number,
8484
base_offset: u64::from(fat_partition.logical_block_address) * 512,
8585
current_offset: 0,
86+
last_read_exact_at_offset: None,
8687
};
8788

8889
let mut fs = fat::FileSystem::parse(disk.clone());

0 commit comments

Comments
 (0)